0

I am trying to remove a metabox ID from a page ID in the admin screen. So I have coded the following:

function ag_remove_cmb_metaboxes() {
    global $post;
    $id = $post->ID;
    if ($id == 21) { ?>
        <script type="text/javascript">
        jQuery(document).ready(function($){
                jQuery('#test_metabox_img') .hide();
        });
        </script>
    <?php }
}
add_action( 'admin_head', 'ag_remove_cmb_metaboxes' );

It is working fine but an error is showing in the widget.php page. Error code is:

Notice: Trying to get property of non-object in E:\xampp\htdocs\speechcare\wp-content\themes\cutedoctor\core\helpers.php on line 56

Can anyone please explain why this error is showing and what I am doing wrong? Thanks in advance.

1 Answer 1

1

While front–end of the site always has a post context (you are either looking at one or archive of posts), admin side doesn't.

So plenty of admin screens won't have $post global set to anything. You should be checking if it's meaningful (for example by if ( ! empty( $post ) )) before trying to access it.

2
  • For the sake of completeness: The OP will still have to put it in-scope with the global keyword first. Not that you weren't aware of that, just that the OP might not be... Sep 1, 2014 at 15:37
  • @JohannesPille code example in question does have it as global, so I didn't dwell on that. :)
    – Rarst
    Sep 1, 2014 at 15:43

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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