Jens Törnell
Jens Törnell

Reputation: 24768

Wordpress init remove_action with conditional tags

I need to do this for a theme:

remove_action( 'wp_head', 'rel_canonical' );

BUT I need to do that with conditional tags. The code below don't work.

if(is_page('comment'))
{
   remove_action( 'wp_head', 'rel_canonical' );
}

AND I need to do this with a plugin.

I tried to hook the if statement into the function test, like this:

add_action('init', 'test');
function test()
{
   if(is_page('comment'))
   {
      remove_action( 'wp_head', 'rel_canonical' );
   }
}

Because it is run before anything else the conditional tags don't work, I guess.

Any ideas?

Upvotes: 2

Views: 2910

Answers (2)

Jens Törnell
Jens Törnell

Reputation: 24768

I found out that instead of using init as an action, I should use this:

add_action('template_redirect', 'test');

Then it runs before the header.php but after the conditional tags are set.

Upvotes: 4

Richard M
Richard M

Reputation: 14535

Try replacing the rel_canonical action with your own function containing the condition, something like this:

remove_action('wp_head', 'rel_canonical');
function my_rel_canonical() {
    if (!is_page('comment')) rel_canonical();
}
add_action('wp_head', 'my_rel_canonical');

Upvotes: 1

Related Questions