1

I'm trying to remove an action added by AMP plugin

add_action( 'wp_head', 'remove_hooks' );
function remove_hooks(){
  remove_action('wp_head', 'amp_frontend_add_canonical');
}

The plugin adds the action like this:

add_action( 'wp_head', 'amp_frontend_add_canonical' );

function amp_frontend_add_canonical() {
    if ( false === apply_filters( 'amp_frontend_show_canonical', true ) ) {
        return;
    }

    $amp_url = amp_get_permalink( get_queried_object_id() );

    printf( '<link rel="amphtml" href="%s" />', esc_url( $amp_url ) );
}

but I don't succeed - I'm not sure if I have to set a priority ?

1

Fixed it by changing to

add_action( 'wp_enqueue_scripts', 'remove_hooks' );

function remove_hooks(){
    global $wp_query;
    $postid = $wp_query->post->ID;
    $format = get_post_format( $postid );

    if ($format == 'gallery') {
    remove_action('wp_head', 'amp_frontend_add_canonical',10);
    }
}
  • this will work, but a more proper place to hook your action on is on template_redirect which is "called" just before wordpress load template files. p_enqueue_scripts is actually called from inside of the wp_head action which makes his code a little shaky. – Mark Kaplun Oct 5 '17 at 13:31

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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