Title: Function only for admin area?
Last modified: March 6, 2023

---

# Function only for admin area?

 *  [wyclef](https://wordpress.org/support/users/wyclef/)
 * (@wyclef)
 * [3 years, 2 months ago](https://wordpress.org/support/topic/function-only-for-admin-area/)
 * 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 3 years, 2 months ago by [wyclef](https://wordpress.org/support/users/wyclef/).

Viewing 4 replies - 1 through 4 (of 4 total)

 *  Moderator [threadi](https://wordpress.org/support/users/threadi/)
 * (@threadi)
 * [3 years, 2 months ago](https://wordpress.org/support/topic/function-only-for-admin-area/#post-16533717)
 * The [admin_head ](https://developer.wordpress.org/reference/hooks/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:
 *     ```wp-block-code
       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](https://wordpress.org/support/users/wyclef/)
 * (@wyclef)
 * [3 years, 2 months ago](https://wordpress.org/support/topic/function-only-for-admin-area/#post-16536866)
 *     ```wp-block-code
       // 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?
 *  Moderator [threadi](https://wordpress.org/support/users/threadi/)
 * (@threadi)
 * [3 years, 2 months ago](https://wordpress.org/support/topic/function-only-for-admin-area/#post-16537538)
 * None is missing in the code from the last answer. Have you already tested my 
   idea above?
 *  Thread Starter [wyclef](https://wordpress.org/support/users/wyclef/)
 * (@wyclef)
 * [3 years, 2 months ago](https://wordpress.org/support/topic/function-only-for-admin-area/#post-16558950)
 * 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.

 * In: [Developing with WordPress](https://wordpress.org/support/forum/wp-advanced/)
 * 4 replies
 * 2 participants
 * Last reply from: [wyclef](https://wordpress.org/support/users/wyclef/)
 * Last activity: [3 years, 2 months ago](https://wordpress.org/support/topic/function-only-for-admin-area/#post-16558950)
 * Status: not resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
