• Hi, I wanted to run the following only in the admin area only.

    // Hide ACF Content Box
    function hide_post_box() {
    global $post;
    if ( 34 == $post->ID ) {
    echo '';
    } elseif ( 31 == $post->ID ) {
    echo '';
    }
    add_action('admin_head', 'hide_post_box');

    How would I adjust this to do that, currently it fires on the front end as well and the logs are showing a PHP Warning: Attempt to read property “ID” on null.

    Something like this?

    if ( is_admin() ) {
    function hide_post_box() {
    global $post;
    if ( 34 == $post->ID ) {
    echo '';
    } elseif ( 31 == $post->ID ) {
    echo '';
    }
    }
    add_action('admin_head', 'hide_post_box');
    }

    • This topic was modified 1 year, 8 months ago by wyclef.
Viewing 4 replies - 1 through 4 (of 4 total)
  • The admin_head hook is only fired on admin pages. If this also happens in the frontend, then there is a bug somewhere in your code – probably a part you didn’t show. Have a look at what you are executing in the frontend.

    By the way, your code is missing a “}” – it cannot work at all.

    Of course you can also use is_admin() for this. But if then like this:

    function hide_post_box() {
      if( !is_admin() ) { return; }
      global $post;
      if ( 34 == $post->ID ) {
        echo '';
      } elseif ( 31 == $post->ID ) {
        echo '';
      }
    }
    add_action('admin_head', 'hide_post_box');

    I don’t see what this function should do in terms of content. Output nothing when certain IDs are called?

    Thread Starter wyclef

    (@wyclef)

    // Hide ACF Content Box
    function hide_post_box() {
        global $post;
        if ( 34 == $post->ID ) {
             echo '<style> .acf-field-51df1eda4b2ac { display:none; } </style>';
        } elseif ( 31 == $post->ID ) {
             echo '<style> .acf-field-51df1eda4b2ac { display:none; } </style>';
        }
    }
    add_action('admin_head', 'hide_post_box');

    Sorry, it looks like some code was stripped out when I pasted before. Where am I missing a bracket?

    None is missing in the code from the last answer. Have you already tested my idea above?

    Thread Starter wyclef

    (@wyclef)

    Yes, seems to work. Thanks!

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘Function only for admin area?’ is closed to new replies.