metaboxes disappear on saved pages

Ok I have a wierd issue with custom meta boxes that I am at a loss for.

I am using this class

Read More
<?php
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;
    }
} 

and this is the php to call the class

<?php

    $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;

I am using several instances of this for posts and custom page templates and it works fine. But when just trying to apply it to a generic page. The Meta boxes show when you choose to add a new page. But as soon as you save or publish they dissappear.

Related posts

Leave a Reply