I'm new to WooCommerce and Storefront theme. I am trying to get a sense of the source code before I start modifying it. I'm just having a little difficulty finding out where all the necessary codes are located.
When I open the header.php, I got lost because every functions were hooked to some other files like this.
do_action( 'storefront_before_header' );
Where are these functions defined in Storefront theme? and how can I find where all these do_action functions are defined in the future other than just opening all the files are searching for the strings?
I've looked into files such as:
storefront-functions.php
storefront-template-functions.php
storefront-template-hooks.php
functions.php
For all woocommerce-related products, there's a #hooked tag in phpdoc block before each hook. If there're no #hooked tags, that hook is just a reserved hook which may be used in the future.
Let's see the storefront_header hook:
/**
* Functions hooked into storefront_header action
*
* #hooked storefront_skip_links - 0
* #hooked storefront_social_icons - 10
* #hooked storefront_site_branding - 20
* #hooked storefront_secondary_navigation - 30
* #hooked storefront_product_search - 40
* #hooked storefront_primary_navigation_wrapper - 42
* #hooked storefront_primary_navigation - 50
* #hooked storefront_header_cart - 60
* #hooked storefront_primary_navigation_wrapper_close - 68
*/
do_action( 'storefront_header' );
After the #hooked tag is a function name and priority in which the function is executed when the action is fired. Lower numbers correspond with earlier execution.
Most of the functions hooked to the hook is located inside storefront-template-functions.php and added inside storefront-template-hooks.php.
You can find those functions with simple IDE searchs inside theme folder.
Related
I have started to learn how to create templates with WooCommerce and I had faced with a little problem. For instance, in the php file content-single-product.php of Woocommerce plugin I have strings like that:
<?php
/**
* woocommerce_single_product_summary hook.
*
* #hooked woocommerce_template_single_title - 5
* #hooked woocommerce_template_single_rating - 10
* #hooked woocommerce_template_single_price - 10
* #hooked woocommerce_template_single_excerpt - 20
* #hooked woocommerce_template_single_add_to_cart - 30
* #hooked woocommerce_template_single_meta - 40
* #hooked woocommerce_template_single_sharing - 50
*/
do_action( 'woocommerce_single_product_summary' );
?>
And for example, when I want to edit this (delete some fields and change the structure) I try erase the string:
do_action( 'woocommerce_single_product_summary' );
and after that write like this:
<?php
/**
* woocommerce_single_product_summary hook.
*
* #hooked woocommerce_template_single_title - 5
* #hooked woocommerce_template_single_rating - 10
* #hooked woocommerce_template_single_price - 10
* #hooked woocommerce_template_single_excerpt - 20
* #hooked woocommerce_template_single_add_to_cart - 30
* #hooked woocommerce_template_single_meta - 40
* #hooked woocommerce_template_single_sharing - 50
*/
//do_action( 'woocommerce_single_product_summary' );
do_action('woocommerce_template_single_title');
?>
Could you tell me please why this doesn't work?
What is the right way to edit like that?
Thanks
First in reference below you will find how to override properly woocommerce templates via a theme (avoiding editing the plugin templates).
In your first code snippet, as you can see for woocommerce_single_product_summary hook, you have in order all the different templates that are #hooked in this hook location with do_action() WordPress function:
do_action( 'woocommerce_single_product_summary' );
So in your customized code (the 2nd code snippet) you have just replaced the hook, by the hooked template slug (that is NOT a hook) and will NOT work as an entry point action hook. See the references at the bottom of this answer for the list of WooCommerce actions and filters existing hooks…
Consequences: All other hooked templates in the commented list code (beginning with #hooked) will be missing if you replace a hook by a template slug.
For the hooks used in the templates see this helpful WooCommerce Visual Hook Guide
Explanations (How to):
HOW TO - Concrete example:
You want to customize woocommerce_template_single_title hooked template in woocommerce_single_product_summary hook.
THE HOOK NAME: woocommerce_single_product_summary hook.
THE TEMPLATES HOOKED (+priority order number) => corresponding template file name:
— woocommerce_template_single_title (5) => single-product/title.php
— woocommerce_template_single_rating (10) => single-product/rating.php
— woocommerce_template_single_price (10) => single-product/price.php
— woocommerce_template_single_excerpt (20) => single-product/short-description.php
— woocommerce_template_single_add_to_cart(30) => single-product/add-to-cart/ (6 files depending on product type)
— woocommerce_template_single_meta (40) => single-product/review-meta.php
— woocommerce_template_single_sharing - (50) => single-product/share.php
Then you will need to edit the corresponding woocommerce_single_product_summary hook title.php located in single-product (sub folder)… Finally is not so complicated, once we understand the template structure files and the hooks in that templates.
The priority number, gives the order for the hooked templates: Smaller in first, bigger at the end…
See also: Hooks and their hooked functions execution queue in Wordpress and Woocommerce
Others ways:
You can also use all that existing templates hooks to target very specific changes or customizations, with custom functions located in the function.php file of your active child theme (or theme) or any plugin file too.
Example using add_action() WordPress function:
// define the woocommerce_single_product_summary callback function
function my_custom_action() {
echo '<p>This is my custom action function</p>';
};
add_action( 'woocommerce_single_product_summary', 'my_custom_action', 15 );
This function has a priority number of 15 and will display
"This is my custom action function" string text, between the product price and the product short description…
Optional arguments of this hooked function for this hook:
• The template slug (string).
• The priority (int).
References:
Template structure & Overriding templates via a theme
WooCommerce Hooks: Actions and filters
WooCommerce Code - Action and Filter Hook Reference
WooCommerce Visual Hook Guide: Single Product Page
I am trying to unhook and modify an action from within my child themes functions.php file.
The WordPress plugin Sensei, adds this action in line 86 of this document.
https://github.com/Automattic/sensei/blob/master/includes/class-sensei-modules.php#L86
The action references a function further down the page that is responsible for outputting a dynamic header element.
/**
* Show the title modules on the single course template.
*
* Function is hooked into sensei_single_course_modules_before.
*
* #since 1.8.0
* #return void
*/
public function course_modules_title( ) {
if( sensei_module_has_lessons() ){
echo '<header><h2>' . __('Modules', 'woothemes-sensei') . '</h2></header>';
}
}
My goal here is to change the html currently output as 'Modules' to something else.
I have tried to the following in my child themes functions.php file but neither seem to be working.
remove_action( 'sensei_single_course_modules_before', array( 'Sensei_Core_Modules', 'course_modules_title' ), 20);
remove_action( 'sensei_single_course_modules_before', array( 'Sensei()->Sensei_Core_Modules', 'course_modules_title' ), 20);
The issue is, I do not know how to determine which initial parameter, to add to the array to call the correct class. Because I am accessing it externally I cannot use $this like it is being used in the core file.
In order to remove the action you have to find the instance of the class. This is an assumption since I don't have access to the source code of Sensei but big chance there is one since most WordPress plug-ins use this method.
When you find the instance name you can load it using global $senseiInstance - replace this with the actual name of the variable.
Then you can remove the action using for example this code:
remove_action( 'sensei_single_course_modules_before', array( $senseiInstance, 'course_modules_title' ), 20);
More information can be found in for example this article: https://www.sitepoint.com/digging-deeper-wordpress-hooks-filters.
Hope this helps you out!
This will probably open a door for me as there's something I'm missing so a WooCommerce 101 please;
part of the WooCommerce template archive-product.php contains the code;
<?php
/**
* woocommerce_before_shop_loop hook.
*
* #hooked woocommerce_result_count - 20
* #hooked woocommerce_catalog_ordering - 30
*/
do_action( 'woocommerce_before_shop_loop' );
?>
From this, and reading the documentation, it implies that this;
remove_action( 'woocommerce_before_shop_loop', 'woocommerce_result_count', 20 );
should remove the result count from the product categories returned. Only it doesn't.
What's wrong?
I was going to say you should read the documentation but it is leaving an important part out.
remove_action() cannot be called directly and must, itself, be added to an action hook. The action hook needs to come before the action being removed. In this case I would just use the same hook, but an earlier priority (default is 10, I've used 1)
function so_38878702_remove_hook(){
remove_action( 'woocommerce_before_shop_loop', 'woocommerce_result_count', 20 );
}
add_action( 'woocommerce_before_shop_loop', 'so_38878702_remove_hook', 1 );
To remove result count action from Woocommerce -
add_action('woocommerce_before_shop_loop', 'remove_result_count' );
function remove_result_count()
{
remove_action( 'woocommerce_before_shop_loop', 'woocommerce_result_count', 20);
}
use this code in your function.php file.
Important: To remove a hook, the $function_to_remove and $priority
arguments must match when the hook was added. This goes for both
filters and actions. No warning will be given on removal failure.
http://codex.wordpress.org/Function_Reference/remove_action
Here you can get WooCommerce Action and Filter Hook -
https://docs.woothemes.com/wc-apidocs/hook-docs.html
Like many other people, I have a problem with TinyMCE stripping HTML tags - specifically the empty ones I'm using with Font Awesome.
I have researched and tried solutions and nothing has worked. I'm not especially strong with PHP, but the problem I'm running into is this: everybody says modifiy the tinyMCE.init function in the tinymce.js file. However being in the newest version of WP, I don't have that. What I have class-wp-editor.php, wherein lives this:
/*
* For people who really REALLY know what they're doing with TinyMCE
* You can modify $mceInit to add, remove, change elements of the config
* before tinyMCE.init. Setting "valid_elements", "invalid_elements"
* and "extended_valid_elements" can be done through this filter. Best
* is to use the default cleanup by not specifying valid_elements,
* as TinyMCE checks against the full set of HTML 5.0 elements and attributes.
*/
if ( $set['teeny'] ) {
/**
* Filter the teenyMCE config before init.
*
* #since 2.7.0
*
* #param array $mceInit An array with teenyMCE config.
* #param string $editor_id Unique editor identifier, e.g. 'content'.
*/
$mceInit = apply_filters( 'teeny_mce_before_init', $mceInit, $editor_id );
} else {
/**
* Filter the TinyMCE config before init.
*
* #since 2.5.0
*
* #param array $mceInit An array with TinyMCE config.
* #param string $editor_id Unique editor identifier, e.g. 'content'.
*/
$mceInit = apply_filters( 'tiny_mce_before_init', $mceInit, $editor_id );
}
Now I know I have to do something with valid_elements, or extended_valid_elements, or verify_html, but I don't know how to do it. I can see from the comments where to put it, but I don't know which to use or the proper syntax.
I found this fiddle: http://fiddle.tinymce.com/j9baab/1 but like I said I don't have that function anywhere in WP.
Please help!
You really don't want to modify the class-wp-editor.php file as each time you update WordPress it will get overwritten.
The easiest way to extend/modify the settings of TinyMCE is to build a simple WordPress Plugin and tie into the hooks WordPress provides to change the editor's settings.
To solve your particular desire to add options to the init you want to look at
the 'tiny_mce_before_init' hook. You might do something like this in the plugin:
add_filter('tiny_mce_before_init', 'add_my_options');
function add_my_options($opt) {
// $opt is the existing array of options for TinyMCE
// We simply add a new array element where the name is the name
// of the TinyMCE configuration setting. The value of the array
// object is the value to be used in the TinyMCE config.
$opt['extended_valid_elements'] = '*[*]';
return $opt;
}
Writing a simple WP plugin is not too hard - there are plenty of examples on the web. For something this simple its really just a single PHP file. Once you have the plugin built you just install and activate it. Once activated, your code is run each time TinyMCE is invoked and your options are injected into the init code.
EDIT: Here is the code from the OPs comment (easier to read than how the comments are formatted):
<?php
/**
* Plugin Name: Disable TinyMCE Filters
* Plugin URI: http://mindspyder.com
* Description: This plugin disables annoying TinyMCE Filters.
* Version: 1.0.0
* Author: Brandon Snow
* Author URI: http://mindspyder.com
* License: GPL2
*/
add_filter('tiny_mce_before_init', 'add_my_options');
function add_my_options($opt) {
// $opt is the existing array of options for TinyMCE
// We simply add a new array element where the name is the name
// of the TinyMCE configuration setting. The value of the array
// object is the value to be used in the TinyMCE config.
$opt['extended_valid_elements'] = '*[*]';
return $opt;
}
?>
I need to add extra fields to the Add new user and Edit user pages in wordpress admin dashboard.
I can add this to Edit user using the hook edit_user_profile. But how to add it in Add new user page?
Also I want to add some extra columns to the user wp list table.
add_action( 'edit_user_profile', 'my_show_extra_profile_fields' );
function my_show_extra_profile_fields( $user ) {
}
If you consult the core code, in /wp-admin/user-new.php you can find two hooks:
user_new_form_tag – before the existing form fields
user_new_form – after the existing form fields
If you check /wp-admin/user-new.php you'll see two do_action, though I never tried using any of these hooks
hook to add tags or attribute on form
<form method="post" name="adduser" id="adduser" class="validate" novalidate="novalidate"<?php
/**
* Fires inside the adduser form tag.
*
* #since 3.0.0
*/
do_action( 'user_new_form_tag' );
?>>
and
/**
* Fires at the end of the new user form.
*
* Passes a contextual string to make both types of new user forms
* uniquely targetable. Contexts are 'add-existing-user' (Multisite),
* and 'add-new-user' (single site and network admin).
*
* #since 3.7.0
*
* #param string $type A contextual string specifying which type of new user form the hook follows.
*/
do_action( 'user_new_form', 'add-existing-user' );
and
<?php
/** This action is documented in wp-admin/user-new.php */
do_action( 'user_new_form', 'add-new-user' );
?>
You might also want to check user_register hook