Wordpress - Remove an action from an external Class
A simple way to achieve this (but without the Class approach) is by filtering the output of wp_head
action hook using the output buffering.
In your theme's header.php
, wrap the wp_head()
call with ob_start($cb)
and ob_end_flush();
functions like:
ob_start('ad_filter_wp_head_output');
wp_head();
ob_end_flush();
Now in theme functions.php
file, declare your output callback function (ad_filter_wp_head_output
in this case):
function ad_filter_wp_head_output($output) {
if (defined('WPSEO_VERSION')) {
$output = str_ireplace('<!-- This site is optimized with the Yoast WordPress SEO plugin v' . WPSEO_VERSION . ' - http://yoast.com/wordpress/seo/ -->', '', $output);
$output = str_ireplace('<!-- / Yoast WordPress SEO plugin. -->', '', $output);
}
return $output;
}
If you want to do all that through the functions.php
without editing header.php
file, you can hook to get_header
and wp_head
action hooks to define the output buffering session:
add_action('get_header', 'ad_ob_start');
add_action('wp_head', 'ad_ob_end_flush', 100);
function ad_ob_start() {
ob_start('ad_filter_wp_head_output');
}
function ad_ob_end_flush() {
ob_end_flush();
}
function ad_filter_wp_head_output($output) {
if (defined('WPSEO_VERSION')) {
$output = str_ireplace('<!-- This site is optimized with the Yoast WordPress SEO plugin v' . WPSEO_VERSION . ' - http://yoast.com/wordpress/seo/ -->', '', $output);
$output = str_ireplace('<!-- / Yoast WordPress SEO plugin. -->', '', $output);
}
return $output;
}
Thanks for all your help, i finally resolved it. I created a functions.php for my child theme, then add
// we get the instance of the class
$instance = WPSEO_Frontend::get_instance();
/* then we remove the function
You can remove also others functions, BUT remember that when you remove an action or a filter, arguments MUST MATCH with the add_action
In our case, we had :
add_action( 'wpseo_head', array( $this, 'debug_marker' ), 2 );
so we do :
remove_action( 'wpseo_head', array( $this, 'debug_marker' ), 2 );
*/
remove_action( 'wpseo_head', array( $instance, 'debug_marker' ), 2 );
I don't think you are going to be able to do that using remove_action
. The function argument in remove_action
will not help you as the debug_marker()
function was not the function that was used in the add_action()
call.
Yoast presumably has something like add_action( "wp_head", "head" )
in his code. So you can remove the "head" function, but debug_marker
was not explicitly added as an action.
You could
- Edit Yoast's source file and remove the debug comment line.
- Extend the
WPSEO_Frontend
class and overload thedebug_marker
function to return "". TBH, I'm not sure how this would work in terms of WP loading the plugin, but could be worth investigating.