I'm trying to add this code into my theme's function.php
function custom_add_author() {
$author = get_the_author();
echo $author;
}
add_action( '???', 'custom_add_author' );
I wanted to add this function code into the specific php named
"author-listing.php"
How can I add this using the correct code?
was it?
add_action( 'author-listing.php', 'custom_add_author' );
If i read your question right..you can just use include() function.
For example at the top of author-listing.php, you can add:
include("function.php");
If you ment a wordpress theme, take alook at the official documentation at: https://developer.wordpress.org/reference/functions/add_action/ there are some more examples as well.
To find out the number and name of arguments for an action, simply
search the code base for the matching do_action() call. For example,
if you are hooking into 'save_post', you would find it in post.php:
<?php do_action( 'save_post', $post_ID, $post, $update ); ?>
Your add_action call would look like:
<?php add_action( 'save_post', 'my_save_post', 10, 3 ); ?>
And your function would be:
function my_save_post( $post_ID, $post, $update ) {
// do stuff here
}
Related
Problem:
I need to add a shortcode [wc_sc_available_coupons] below the product table on the checkout page.
I added the following code in functions.php
The problem is the shortcode displays at the very bottom of the checkout form.
I changed the number 10 to 120 but still the same.
Would you please let me know how to add the shortcode below the product table (=above the payment)?
Code I tried:
add_action( 'woocommerce_after_checkout_form', 'wnd_checkout_code', 10 );
function wnd_checkout_code( ) {
echo do_shortcode('[wc_sc_available_coupons]');
}
Thank you.
Would woocommerce_checkout_after_customer_details hook work for you? So your code would be something like this:
add_action( 'woocommerce_checkout_after_customer_details', 'wnd_checkout_code' );
function wnd_checkout_code( )
{
echo do_shortcode('[wc_sc_available_coupons]');
}
If not then you could try other hooks such as woocommerce_checkout_before_order_review or you could try this woocommerce_before_order_notes as well.
This one is right before the payment:
add_action( 'woocommerce_review_order_before_payment', 'wnd_checkout_code' );
function wnd_checkout_code( )
{
echo do_shortcode('[wc_sc_available_coupons]');
}
Use the action:
add_action("woocommerce_after_cart_table", $action)
OR
add_action("woocommerce_before_cart_collaterals", $action)
Result
https://imgur.com/a/zQVNedb
In general for WooCommerce you can find hook info here:
https://woocommerce.github.io/code-reference/hooks/hooks.html
and for your template:
https://woocommerce.github.io/code-reference/files/woocommerce-templates-cart-cart.html#source-view.159
You can see information on the actions allowed by looking at the source code in woocommerce/templates/ and checking the do_action() functions.
I am building a theme in woocommerce and I have a need to make a small change to the html output by the woocommerce_output_all_notices() in wc-template-functions.php.
The existing function itself is like so:
function woocommerce_output_all_notices() {
echo '<div class="woocommerce-notices-wrapper">';
wc_print_notices();
echo '</div>';
}
What I'd like to do is simply add an id or data attribute and a <span> inside the div, but without editing the code directly (which I clearly don't want to do) I am stumped as it doesn't appear to be a pluggable function and I don't see a way that I can use a hook for this one.
Removing the action that executes the above function should work.
remove_action( 'woocommerce_before_single_product', 'woocommerce_output_all_notices', 10 );
then add a new action that returns what you need
function custom_woocommerce_output_all_notices() {
// Change your code here.
echo '<div class="woocommerce-notices-wrapper">';
wc_print_notices();
echo '</div>';
}
add_action( 'woocommerce_before_single_product', 'custom_woocommerce_output_all_notices', 10 );
I know there are other questions like this but didn't find a reliable answer. So:
First activate the thing (simplyfied code):
add_action( 'after_setup_theme', 'theme_setup' );
function theme_setup() {
add_theme_support('title-tag');
}
Second, delete title tag from header.php.
Third, on page templates, before calling get_header(), add something like this:
add_filter('wp_title', 'set_custom_title', 10, 3);
function set_custom_title($title, $sep, $seplocation){
return 'test';
}
Well, this is not working at all, in any template, being a page, an archive, a custom taxonomy or post type archive. No nothing. Wordpress is generating titles by itself.
Why? Am I doing something wrong? Note that this code once upon a time just worked: used in other sites/themes.
Is it maybe an issue of wp5.2.0?
So, thanks to #Vel, the answer is to re-add the title tag (even if in previous wp versions > don't know til what version you had to delete it form head instead).
Current working code for me:
//functions.php
add_action( 'after_setup_theme', 'theme_setup' );
function theme_setup() {
add_theme_support('title-tag');
}
//header.php
<title><?php wp_title('|', true, 'right'); ?> | <?php echo get_bloginfo('name') ?></title>
//page templates
$window_title = // do something
add_filter('wp_title', function($title, $sep, $seplocation) use($window_title){ return $window_title; }, 10, 3);
Try to use follows code -
add_filter('document_title_parts', function($titles){
return array('title' => 'Custom Title');
});
For anyone still having this issue of the wp_title filter not working, I'd suggest adding a higher priority value. The higher priority value will ensure that your filter is executed and not overriden by other filters in your theme or plugins installed. Please see below: (ref: https://developer.wordpress.org/reference/functions/add_filter/)
// the 9999999 priority value will force this filter to be executed closer to the end. A lower number corresponds with earlier execution
add_filter('wp_title', 'set_custom_title', 9999999, 3);
function set_custom_title($title, $sep, $seplocation){
return 'test';
}
In my case Yoast SEO was changing the way title was rendered and only the following worked:
function filter_lp_title($title) {
return 'New title';
}
add_filter( 'pre_get_document_title', 'filter_lp_title', 25 );
So I have this plugin code
class WC_List_Grid {
public function __construct() {
add_action( 'wp' , array( $this, 'setup_gridlist' ) , 20);
}
function setup_gridlist() {
add_action( 'woocommerce_before_shop_loop', array( $this, 'gridlist_toggle_button' ), 30);
}
function gridlist_toggle_button() {?>
<nav class="gridlist-toggle">
<span class="dashicons dashicons-grid-view"></span> <em><?php _e( 'Grid view', 'woocommerce-grid-list-toggle' ); ?></em>
<span class="dashicons dashicons-exerpt-view"></span> <em><?php _e( 'List view', 'woocommerce-grid-list-toggle' ); ?></em>
</nav>
<?php }
}
I want to change the content of the gridlist_toggle_button function.
This is how I am planning to change the content of that function. Like writing another function with almost the same html as the original one but bits of my changes. Something like
add_action('woocommerce_before_shop_loop','new_gridlist_toggle_button')
function new_gridlist_toggle_button() {?>
<nav class="gridlist-toggle">
<span class="dashicons dashicons-grid-view"></span> <em><?php _e( 'Grid view', 'woocommerce-grid-list-toggle' ); ?></em>
<span class="dashicons dashicons-exerpt-view"></span> <em><?php _e( 'List view', 'woocommerce-grid-list-toggle' ); ?></em>
</nav>
<?php }
}
That way, I won't have to change the plugin files directly
For that, I am trying to remove its related action.
add_action( 'woocommerce_before_shop_loop', array( $this, 'gridlist_toggle_button' ), 30);
So that I may use my own code. But I can't seem to remove this action. I have tried this so far.
Method 1:
global $WC_List_Grid ;
remove_action( 'woocommerce_before_shop_loop', array( $WC_List_Grid, 'gridlist_toggle_button' ), 100);
Method 2:
function remove_plugin_actions(){
global $WC_List_Grid ;
remove_action( 'woocommerce_before_shop_loop', array( $WC_List_Grid, 'gridlist_toggle_button' ), 100);
}
add_action('init','remove_plugin_actions');
Method 3
remove_action( 'woocommerce_before_shop_loop', array( 'WC_List_Grid', 'gridlist_toggle_button' ), 100);
None of them seems to work.
With a bit of brainstorming, I think there could be two possible causes.
Its not working because the action I want to remove is not directly attached to the hook. Its action inside action.
I am trying to block the output of gridlist_toggle_button via functions.php. But if the plugins load before functions.php then the function to be blocked away is already being called and hence it is always showing output.
I am no good with OOP at all. Can someone please help me out?
Whenever $wc_lg = new WC_List_Grid() is instantiated, its going to trigger wp hook. Right after global WP class object is set up, the instance $wg_lg is going to call setup_gridlist() on itself. Its going to trigger woocommerce_before_shop_loop hook. And whenever that happens, again $wg_lg is going to call a function on itself. Namely gridlist_toggle_button().
I want to change the content of the gridlist_toggle_button function
Why not just to change it inside plugin ? Like override everything whats inside it.
global $WC_List_Grid;
How do you know that $WC_List_Grid has its name? Its just a class name. It can be instantiated under any name like $foo or $bar.
I assume that you are coding in functions.php file of your template. Plugins are being loaded before functions.php and remove_action() can only work after add_action() happend.
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
So this should actually work just for removing the action, but how that helps you to change content inside of the function? :
remove_action(
'woocommerce_before_shop_loop',
array('WC_List_Grid', 'gridlist_toggle_button'),
30
);
Notice that you were using priority 100
EDIT
I found out that do_action('woocommerce_archive_description') happens in archive-product.php and in wc-template-functions.php right before woocommerce_before_shop_loop hook actions are being executed in plugin. Try to use this:
function remove_plugin_actions(){
global $WC_List_Grid;
remove_action( 'woocommerce_before_shop_loop', array( $WC_List_Grid, 'gridlist_toggle_button' ), 30);
}
add_action('woocommerce_archive_description','remove_plugin_actions');
I created my own child theme. Everything works great except I can't seem to unregister a hoook.
$this is class TC_footer_main and the following code is in the __construct
add_action ( '__colophon' , array( $this , 'tc_colophon_center_block' ), 20 );
I tried several remove actions with no success. I am just trying to change/remove the footer:
remove_action( '__colophon' , 'tc_colophon_center_block' , 55);
or
remove_action( '__colophon' , array('TC_footer_main','tc_colophon_center_block') , 55);
I've also tried
remove_action( '__colophon' , TC_footer_main::$instance->tc_colophon_center_block() , 55);
But that threw an error as TC_footer_main was not loaded by the time my functions.php file ran.
I am just trying to change/remove the footer:
I think you're making it way more complex, to modify the output of the tc_colophon_center_block() method, than it has to be.
Just use the tc_credits_display filter:
add_filter( 'tc_credits_display', function( $html )
{
// Modify output to your needs!
return $html;
} );
to modify that block to your needs.
To totally remove the output (if that's allowed), simply use:
add_filter( 'tc_credits_display', '__return_null', PHP_INT_MAX );
You have further access to filters like:
tc_copyright_link
tc_credit_link
tc_wp_powered
to choose from.
That's it!
For your purpose add the following code in function.php. It will get call on after_setup_theme hook.
// replace parent function
function child_theme_function () {
// your code goes here
}
function my_theme_setup () {
remove_action( '__colophon', 'tc_colophon_center_block', 1000 );
add_action( '__colophon', 'child_theme_function', 1000 );
}
add_action( 'after_setup_theme', 'my_theme_setup' );
You can also try for overriding the parent class from child class as described here: https://thethemefoundry.com/tutorials/advanced-customization-replacing-theme-functions/
you werent too far away...one problem you may have is you are trying to remove the hook before it has been added by your parent theme..the class could be initialized at a later stage...
im not too sure when your hook runs, but hopefully its after init
add_action('init', 'remove_parent_hook');
function remove_parent_hook(){
remove_action( '__colophon' , array('TC_footer_main','tc_colophon_center_block') , 20); // needs to be the same priority
}
you can obviously now just add a action for your new function.
There is a offchance that a anonymous function has been added, often the significance of &$this is overlooked when trying to remove a hooked function. This is a pain because wp will assign a random string as the key name & the function name for the function, its different every time so it cant be guessed. But we can search for the function name within the key so something like this will work
function remove_anon_hk($hook, $function, $priority=10 ){
global $wp_filter;
$hooks= $wp_filter[$hook][$priority];
if(empty ($hooks))
return;
foreach($hooks as $hk=>$data):
if(stripos($hk, $function) !== false ){
unset($wp_filter[$hook][$priority][$hk]);
}
endforeach;
}
add_action('init', function(){
remove_anon_hk('__colophon', 'tc_colophon_center_block');
});