0

I'm trying to look around over Google of how to hook the add_action and change the priority of Yoast Canonical,

from: 20

add_action( 'wpseo_head', array( $this, 'canonical' ), 20 );

to: 55

add_action( 'wpseo_head', array( $this, 'canonical' ), 55 );

This affect the position from the frontend head tag section. Is there a way to manipulate this function to hook it inside my functions.php file?

Note: I tried to copy and paste this to my function, but hook is not reaching the action.

Updated Response Fig. 1

global $wpseo;
$wpseo = new WPSEO_Frontend();

remove_action( 'wpseo_head', array( $wpseo, 'canonical' ), 20 );
add_action( 'wpseo_head', array( $wpseo, 'canonical' ), 55 );

I tried using this as I can see the WPSEO_Frontend was the declared class where the add_action( 'wpseo_head', array( $wpseo, 'canonical' ), 20 ); is located... But ended-up to broken site.

3
  • Seems like intimate knowledge with wpseo is required to answer this question which makes it a better fit to their support channel than here. Anyway your code is not what @majick had suggested. it is also possible that you do not place it at right filter. Sep 28, 2017 at 17:17
  • Hi Mark, Yeah, I actually contacted their support but they cannot also give me some dev info regarding what I need to override that canonical. They just give me a go signal to freely modified the plugin, but the question is, what will happen if someone updated the premium plugin of Yoast... For sure it will overwrite my simple code there again. Sep 28, 2017 at 20:28
  • 1
    yes, I am amazed they even suggested that.... Sep 28, 2017 at 20:34

1 Answer 1

2

You would need to look in the Yoast plugin to find what the global class is that is referred to as $this, so that you can access it outside the class context. If it was for example, $wpseo, in your functions.php you could do something like:

$wpseo = WPSEO_Frontend::get_instance();
remove_action( 'wpseo_head', array( $wpseo, 'canonical' ), 20 );
add_action( 'wpseo_head', array( $wpseo, 'canonical' ), 55 );

To find the class name to use, look for the name of the class that contains the add_action with $this in it as you have already, and then get the instance of that class, like $wpseo = WPSEO_Frontend::get_instance();

UPDATE changed code to reflect actual found and tested class instance.

3
  • Thank you for your answer... I tried to dig deeper on the plugin but I ended up to broken WP site... I update my question including the test flow of your suggestion. Sep 28, 2017 at 16:56
  • on a quick look, seems like they don't use a global there so don't use that, just try $wpseo = WPSEO_Frontend::get_instance(); instead and don't do new WPSEO_Frontend();
    – majick
    Sep 28, 2017 at 21:18
  • 1
    I can confirm that this worked with $wpseo = WPSEO_Frontend::get_instance(); followed by your answer above. Thanks for this. @majick Dec 18, 2017 at 20:10

Not the answer you're looking for? Browse other questions tagged or ask your own question.