Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

Morning!

I have a call in my functions.php file to include a custom meta-box php file in my theme's directory called: /metaboxes/home-meta.php

The code is as follows for the call:

    add_action( 'add_meta_boxes_page','load_home_meta' );
    function load_home_meta() {
        $post_id = $_GET['post'] ? $_GET['post'] : $_POST['post_ID'] ;
        if($post_id == '104'){
            include( get_template_directory() . '/metaboxes/home-meta.php' );
        }
    }

The include is now working (had an add_meta_boxes function wrapped around my metaboxes within the home-meta.php file below (updated code)).

However, I'm getting errors in each metabox siting the first case within the switch for the callback functions. An example error is:

Warning: Invalid argument supplied for foreach() in /Users/[my_mac]/Sites/[site_dir]/wp-content/themes/[site_theme]/metaboxes/home-meta.php on line 224

The home-meta.php code is:

    <?php

    /**
     * Home Page Custom Meta Content
     *
    **/

add_meta_box(  
    'home_meta_box', // $id  
    'Home Page Content', // $title   
    'show_home_meta_box', // $callback  
    'page', // $page  
    'normal', // $context  
    'high'); // $priority
add_meta_box(  
    'home_meta_box_lower_1', // $id  
    'Home Lower Left', // $title   
    'show_home_meta_box_lower_left', // $callback  
    'page', // $page  
    'normal', // $context  
    'high'); // $priority    
add_meta_box(  
    'home_meta_box_lower_2', // $id  
    'Home Lower Center', // $title   
    'show_home_meta_box_lower_center', // $callback  
    'page', // $page  
    'normal', // $context  
    'high'); // $priority 
add_meta_box(  
    'home_meta_box_lower_3', // $id  
    'Home Lower Right', // $title   
    'show_home_meta_box_lower_right', // $callback  
    'page', // $page  
    'normal', // $context  
    'high'); // $priority 


    // Creating Array for Fields
    $prefix = 'home_';
    $home_meta_fields = array(
        array(
            'label' => 'Caption Title',
            'desc' => 'Upper section H2 caption title.',
            'id' => $prefix.'title',
            'type' => 'text'
        ),
        array(
            'label' => 'Caption Sub Title',
            'desc' => 'Upper section H3 caption title.',
            'id' => $prefix.'sub_title',
            'type' => 'text'
        ),
      array(  
          'label'=> 'Caption',  
          'desc'  => 'Caption text block.',  
          'id'    => $prefix.'caption',  
          'type'  => 'textarea'  
      ),
        array(  
            'label'  => 'Caption Image',  
            'desc'  => 'Upload a pre-cropped 1140px wide x 530px tall web-optimized image.',  
            'id'    => $prefix.'image',  
            'type'  => 'image'  
        )
    );// end caption array

    $prefix2 = 'home_lower_left_';
    $home_meta_fields_lower_left = array(
        array(
            'label' => 'Column Title',
            'desc' => 'H2 title for column.',
            'id' => $prefix2.'title',
            'type' => 'text'
        ),
        array(  
            'label'  => 'Column Image',  
            'desc'  => 'Upload a pre-cropped 360px wide x 300px tall web-optimized image.',  
            'id'    => $prefix2.'image',  
            'type'  => 'image'  
        ),
        array(
            'label' => 'Column Sub-Title',
            'desc' => 'H3 sub-title above text block.',
            'id' => $prefix2.'sub_title',
            'type' => 'text'
        ),
      array(  
          'label'=> 'Text block',  
          'desc'  => 'Caption text block.',  
          'id'    => $prefix2.'caption',  
          'type'  => 'textarea'  
      ),
        array(
            'label' => 'Button Label',
            'desc' => 'Button link label (what user reads on button).',
            'id' => $prefix2.'btn_label',
            'type' => 'text'
        ),
        array(
            'label' => 'Button Title Tag',
            'desc' => 'On hover and SEO text.',
            'id' => $prefix2.'btn_title_tag',
            'type' => 'text'
        ),
        array(
            'label' => 'Button URL',
            'desc' => 'URL where button links to. Enter http:// to work.',
            'id' => $prefix2.'btn_url',
            'type' => 'text'
        )
    );// end lower left array

    $prefix3 = 'home_lower_center_';
    $home_meta_fields_lower_center = array(
        array(
            'label' => 'Column Title',
            'desc' => 'H2 title for column.',
            'id' => $prefix3.'title',
            'type' => 'text'
        ),
        array(  
            'label'  => 'Column Image',  
            'desc'  => 'Upload a pre-cropped 360px wide x 300px tall web-optimized image.',  
            'id'    => $prefix3.'image',  
            'type'  => 'image'  
        ),
        array(
            'label' => 'Column Sub-Title',
            'desc' => 'H3 sub-title above text block.',
            'id' => $prefix3.'sub_title',
            'type' => 'text'
        ),
      array(  
          'label'=> 'Text block',  
          'desc'  => 'Caption text block.',  
          'id'    => $prefix3.'caption',  
          'type'  => 'textarea'  
      ),
        array(
            'label' => 'Button Label',
            'desc' => 'Button link label (what user reads on button).',
            'id' => $prefix3.'btn_label',
            'type' => 'text'
        ),
        array(
            'label' => 'Button Title Tag',
            'desc' => 'On hover and SEO text.',
            'id' => $prefix3.'btn_title_tag',
            'type' => 'text'
        ),
        array(
            'label' => 'Button URL',
            'desc' => 'URL where button links to. Enter http:// to work.',
            'id' => $prefix3.'btn_url',
            'type' => 'text'
        )
    );// end lower center array

    $prefix4 = 'home_lower_right_';
    $home_meta_fields_lower_right = array(
        array(
            'label' => 'Column Title',
            'desc' => 'H2 title for column.',
            'id' => $prefix4.'title',
            'type' => 'text'
        ),
        array(  
            'label'  => 'Column Image',  
            'desc'  => 'Upload a pre-cropped 360px wide x 300px tall web-optimized image.',  
            'id'    => $prefix4.'image',  
            'type'  => 'image'  
        ),
        array(
            'label' => 'Column Sub-Title',
            'desc' => 'H3 sub-title above text block.',
            'id' => $prefix4.'sub_title',
            'type' => 'text'
        ),
      array(  
          'label'=> 'Text block',  
          'desc'  => 'Caption text block.',  
          'id'    => $prefix4.'caption',  
          'type'  => 'textarea'  
      ),
        array(
            'label' => 'Button Label',
            'desc' => 'Button link label (what user reads on button).',
            'id' => $prefix4.'btn_label',
            'type' => 'text'
        ),
        array(
            'label' => 'Button Title Tag',
            'desc' => 'On hover and SEO text.',
            'id' => $prefix4.'btn_title_tag',
            'type' => 'text'
        ),
        array(
            'label' => 'Button URL',
            'desc' => 'URL where button links to. Enter http:// to work.',
            'id' => $prefix4.'btn_url',
            'type' => 'text'
        )
    );// end lower right array


    //The Callback
    function show_home_meta_box() {
    global $home_meta_fields, $post;
    // Using nonce for verification
    echo '<input type="hidden" name="home_meta_box_nonce" value="'.wp_create_nonce(basename(__FILE__)).'" />';

        //Begin field table and loop
        echo '<table class="form-table">';
        foreach ($home_meta_fields as $field) {
            // get value of this field if it exists for this page
            $meta = get_post_meta($post->ID, $field['id'], true);
            // begin a table row with
            echo '<tr>
                            <th><label for="'.$field['id'].'">'.$field['label'].'</label></th>
                            <td>';
                            switch($field['type']) {
                                // case items will go here
                                // text  
                                case 'text':  
                                    echo '<input type="text" name="'.$field['id'].'" id="'.$field['id'].'" value="'.$meta.'" size="30" /> 
                                        <br /><span class="description">'.$field['desc'].'</span>';  
                                break;
                                // textarea  
                                case 'textarea':  
                                    echo '<textarea name="'.$field['id'].'" id="'.$field['id'].'" cols="60" rows="4">'.$meta.'</textarea> 
                                        <br /><span class="description">'.$field['desc'].'</span>';  
                                break;
                                case 'image':  
                                    $image = get_template_directory_uri().'/library/images/img-preview-blank.png';    
                                    echo '<span class="custom_default_image" style="display:none">'.$image.'</span>';  
                                    if ($meta) { $image = wp_get_attachment_image_src($meta, 'medium'); $image = $image[0]; }                 
                                    echo    '<input name="'.$field['id'].'" type="hidden" class="custom_upload_image" value="'.$meta.'" /> 
                                                <img src="'.$image.'" class="custom_preview_image" style="max-width:300px" alt="" /><br /> 
                                                    <input class="custom_upload_image_button button" type="button" value="Choose Image" /> 
                                                    <small> <a href="#" class="custom_clear_image_button">Remove Image</a></small> 
                                                    <br clear="all" /><span class="description">'.$field['desc'].'';  
                                break;  
                            } // end switch
            echo '</td></tr>';
        } // end foreach
        echo '</table>'; // end table
    }

    // Save the Data
    function save_home_meta($post_id) {  
        global $home_meta_fields;  

        // verify nonce  
        if (!wp_verify_nonce($_POST['home_meta_box_nonce'], basename(__FILE__)))   
            return $post_id;  
        // check autosave  
        if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE)  
            return $post_id;  
        // check permissions  
        if ('page' == $_POST['post_type']) {  
            if (!current_user_can('edit_page', $post_id))  
                return $post_id;  
            } elseif (!current_user_can('edit_post', $post_id)) {  
                return $post_id;  
        }  

        // loop through fields and save the data  
        foreach ($home_meta_fields as $field) {  
            $old = get_post_meta($post_id, $field['id'], true);  
            $new = $_POST[$field['id']];  
            if ($new && $new != $old) {  
                update_post_meta($post_id, $field['id'], $new);  
            } elseif ('' == $new && $old) {  
                delete_post_meta($post_id, $field['id'], $old);  
            }  
        } // end foreach  
    }  
    add_action('save_post', 'save_home_meta');
    // end Upper Home Content

    // Lower Left
    //The Callback
    function show_home_meta_box_lower_left() {
    global $home_meta_fields_lower_left, $post;
    // Using nonce for verification
    echo '<input type="hidden" name="home_meta_box_left_nonce" value="'.wp_create_nonce(basename(__FILE__)).'" />';

        //Begin field table and loop
        echo '<table class="form-table">';
        foreach ($home_meta_fields_lower_left as $field) {
            // get value of this field if it exists for this page
            $meta = get_post_meta($post->ID, $field['id'], true);
            // begin a table row with
            echo '<tr>
                            <th><label for="'.$field['id'].'">'.$field['label'].'</label></th>
                            <td>';
                            switch($field['type']) {
                                // case items will go here
                                // text  
                                case 'text':  
                                    echo '<input type="text" name="'.$field['id'].'" id="'.$field['id'].'" value="'.$meta.'" size="30" /> 
                                        <br /><span class="description">'.$field['desc'].'</span>';  
                                break;
                                // textarea  
                                case 'textarea':  
                                    echo '<textarea name="'.$field['id'].'" id="'.$field['id'].'" cols="60" rows="4">'.$meta.'</textarea> 
                                        <br /><span class="description">'.$field['desc'].'</span>';  
                                break;
                                case 'image':  
                                    $image = get_template_directory_uri().'/library/images/img-preview-blank.png';    
                                    echo '<span class="custom_default_image" style="display:none">'.$image.'</span>';  
                                    if ($meta) { $image = wp_get_attachment_image_src($meta, 'medium'); $image = $image[0]; }                 
                                    echo    '<input name="'.$field['id'].'" type="hidden" class="custom_upload_image" value="'.$meta.'" /> 
                                                <img src="'.$image.'" class="custom_preview_image" style="max-width:300px" alt="" /><br /> 
                                                    <input class="custom_upload_image_button button" type="button" value="Choose Image" /> 
                                                    <small> <a href="#" class="custom_clear_image_button">Remove Image</a></small> 
                                                    <br clear="all" /><span class="description">'.$field['desc'].'';  
                                break;  
                            } // end switch
            echo '</td></tr>';
        } // end foreach
        echo '</table>'; // end table
    }

    // Save the Data
    function save_home_meta_lower_left($post_id) {  
        global $home_meta_fields_lower_left;  

        // verify nonce  
        if (!wp_verify_nonce($_POST['home_meta_box_left_nonce'], basename(__FILE__)))   
            return $post_id;  
        // check autosave  
        if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE)  
            return $post_id;  
        // check permissions  
        if ('page' == $_POST['post_type']) {  
            if (!current_user_can('edit_page', $post_id))  
                return $post_id;  
            } elseif (!current_user_can('edit_post', $post_id)) {  
                return $post_id;  
        }  

        // loop through fields and save the data  
        foreach ($home_meta_fields_lower_left as $field) {  
            $old = get_post_meta($post_id, $field['id'], true);  
            $new = $_POST[$field['id']];  
            if ($new && $new != $old) {  
                update_post_meta($post_id, $field['id'], $new);  
            } elseif ('' == $new && $old) {  
                delete_post_meta($post_id, $field['id'], $old);  
            }  
        } // end foreach  
    }  
    add_action('save_post', 'save_home_meta_lower_left');
    // end lower left

    // Lower Center Column
    //The Callback
    function show_home_meta_box_lower_center() {
    global $home_meta_fields_lower_center, $post;
    // Using nonce for verification
    echo '<input type="hidden" name="home_meta_box_center_nonce" value="'.wp_create_nonce(basename(__FILE__)).'" />';

        //Begin field table and loop
        echo '<table class="form-table">';
        foreach ($home_meta_fields_lower_center as $field) {
            // get value of this field if it exists for this page
            $meta = get_post_meta($post->ID, $field['id'], true);
            // begin a table row with
            echo '<tr>
                            <th><label for="'.$field['id'].'">'.$field['label'].'</label></th>
                            <td>';
                            switch($field['type']) {
                                // case items will go here
                                // text  
                                case 'text':  
                                    echo '<input type="text" name="'.$field['id'].'" id="'.$field['id'].'" value="'.$meta.'" size="30" /> 
                                        <br /><span class="description">'.$field['desc'].'</span>';  
                                break;
                                // textarea  
                                case 'textarea':  
                                    echo '<textarea name="'.$field['id'].'" id="'.$field['id'].'" cols="60" rows="4">'.$meta.'</textarea> 
                                        <br /><span class="description">'.$field['desc'].'</span>';  
                                break;
                                case 'image':  
                                    $image = get_template_directory_uri().'/library/images/img-preview-blank.png';    
                                    echo '<span class="custom_default_image" style="display:none">'.$image.'</span>';  
                                    if ($meta) { $image = wp_get_attachment_image_src($meta, 'medium'); $image = $image[0]; }                 
                                    echo    '<input name="'.$field['id'].'" type="hidden" class="custom_upload_image" value="'.$meta.'" /> 
                                                <img src="'.$image.'" class="custom_preview_image" style="max-width:300px" alt="" /><br /> 
                                                    <input class="custom_upload_image_button button" type="button" value="Choose Image" /> 
                                                    <small> <a href="#" class="custom_clear_image_button">Remove Image</a></small> 
                                                    <br clear="all" /><span class="description">'.$field['desc'].'';  
                                break;  
                            } // end switch
            echo '</td></tr>';
        } // end foreach
        echo '</table>'; // end table
    }

    // Save the Data
    function save_home_meta_lower_center($post_id) {  
        global $home_meta_fields_lower_center;  

        // verify nonce  
        if (!wp_verify_nonce($_POST['home_meta_box_center_nonce'], basename(__FILE__)))   
            return $post_id;  
        // check autosave  
        if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE)  
            return $post_id;  
        // check permissions  
        if ('page' == $_POST['post_type']) {  
            if (!current_user_can('edit_page', $post_id))  
                return $post_id;  
            } elseif (!current_user_can('edit_post', $post_id)) {  
                return $post_id;  
        }  

        // loop through fields and save the data  
        foreach ($home_meta_fields_lower_center as $field) {  
            $old = get_post_meta($post_id, $field['id'], true);  
            $new = $_POST[$field['id']];  
            if ($new && $new != $old) {  
                update_post_meta($post_id, $field['id'], $new);  
            } elseif ('' == $new && $old) {  
                delete_post_meta($post_id, $field['id'], $old);  
            }  
        } // end foreach  
    }  
    add_action('save_post', 'save_home_meta_lower_center');
    // end lower center

    // Lower Right Column
    //The Callback
    function show_home_meta_box_lower_right() {
    global $home_meta_fields_lower_right, $post;
    // Using nonce for verification
    echo '<input type="hidden" name="home_meta_box_right_nonce" value="'.wp_create_nonce(basename(__FILE__)).'" />';

        //Begin field table and loop
        echo '<table class="form-table">';
        foreach ($home_meta_fields_lower_right as $field) {
            // get value of this field if it exists for this page
            $meta = get_post_meta($post->ID, $field['id'], true);
            // begin a table row with
            echo '<tr>
                            <th><label for="'.$field['id'].'">'.$field['label'].'</label></th>
                            <td>';
                            switch($field['type']) {
                                // case items will go here
                                // text  
                                case 'text':  
                                    echo '<input type="text" name="'.$field['id'].'" id="'.$field['id'].'" value="'.$meta.'" size="30" /> 
                                        <br /><span class="description">'.$field['desc'].'</span>';  
                                break;
                                // textarea  
                                case 'textarea':  
                                    echo '<textarea name="'.$field['id'].'" id="'.$field['id'].'" cols="60" rows="4">'.$meta.'</textarea> 
                                        <br /><span class="description">'.$field['desc'].'</span>';  
                                break;
                                case 'image':  
                                    $image = get_template_directory_uri().'/library/images/img-preview-blank.png';    
                                    echo '<span class="custom_default_image" style="display:none">'.$image.'</span>';  
                                    if ($meta) { $image = wp_get_attachment_image_src($meta, 'medium'); $image = $image[0]; }                 
                                    echo    '<input name="'.$field['id'].'" type="hidden" class="custom_upload_image" value="'.$meta.'" /> 
                                                <img src="'.$image.'" class="custom_preview_image" style="max-width:300px" alt="" /><br /> 
                                                    <input class="custom_upload_image_button button" type="button" value="Choose Image" /> 
                                                    <small> <a href="#" class="custom_clear_image_button">Remove Image</a></small> 
                                                    <br clear="all" /><span class="description">'.$field['desc'].'';  
                                break;  
                            } // end switch
            echo '</td></tr>';
        } // end foreach
        echo '</table>'; // end table
    }

    // Save the Data
    function save_home_meta_lower_right($post_id) {  
        global $home_meta_fields_lower_right;  

        // verify nonce  
        if (!wp_verify_nonce($_POST['home_meta_box_right_nonce'], basename(__FILE__)))   
            return $post_id;  
        // check autosave  
        if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE)  
            return $post_id;  
        // check permissions  
        if ('page' == $_POST['post_type']) {  
            if (!current_user_can('edit_page', $post_id))  
                return $post_id;  
            } elseif (!current_user_can('edit_post', $post_id)) {  
                return $post_id;  
        }  

        // loop through fields and save the data  
        foreach ($home_meta_fields_lower_right as $field) {  
            $old = get_post_meta($post_id, $field['id'], true);  
            $new = $_POST[$field['id']];  
            if ($new && $new != $old) {  
                update_post_meta($post_id, $field['id'], $new);  
            } elseif ('' == $new && $old) {  
                delete_post_meta($post_id, $field['id'], $old);  
            }  
        } // end foreach  
    }  
    add_action('save_post', 'save_home_meta_lower_right');
    // end lower right

    ?>
share|improve this question
    
Are you trying to include a meta box on the front end of the site? –  s_ha_dum Feb 21 '13 at 16:16
    
No, these are edit-post screen metaboxes. –  Chip Bennett Feb 21 '13 at 16:18
    
as @ChipBennett said, I'm trying to have the metaboxes show up in the admin area for editing a post/page. It seems that I'm not including the file properly, not sure where it's hanging. –  Rick Pickett Feb 21 '13 at 16:20
    
So, the first step, before digging into all your metabox code: inside home-meta.php, can you create a very simple add_meta_box() call, and in the callback, just output some HELLO WORLD text? –  Chip Bennett Feb 21 '13 at 16:20
    
It seems that I'm not including the file properly, not sure where it's hanging - Actually, per your previous question, you've verified that you're including the file properly. The problem is now with the functional code itself. –  Chip Bennett Feb 21 '13 at 16:23
show 3 more comments

1 Answer

up vote 0 down vote accepted

Problem #1:

First problem: inside home-meta.php, pull all of your add_meta_box() calls outside of the add_meta_boxes_home() callback, and get rid of the callback and add_action() call. You handle this already, with the add_meta_boxes_page callback in functions.php, that includes home-meta.php.

Problem #2:

Warning: Invalid argument supplied for foreach() in /Users/[my_mac]/Sites/[site_dir]/wp-content/themes/[site_theme]/metaboxes/home-meta.php on line 224

Assuming that this is line 224:

foreach ($home_meta_fields as $field) {

I think the problem is that you globalize $home_meta_fields after you define it:

$home_meta_fields = array( ... );

...later followed by:

global $home_meta_fields, $post;
foreach ( $home_meta_fields as $field ) {

Try globalizing $home_meta_fields before you define it:

global $home_meta_fields;
$home_meta_fields = array( ... );

...then it should be recognized as a valid, set array in your foreach() call.

share|improve this answer
    
got that fixed and now the boxes are showing up properly, but still crashing on the switch –  Rick Pickett Feb 21 '13 at 16:33
    
For the second problem, I believe you have a PHP syntax issue regarding when you define $home_meta_fields vs when you globalize $home_meta_fields. See edited answer. –  Chip Bennett Feb 21 '13 at 16:38
    
Thanks @ChipBennet. That solved it! I globalized $home_meta_fields before the defined array and it's showing up properly. Thanks for the help and guidance! –  Rick Pickett Feb 21 '13 at 16:42
    
Unfortunately, now when I click update, any new info entered into the meta boxes isn't being saved. Am I having the same globalize issue with the save functions? Error states: Notice: Undefined index: post in /nfs/c07/h04/mnt/111667/domains/staging.[site].org/html/wp-content/themes/[site_‌​theme]/functions.php on line 329. 329 is $post_id = $_GET['post'] ? $_GET['post'] : $_POST['post_ID'] ; of the hide_editor function. –  Rick Pickett Feb 21 '13 at 20:34
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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