Issue with custom meta boxes.

3 replies
I'm having a wierd issue with my custom meta boxes. They appear correctly on posts and on custom page templates. But, on the default page template they will load when you create a new page. After you hit publish or save the meta boxes dissappear.

This is the Meta Class

PHP Code:

class ARIXWP_Meta {
    
    private 
$meta     null;
    public 
$postid    null;
    private 
$template null;
    
    function 
__construct() {
        
add_action('save_post',array(&$this'save_meta_data'));
    }
    
    function 
__destruct() {
        
$this->meta     null;
        
$this->postid   null;
        
$this->template null;
    }
    
    public function 
init_meta() {
        
add_action('admin_init',array(&$this'add_meta'));
    }
    
    public function 
add_meta() {
        if (
$this->meta['template'] == $this->template) {
            
add_meta_box($this->meta['id'], $this->meta['title'],  array(&$this'format_meta'), $this->meta['post_type'],  $this->meta['context'], $this->meta['priority']);    
        }
    }
    
    public function 
format_meta() {
        global 
$post;
        
        if (!isset(
$post)) return false;
        
        if (
$this->meta['post_type'] != $post->post_type) return false;
        
        
$this->set_postid($post->ID);
        
        
// Use nonce for verification
        
echo '<input type="hidden" name="' .  $this->meta['nonce_name'] . '" value="',  wp_create_nonce(basename(__FILE__)), '" />';
        
        foreach (
$this->meta['fields'] as $field) {
            
// get current post meta data
            
$meta get_post_meta($post->ID$field['id'], true);
       
            if (isset(
$field['before']) && $field['before'] != "") {
              echo 
'<p class="heading">' $field['before'] . '</p>';
            }
            
            echo 
'<label for="'$field['id'] .'" class="arixwp_label">'$field['name']. '</label><p>';
            
            if (isset(
$field['desc']) && $field['desc'] != "") {
              echo 
'<p class="arixwp_text">' $field['desc'] . '</p>';
            }
            
            if ( 
$field['type'] == 'radio' )
                echo 
'<div class="arixwp_radio">';
            
            switch (
$field['type']) {
                case 
'text':
                    echo 
'<input type="text" name="'$field['id'].  '" id="'$field['id'] .'" value="'. ($meta $meta $field['default'])  . '" class="arixwp_admin_input" />''<br />';
                    break;
                case 
'textarea':
                    echo 
'<textarea name="'$field['id']. '" id="'.  $field['id']. '" cols="60" rows="4" class="arixwp_admin_input">'.  ($meta $meta $field['default']) . '</textarea>''<br  />';
                    break;
                case 
'select':
                    echo 
'<select name="'$field['id'] . '" id="'$field['id'] . '" class="arixwp_admin_select">';
                    foreach (
$field['options'] as $option) {
                        echo 
'<option '. ( $meta == $option '  selected="selected"' '' ) . '>'$option '</option>';
                    }
                    echo 
'</select>';
                    break;
                case 
'radio':
                    foreach (
$field['options'] as $option) {
                       
// echo '<input type="radio" name="' .  $field['id'] . '" value="' . $option['value'] . '"' . ( $meta ==  $option['value'] ? ' checked="checked"' : '' ) . ' />' .  $option['name'] . '<br />';
                        
echo '<input type="radio" id="' $field[  'id' ] . $option['value'] . '" name="' $field'id' ] . '" value="' .  $option['value'] . '"' . ( $meta == $option'value' ] ? '  checked="checked"' '' ) . ' /><label for="' $field'id' ] .  $option['value'] . '">' $option'name' ] . '</label>'
                    }
                    break;
                case 
'checkbox':
                    echo 
'<input type="checkbox" name="' .  $field['id'] . '" id="' $field['id'] . '"' . ( $meta '  checked="checked"' '' ) . ' />';
                    break;
            }
            
            if ( 
$field['type'] == 'radio' )
                echo 
'</div>';
                
           
// echo     '</p>';
        
}
    }
    
    public function 
save_meta_data($post_id) {
        global 
$post;
        
        if (!isset(
$post->ID)) {
            
$post get_post($post_id);
        }
        
        
$this->set_postid($post->ID);
        
$this->meta $this->get_meta();
        
        if (
$this->meta['post_type'] != $post->post_type) return $post_id;
                
        
$_POST[$this->meta['nonce_name']] =  isset($_POST[$this->meta['nonce_name']]) ?  $_POST[$this->meta['nonce_name']] : "";
        
        
//Verify nonce
        
if (!wp_verify_nonce($_POST[$this->meta['nonce_name']], basename(__FILE__))) {
            return 
$post_id;
        }
                
        
//Check autosave
        
if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE) {
            return 
$post_id;
        }
     
        
//Check permissions
        
if (isset($_POST['post_type']) && '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;
        }
       
        foreach (
$this->meta['fields'] as $field) {
            
            
$old get_post_meta($post_id$field['id'], true);
            
$new = isset($_POST[$field['id']]) ? $_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);
            }
        }
    }
    
    public function 
set_postid($axPostId 0) {
        
$postID  null;
        
$postID2 null;
        
        if (
$axPostId 0) {
            
$this->postid $axPostId;
        } else {
            
            if ( isset( 
$_GET['post'] ) ) {
                
                
$postID  filter_var$_GET['post'], FILTER_VALIDATE_INT );
            
                if ( isset( 
$_POST['post_ID'] ) ) {
                    
$postID2 filter_var$_POST['post_ID'], FILTER_VALIDATE_INT );
                } else {
                    
$postID2 '';
                }
            
                
$this->postid $postID $postID $postID2 ;        
            }
        }
    }
    
    public function 
get_postid() {
        return 
$this->postid;
    }
    
    public function 
set_template() {
        
$this->template get_post_meta($this->postid,'_wp_page_template',TRUE);
    }
    
    public function 
get_template() {
        return 
$this->template;
    }
    
    public function 
set_meta($axMeta) {
        
update_post_meta($this->postid'_arixwp_field_meta'serialize($axMeta));
        
$this->meta $axMeta;
    }
    
    public function 
get_meta() {
        if (
is_null(($this->meta))) {
            
$this->meta unserialize(get_post_meta($this->postid,'_arixwp_field_meta',TRUE));
        }
        return 
$this->meta;
    }

This is how i am calling the meta boxes:

PHP Code:
    $meta_template '';
    
$meta_posttype 'page';
    
$meta_title    'Page Options';
    
$meta_context  'normal';
    
$meta_priority 'high';
    
$meta_id       'page-meta';
    
$meta_nonce    $meta_id '-nonce';
    
$arixwp_meta       = new ARIXWP_Meta();
    
    
$arixwp_meta->set_postId();
    
$arixwp_meta->set_template();
    
$arixwp_template  $arixwp_meta->get_template();
    
    if (
$arixwp_template == $meta_template) {
        
$arixwp_meta->set_meta(array(
           
            
//This is the id applied to the meta box
            
'id'         => $meta_id,
            
            
//This is the post type the meta is for
            
'post_type'  => $meta_posttype,
            
            
//This is the template to show the meta for
            
'template'   => $meta_template,
            
            
//This is the nonce for this meta
            
'nonce_name' => $meta_nonce,
           
            
//This is the title that appears on the meta box container
            
'title'      => $meta_title,
    
            
//This defines the part of the page where the edit screen section should be shown
            
'context'    => $meta_context,    
           
            
//This sets the priority within the context where the boxes should show
            
'priority'   => $meta_priority,
           
            
//Here we define all the fields we want in the meta box
            
'fields' => array(    
                        
            
// Show or hide featured posts
                        
                
array(
                    
'before'     => '',
                    
'after'     => '',
                    
'name'         => __'Show Featured Posts'THEMENAME ),
                    
'desc'         => __'This allows you to show or  hide the featured posts banner on this page. ( Note: this will override  the setting in the themes control panel. )'THEMENAME ),
                    
'id'        => '_arixwp_featured_show_page',
                    
'type'         => 'radio'// text,textarea,select,radio,checkbox
                    
'default'     => 'home',
                    
'options'     => array(
                        array(
'name' => __' Yes'THEMENAME ), 'value' => 'yes'),
                        array(
'name' => __' No'THEMENAME ), 'value' => 'no')
                    )                                
                ),    
                        
            
// Featured Posts Display Method
                        
                
array(
                    
'before'     => '',
                    
'after'     => '',
                    
'name'         => __'Featured Post Display'THEMENAME ),
                    
'desc'         => __'This controls how your  featured posts are displayed. You can show one main feature and 3 normal  features, one main feature only, three normal features only, or a  jQuery slider that slides through the latest 4 posts in the featured  category. 'THEMENAME ),
                    
'id'        => '_arixwp_featured_display_page',
                    
'type'         => 'radio'// text,textarea,select,radio,checkbox
                    
'default'     => 'four',
                    
'options'     => array(
                        array(
'name' => __' Main Feature &amp; 3  Normal Features'THEMENAME ), 'value' => 'four''class' =>  'wparix_feature_four' ),
                        array(
'name' => __' Main Feature Only',  THEMENAME ), 'value' => 'one''class' => 'wparix_feature_one' ),
                        array(
'name' => __' 3 Normal Features  Only'THEMENAME ), 'value' => 'three''class' =>  'wparix_feature_three' ),
                        array(
'name' => __' Slidertron Slider',  THEMENAME ), 'value' => 'slide''class' => 'wparix_feature_slide'  )
                    )                                
                ),
                            
            
// Sidebar Location
                        
                
array (
                    
'before'     => '',
                    
'after'     => '',
                    
'name'         => __'Sidebar Location'THEMENAME ),
                    
'desc'         => __'This allows you to override the default sidebar settings.'THEMENAME ),
                    
'id'         => '_arixwp_sidebar_page',
                    
'type'         => 'radio'// text,textarea,select,radio,checkbox
                    
'default'     => 'left',
                    
'options'     => array(
                        array(
'name' => ' Left''value' => 'left'),
                        array(
'name' => ' Right''value' => 'right'),
                        array(
'name' => ' None''value' => 'none')
                    )                                
                ),                                    
                
            )
        ));
        
        
$arixwp_meta->init_meta();
    }
    
    
$arixwp_meta null
If i switch the $meta_posttype to post or leave it as page and set the $meta_template it works fine. Please help I am totally stumped.
#boxes #custom #issue #meta
  • Profile picture of the author SteveJohnson
    That's a very funky way of doing meta boxes...I think you're causing yourself a lot of grief.

    To begin with, your init_meta() function should be hooking into 'add_meta_boxes', not 'admin_init'. I don't think the post object is available at that stage, so you can't know what template is being used.
    Signature

    The 2nd Amendment, 1789 - The Original Homeland Security.

    Gun control means never having to say, "I missed you."

    {{ DiscussionBoard.errors[6687550].message }}
  • Profile picture of the author JohnnyS
    have you tried to check/debug if the save_meta_data is being called? try to insert error_log function (PHP: error_log - Manual) inside the save_meta_data function then check you logs in the error.log of your php..
    {{ DiscussionBoard.errors[6688105].message }}
  • Profile picture of the author jaasmit
    This is the issue for the masters of php. I am not like that so i can not tell you anything like that.
    If you rally want to make the happen you should contact with some professionals.
    {{ DiscussionBoard.errors[6689342].message }}

Trending Topics