Question

I'm trying to remove the plugin's Page Links To metabox for Author user roles from the edit.php screen (article editing)... I can do it for all other metaboxes but page-links-to.

This is my code

if ( current_user_can('author') )
{

    function my_remove_meta_boxes()
    {
        remove_meta_box('postexcerpt', 'post', 'normal');
        remove_meta_box('trackbacksdiv', 'post', 'normal');
        remove_meta_box('postcustom', 'post', 'normal');
        remove_meta_box('revisionsdiv', 'post', 'normal');
        remove_meta_box('commentstatusdiv', 'post', 'normal');
        remove_meta_box('commentsdiv', 'post', 'normal');
        remove_meta_box('slugdiv', 'post', 'normal');
        remove_meta_box('tagsdiv-post_tag', 'post', 'side');
        remove_meta_box('categorydiv', 'post', 'side');
        remove_meta_box('postimagediv', 'post', 'side');

        remove_meta_box('page-links-to', 'post', 'normal');

    }
    add_action( 'do_meta_boxes', 'my_remove_meta_boxes' );

In plugin's source code I found this:

function do_meta_boxes( $page, $context ) {
        // Plugins that use custom post types can use this filter to hide the PLT UI in their post type.
        $plt_post_types = apply_filters( 'page-links-to-post-types', array_keys( get_post_types( array('show_ui' => true ) ) ) );

        if ( in_array( $page, $plt_post_types ) && 'advanced' === $context )
            add_meta_box( 'page-links-to', 'Page Links To', array( $this, 'meta_box' ), $page, 'advanced', 'low' );
    }

But I cannot find any way to detect a working hook to remove the metabox.

Was it helpful?

Solution

Two options.

Add a lower priority to the hook.

add_action( 'do_meta_boxes', 'my_remove_meta_boxes', 9999 );

By this point, the plugin hook should already have run and the removal will work. I suggest moving current_user_can inside the callback function my_remove_meta_boxes().

Use the hook provided by the plugin

add_filter( 'page-links-to-post-types', 'remove_box_so_16290352' );

function remove_box_so_16290352( $post_types )
{
    $key = array_search( 'page', $post_types );
    if( $key !== false ) {
        unset($post_types[$key]);
    }

    return $post_types;
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top