1

I am using Magic Fields 2 for custom meta boxes and custom fields, but this situation pertains to any meta box.

I have a custom meta box that I want to display on only 3 specific page IDs.

function mf_1_remove_meta_boxes() {

    if( !in_array($_GET['post'], array('194','185','2') ) ):
        remove_meta_box( 'mf_1', 'page', 'normal' );
}

UPDATE

Using Bainternet's solution, the code works. That being said, a new issue became apparent. When a new page is created, the meta box is visible immediately, then disappears once the page is saved, since the ID does not match an ID that has been specified.

UPDATE 2

The code above has been modified to correct the issue with the meta box appearing on a new (unsaved) page. It has also been modified to a complete and working code. The meta box will be removed for all users.

To remove the meta box for everyone except admins:

function mf_1_remove_meta_boxes() {

    if( !is_admin())
        return;

    if( !in_array($_GET['post'], array('194','185','2') ) ):
        remove_meta_box( 'mf_1', 'page', 'normal' );
}
0
2

A simple and cleaner solution would be to use !in_array ex:

function mf_1_remove_meta_boxes() {

    if( !is_admin() && !isset( $_GET['post'] ) )
        return;

    if( !in_array($_GET['post'], array('194','185','2') ) )
        remove_meta_box( 'mf_1', 'page', 'normal' );
}

this way you can just add the ids in the array and as many as you want

5
  • Thanks! The code needs a colon - if( !in_array($_GET['post'], array('194','185','2') ) ): otherwise a 500 error is returned. SE won't let me edit a single character. Other than that, works great. May 18 '13 at 21:50
  • A new issue became apparent after using this code. I have added an update to the question. May 18 '13 at 22:04
  • The colon is needed only if you are using the keyword endif.
    – Bainternet
    May 19 '13 at 6:06
  • using the exact code i posted will never give you 500.
    – Bainternet
    May 19 '13 at 22:37
  • 1
    Hmmm... It did yesterday, but I tested it again and it didn't. I must have missed something before. Thanks, again. May 20 '13 at 3:08
1

Try:

if($_GET['post'] != 194 || $_GET['post'] != 185 || $_GET['post'] != 2)

It should work like this.

2
  • Thanks! The code needs a colon - if($_GET['post'] != 194 || $_GET['post'] != 185 || $_GET['post'] != 2): otherwise a 500 error is returned. SE won't let me edit a single character. Other than that, works great. May 18 '13 at 21:50
  • Either a colon (:) or a curly brace ({).
    – Ciprian
    May 20 '13 at 20:59

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.