Title: Conditionals in functions.php
Last modified: August 19, 2016

---

# Conditionals in functions.php

 *  Resolved [slurve](https://wordpress.org/support/users/slurve/)
 * (@slurve)
 * [15 years, 6 months ago](https://wordpress.org/support/topic/conditionals-in-functionsphp/)
 * Anyone know how to get conditionals (like is_page_template) working in functions.
   php? I suppose this has something to do with the loop … and that WP doesn’t know
   the ID of the page? The first conditional below (post_id == ’24’) is working 
   fine. The second one (is_page_template) is not. Any help would be greatly appreciated.
   Thanks.
 * Tom
 *     ```
       function admin_init(){
           $post_id = $_GET['post'] ? $_GET['post'] : $_POST['post_ID'];
           if ($post_id == '24') add_meta_box("lead_text", "Lead Text", "lead_text", "page", "normal", "high");
           if (is_page_template('page_staff_bio.php')) add_meta_box("bio_excerpt", "Bio Excerpt", "bio_excerpt", "page", "normal", "high");
         }
       ```
   

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

 *  Thread Starter [slurve](https://wordpress.org/support/users/slurve/)
 * (@slurve)
 * [14 years, 9 months ago](https://wordpress.org/support/topic/conditionals-in-functionsphp/#post-1763717)
 * Anyone have an answer for this?
 *  [esmi](https://wordpress.org/support/users/esmi/)
 * (@esmi)
 * [14 years, 9 months ago](https://wordpress.org/support/topic/conditionals-in-functionsphp/#post-1763718)
 * Try:
 *     ```
       function admin_init(){
       	global $post;
           if ($post->ID == '24') add_meta_box("lead_text", "Lead Text", "lead_text", "page", "normal", "high");
           if (is_page_template('page_staff_bio.php')) add_meta_box("bio_excerpt", "Bio Excerpt", "bio_excerpt", "page", "normal", "high");
        }
       ```
   
 *  Thread Starter [slurve](https://wordpress.org/support/users/slurve/)
 * (@slurve)
 * [14 years, 9 months ago](https://wordpress.org/support/topic/conditionals-in-functionsphp/#post-1763719)
 * Thanks for the response, esmi. Doesn’t seem to work, though.
 *  [Chip Bennett](https://wordpress.org/support/users/chipbennett/)
 * (@chipbennett)
 * [14 years, 9 months ago](https://wordpress.org/support/topic/conditionals-in-functionsphp/#post-1763720)
 * **First**, don’t name a function `admin_init()`.
 * **Second**, how is this function being called? Are you calling it explicitly 
   in a template file somewhere, or are you hooking it into a filter?
 * **Third**, what are you trying to _accomplish_ with this function?
 * I assume that you’re trying to add a metabox to the post-edit screen for a specific
   post?
 *  Thread Starter [slurve](https://wordpress.org/support/users/slurve/)
 * (@slurve)
 * [14 years, 9 months ago](https://wordpress.org/support/topic/conditionals-in-functionsphp/#post-1763721)
 * Chip, I’m hooking into admin_init. (Good call, I’ll change the function name.)
   Basically, I’m trying to add the bio_excerpt meta box to all pages using the 
   page_staff_bio.php template.
 *  [Chip Bennett](https://wordpress.org/support/users/chipbennett/)
 * (@chipbennett)
 * [14 years, 9 months ago](https://wordpress.org/support/topic/conditionals-in-functionsphp/#post-1763722)
 * Okay then, first things, first, here’s how you properly hook into `admin_init`:
 *     ```
       function mytheme_add_bio_metabox() {
           // function code goes here
       }
       add_action( 'admin_init', 'mytheme_add_bio_metabox' );
       ```
   
 * But actually, you don’t want to hook that function into `admin_init`; you can
   specify a _much more precise_ action: `add_meta_boxes_{post-type}`.
 * The post-type in this case is “page”, so you should use `add_meta_boxes_page`:
 *     ```
       function mytheme_add_bio_metabox() {
           // function code goes here
       }
       add_action( 'add_meta_boxes_page', 'mytheme_add_bio_metabox' );
       ```
   
 *  [Chip Bennett](https://wordpress.org/support/users/chipbennett/)
 * (@chipbennett)
 * [14 years, 9 months ago](https://wordpress.org/support/topic/conditionals-in-functionsphp/#post-1763723)
 * Next, we need to build out that function, to add the meta box. We’re going to
   pass the `$post` global variable to the function, as we’ll need it. We will determine
   if the current Page uses the appropriate template (as defined in the Post metadata),
   or if the Page has the appropriate ID. If so, we add our meta box:
 *     ```
       function mytheme_add_bio_metabox( $post ) {
           $template = get_post_meta( $post->ID, '_wp_page_template' ,true );
           if ( 'page_staff_bio.php' == $template || '24' == $post->ID ) {
               global $wp_meta_boxes;
               add_meta_box( 'bio_excerpt', 'Bio Excerpt', 'bio_excerpt', 'page', 'normal', 'high' );
           }
       }
       add_action( 'add_meta_boxes_page', 'mytheme_add_bio_metabox' );
       ```
   
 *  Thread Starter [slurve](https://wordpress.org/support/users/slurve/)
 * (@slurve)
 * [14 years, 9 months ago](https://wordpress.org/support/topic/conditionals-in-functionsphp/#post-1763724)
 * Good to know about the `add_meta_boxes_{post-type}` action. Thanks. Is there 
   a way to add the meta box _only_ to pages using a certain template? Would I be
   able to use `is_page_template()` within `mytheme_add_bio_metabox()`?
 *  [Chip Bennett](https://wordpress.org/support/users/chipbennett/)
 * (@chipbennett)
 * [14 years, 9 months ago](https://wordpress.org/support/topic/conditionals-in-functionsphp/#post-1763725)
 * > Is there a way to add the meta box only to pages using a certain template?
 * (See my follow-up reply. 🙂 )
 *  Thread Starter [slurve](https://wordpress.org/support/users/slurve/)
 * (@slurve)
 * [14 years, 9 months ago](https://wordpress.org/support/topic/conditionals-in-functionsphp/#post-1763726)
 * Doh. Perhaps I need an afternoon cup of coffee. 🙂 Thanks for the help, Chip.
 *  [Chip Bennett](https://wordpress.org/support/users/chipbennett/)
 * (@chipbennett)
 * [14 years, 9 months ago](https://wordpress.org/support/topic/conditionals-in-functionsphp/#post-1763727)
 * Good idea! I think I’ll do the same. Let me know if the conditionals work as 
   intended!
 *  Thread Starter [slurve](https://wordpress.org/support/users/slurve/)
 * (@slurve)
 * [14 years, 9 months ago](https://wordpress.org/support/topic/conditionals-in-functionsphp/#post-1763728)
 * Worked like a champ. (Coffee and the code.) Thanks again.

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

The topic ‘Conditionals in functions.php’ is closed to new replies.

 * 12 replies
 * 3 participants
 * Last reply from: [slurve](https://wordpress.org/support/users/slurve/)
 * Last activity: [14 years, 9 months ago](https://wordpress.org/support/topic/conditionals-in-functionsphp/#post-1763728)
 * Status: resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
