Replacing parent action of Custormizr theme - php

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');
});

Related

why wp_title filter is not working at all?

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 );

Woocommerce | If page_id= .. { }

I am trying to apply specific functions to a certain page ID in Woocommerce.
However, it does not seem to apply. I have looked at both Wordpress and Woommerce conditional tags (https://docs.woocommerce.com/document/conditional-tags/).
The simple code I am testing is shown below, but even that is not working.
if ( is_product()) {
echo ' HALLO sdfdsfsdfsdfsdf ';
}
I have also tried with page_id and post_id but cannot get this to work.
I have added this code to my functions.php in my child theme.
Does anybody know to to write a specific function only for a specific product page (I know the value).
Method - 1
is_page() relies on a complete global $wp_query object. If you call it before the action template_redirect has been fired, it might be impossible to get that data.
add_filter( 'template_include', function( $template ) {
if ( is_product() )
echo 'HELLO';
return $template;
});
Method - 2
The WordPress/WooCommerce conditional tags won’t work until after the wp action is fired, making it the earliest point at which you can use the tags. You can view the entire list, and the order of the core WordPress actions at the Action Reference page.
add_action( 'wp', 'init' );
function init() {
if ( is_shop() ) {
echo 'HELLO, this works!';
}
}

Hook Do-action with Add_action

I am trying to call function, created with add_action using do_action:
In the function.php of the theme:
function bleute_utbi_service_type()
{
return "service";
}
add_action('utbi_service_type', 'bleute_utbi_service_type');
Now, I need to get the value of the function in a plugin file:
// 'x' plugin file:
function get_valor(){
$val = do_action('utbi_service_type');
echo "this is the valor:" . $val";
}
This way of doing is not working, $val return 'null'...why?
Action hooks do not return content, and honestly if you need an action hook to return content there is a pretty good chance that you are doing something wrong.
add_action() takes the identification of a function, in your case
bleute_utbi_service_type(), and puts it into a list of functions to be called
whenever anybody calls do_action().
Either use $params with your do_action and add_action and then set the $value in your add_action callback function or use filters to return contents. To know how return works with filters, u may refer here: Wordpress: How to return value when use add_filter? or https://developer.wordpress.org/reference/functions/add_filter/
If you want to do it with add_action than you have to follow this referenece by passing argument to add_action
Reference
else try using apply filters like this.
add above code in function.php:
function example_callback( $string ) {
return $string;
}
add_filter( 'example_filter', 'example_callback', 10, 1 );
function get_valor(){
$val = apply_filters( 'example_filter', 'filter me' );
echo "this is the valor:". $val;
}
and add below code to wherever you want to print:
get_valor();
hope it works for you :)

WordPress - remove action defined within plugin class

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');

add function code into specific php page

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
}

Categories