0
// MOVE REVISIONS METABOX TO BOTTOM OF EDIT-FORM.PHP
$post_types = get_post_types();
add_action('do_meta_boxes', 'batteryboys_reorder_meta_boxes');
function batteryboys_reorder_meta_boxes(){
    remove_meta_box( 'revisionsdiv', $post_types, 'normal' );
    add_meta_box('revisionsdiv', __('Revisions'), 'post_revisions_meta_box', $post_types, 'normal', 'low');
}

This code works fine, however when you move the $post_types = get_post_types(); inside of the function, it does not work.

Is this because the get_post_types() function has to be global to execute and by containing it inside the function I am restricting it to just inside the function? In other words, it can't get out to query the database, right?

0

As @Milo pointed out, when you use code in OP $post_types is undefined inside the function, so WordPress will act just like it was not there at all.

However, that means you are using an undefined variable, and if turn WP_DEBUG on you'll see a warning, and so you can notice you are doing something wrong.

That's one of the reasons you always should develop with debug turned on.

Regarding your problem, sure run a foreach can be a solution, but is not needed in your case, reason is that 'do_meta_boxes' hook pass screen id (that is post type name) as argument, so you can use it to remove and add your metabox.

add_action('do_meta_boxes', 'batteryboys_reorder_meta_boxes', 1);

function batteryboys_reorder_meta_boxes($screen){
    if ($screen === 'dashboard') return;
    remove_meta_box( 'revisionsdiv', $screen, 'normal' );
    add_meta_box(
      'revisionsdiv', __('Revisions'), 'post_revisions_meta_box', $screen, 'normal', 'low'
    );
}
  • This is what I was looking for. I had originally used the do_meta_boxes hook, however was having a call to undefined meta box display on the dashboard. 1. How did you know to omit the dashboard in the function? 2. When using $screen as a parameter in the function, what is telling the function we are on the dashboard screen? How does it know what screen we are on? – lime Dec 20 '14 at 20:22
  • 1) I knew that 'do_meta_boxes' is fired on dashboard because I know that the Dashboard Widget API use it. 2) The $screen argument is passed to function using do_action you can see it in source code. – gmazzap Dec 20 '14 at 22:12
  • The code works great, however I don't understand what is happening. When I look at the function reference and the hook reference, it says it requires 3 parameters. You used only 1. Why does it work? – lime Dec 30 '14 at 2:53
2

$post_types is undefined inside your function, add_meta_box checks if the value is null and defaults to the current post type when the action runs. That's why it appears to work when $post_types = get_post_types(); is outside, when in reality it's doing nothing.

It doesn't work when you add $post_types = get_post_types(); inside the function, because that function returns an array and that argument for add_meta_box must be a string. It's no longer null, but it's invalid, so it fails.

To make it work you need to loop over the array of post types and call add_meta_box for each:

function batteryboys_reorder_meta_boxes(){
    $post_types = get_post_types();
    foreach( $post_types as $post_type ){
        remove_meta_box( 'revisionsdiv', $post_type, 'normal' );
        add_meta_box('revisionsdiv', __('Revisions'), 'post_revisions_meta_box', $post_type, 'normal', 'low');
    }
}

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.