2

I am having to update a website for someone that was built with one of those (not very nice, clunky) pre-made themes. The theme (it's the parent theme, actually, called Mineral) inserts a meta box in all of the post and page edit pages of the site with various layout options.

I want to hide this though for various pages. So, to get this working, I have tried the normal code:

function remove_page_excerpt_field() {
    remove_meta_box( 'pexeto-meta-page-boxes' , 'page' , 'normal' ); 
}
add_action( 'admin_menu' , 'remove_page_excerpt_field' );

where pexeto-meta-page-boxes is the name of the id of the meta box's outer div. This has no effect though.

I've had this problem in the past but never got to the bottom of it. I assume it's due to a priority issue of some kind. Is there a way to set up my remove_meta_box() function with a greater priority to override whatever was defined by the parent theme's coders?

  • Does it make a difference if you hook into add_meta_boxes instead of admin_menu with a high priority? – Ahmed Fouad Sep 28 '16 at 14:14
3

Remove WordPress Meta Boxes

You have to make use of the function called remove meta box.

Description: Removes a meta box or any other element from a particular post edit screen of a given post type. It can be also used to remove a widget from the dashboard screen.

Steps to remove the meta box from WordPress Dashboard:

When removing WordPress meta boxes, I’ve seen it done in a number of different ways two of which are often done via JavaScript. Though this is functional for many, client-side solutions aren’t always great especially as it relates to accessibility.

On top of that, hiding things via JavaScript is kind of a hack – the visibility of elements should often be set in terms of CSS and it will likely cause a little bit of a flicker when the element is being hidden while the DOM loads.

Anyway, the two ways I’ve often seen it done is when:

Developers look for the check boxes under the Screen Options and then trigger the click event.

Developers sniff out the meta boxes ID and then use jQuery’s hide() method to hide the meta boxes from the page.

Sure, these work, but you’re still faced with the challenges of accessibility and with the fact that you’re modifying something that should probably be done via CSS.

Unless a hook exists.

And that’s really the mentality that we, as WordPress developers, should have whenever we’re attempting to introduce, remove, or modify anything as it relates to WordPress both in the dashboard and on the front-end.

Specifically, we need to ask ourselves:

Given what I need to do, is there a hook that makes this available?

And this is this case (and many, many cases), there is. We can take advantage of the default_hidden_meta_boxes hook.

Using The Hook

The function accepts two arguments:

  1. An array of meta boxes that should be hidden
  2. The current screen being displayed

This allows us to modify the existing array of hidden meta boxes (or create our own) and only trigger it on specific screen’s, as well.

So let’s say that we have a custom post type called acme_post_type and we want to hide the Categories, Author, Post Excerpt, and Slug meta box.

<?php
add_action( 'default_hidden_meta_boxes', 'acme_remove_meta_boxes', 10, 2 );
/**
 * Removes the category, author, post excerpt, and slug meta boxes.
 *
 * @since    1.0.0
 *
 * @param    array    $hidden    The array of meta boxes that should be hidden for Acme Post Types
 * @param    object   $screen    The current screen object that's being displayed on the screen
 * @return   array    $hidden    The updated array that removes other meta boxes
 */
function acme_remove_meta_boxes( $hidden, $screen ) {
    if ( 'acme_post_type' == $screen->id ) {
        $hidden = array(
            'acme_post_type_categorydiv',
            'authordiv',
            'postexcerpt',
            'slugdiv'
            );

    }
    return $hidden;    
}

First, the code checks to see if we’re on the screen for the custom post type. If so, then we define a new array that includes the IDs of the meta boxes that we want to hide. After that, we the array to WordPress and the end-result of the function will be that the specified meta boxes will be hidden from view.

In terms of getting the ID of the meta boxes that you want to hide, you can use the Developer Tools of your preferred browser to inspect the elements on the page.

Another Example of how to remove the meta box in WordPress.

// REMOVE POST META BOXES
function remove_my_post_metaboxes() {
remove_meta_box( 'authordiv','post','normal' ); // Author Metabox
remove_meta_box( 'commentstatusdiv','post','normal' ); // Comments Status Metabox
remove_meta_box( 'commentsdiv','post','normal' ); // Comments Metabox
remove_meta_box( 'postcustom','post','normal' ); // Custom Fields Metabox
remove_meta_box( 'postexcerpt','post','normal' ); // Excerpt Metabox
remove_meta_box( 'revisionsdiv','post','normal' ); // Revisions Metabox
remove_meta_box( 'slugdiv','post','normal' ); // Slug Metabox
remove_meta_box( 'trackbacksdiv','post','normal' ); // Trackback Metabox
}
add_action('admin_menu','remove_my_post_metaboxes');

Place the following code in your functions file (Appearance > Editor > Theme Functions – functions.php).

Placing that code should remove all the meta boxes below the post editor and give you a nice, clean look.

0

You can try to hook your function with admin_init.

add_action('admin_init', 'remove_page_excerpt_field');

function remove_page_excerpt_field(){
    remove_meta_box( 'pexeto-meta-page-boxes' , 'page' , 'normal' ); 
}

or just remove_action('the_function_used_by_the_theme_to_add_metabox', 10, 1); use the same priority and args number of the add_action.

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.