lundi 25 avril 2016

Metabox save method dont work for me

I am trying to make a metabox class, everything is ok in my function method but my save method in my class dont work, someone please give me a solution where is the problem in my save function ? my code is given bellow

    <?php

if ( ! class_exists( 'Metabox_Library') ) :

/**
 * All Types Meta Box class.
 *
 * @package All Types Meta Box
 * @since 1.0
 *
 * @todo Nothing.
 */

class Metabox_Library
{

      /**
   * Holds meta box object
   *
   * @var object
   * @access protected
   */
    protected $meta_box;

  /**
   * Holds meta box fields.
   *
   * @var array
   * @access protected
   */
    protected $_prefix;

  /**
   * Holds Prefix for meta box fields.
   *
   * @var array
   * @access protected
   */
    public $fields = array();

  /**
   * Use local images.
   *
   * @var bool
   * @access protected
   */

   /**
   * $field_types  holds used field types
   * @var array
   * @access public
   * @since 2.9.7
   */
    public $field_types = array();



    public function __construct($metabox,$meta_fields) {
        // Assign meta box values to local variables and add it's missed values.
        $this->meta_box = $metabox;
        $this->prefix = (isset($metabox['prefix'])) ? $metabox['prefix'] : '';
        $this->fields = $meta_fields;

        foreach ($this->fields as $field) {
            $this->field_types = $field['type'];
        }
        // If we are not in admin area exit.
        if ( ! is_admin() )
          return;

        // Add metaboxes
        add_action( 'add_meta_boxes', array( $this, 'add_metabox' ) );


            add_action('save_post', array($this, 'save_custom_meta'));
            // Must enqueue for all pages as we need js for the media upload, too.
        if ($this->is_edit_page()) {
            add_action( 'admin_enqueue_scripts', array( $this, 'load_scripts' ) );
            add_action( 'admin_head', array($this,'add_custom_scripts'));
            }       


    }

    public function fields_type() {
        foreach ($this->fields as $field) {
            $this->field_types = $field['type'];
        }
    }

    public function add_metabox($postType) {
        if (in_array($postType, $this->meta_box['pages'])) {


            add_meta_box( $this->meta_box['id'], $this->meta_box['title'], array( $this, 'show_fields' ),$postType, $this->meta_box['context'], $this->meta_box['priority'] );


        }
    }

    public function show_fields() {

        global $post;
        $fields = $this->fields;
    // Use nonce for verification
    echo '<input type="hidden" id="custom_meta_box_nonce" name="custom_meta_box_nonce" value="'.wp_create_nonce(basename(__FILE__)).'" />';

        // Begin the field table and loop
        echo '<table class="form-table">';
        if (is_array($fields) || is_object($fields)) {

            foreach ($fields as $field) {
                // get value of this field if it exists for this post
                $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']) {

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

                        // select
                        case 'select':
                            echo '<select name="'.$field['id'].'" id="'.$field['id'].'">';
                            foreach ($field['options'] as $option) {
                                echo '<option', $meta == $option['value'] ? ' selected="selected"' : '', ' value="'.$option['value'].'">'.$option['label'].'</option>';
                            }
                            echo '</select><br /><span class="description">'.$field['desc'].'</span>';
                        break;

                        // checkbox
                        case 'checkbox':
                            echo '<input type="checkbox" name="'.$field['id'].'" id="'.$field['id'].'" ',$meta ? ' checked="checked"' : '','/>
                                <label for="'.$field['id'].'">'.$field['desc'].'</label>';
                        break;

                        // radio
                        case 'radio':
                            foreach ( $field['options'] as $option ) {
                                echo '<input type="radio" name="'.$field['id'].'" id="'.$option['value'].'" value="'.$option['value'].'" ',$meta == $option['value'] ? ' checked="checked"' : '',' />
                                        <label for="'.$option['value'].'">'.$option['label'].'</label><br />';
                            }
                        break;
                        // checkbox_group
                        case 'checkbox_group':
                            foreach ($field['options'] as $option) {
                                echo '<input type="checkbox" value="'.$option['value'].'" name="'.$field['id'].'[]" id="'.$option['value'].'"',$meta && in_array($option['value'], $meta) ? ' checked="checked"' : '',' /> 
                                        <label for="'.$option['value'].'">'.$option['label'].'</label><br />';
                            }
                            echo '<span class="description">'.$field['desc'].'</span>';
                        break;
                        // tax_select
                        case 'tax_select':
                            echo '<select name="'.$field['id'].'" id="'.$field['id'].'">
                                    <option value="">Select One</option>'; // Select One
                            $terms = get_terms($field['id'], 'get=all');
                            $selected = wp_get_object_terms($post->ID, $field['id']);
                            foreach ($terms as $term) {
                                if (!empty($selected) && !strcmp($term->slug, $selected[0]->slug)) 
                                    echo '<option value="'.$term->slug.'" selected="selected">'.$term->name.'</option>'; 
                                else
                                    echo '<option value="'.$term->slug.'">'.$term->name.'</option>'; 
                            }
                            $taxonomy = get_taxonomy($field['id']);
                            echo '</select><br /><span class="description"><a href="'.get_bloginfo('url').'/wp-admin/edit-tags.php?taxonomy='.$field['id'].'">Manage '.$taxonomy->label.'</a></span>';
                        break;


                        } //end switch
                echo '</td></tr>';
            } 
        }// end foreach
        echo '</table>'; // end table
    }


    public  function save_custom_meta($post_id) {
            global $post_id;

            // verify nonce
            if (!wp_verify_nonce($_POST['custom_meta_box_nonce'], basename(__FILE__))) 
                return $post_id;
            // check autosave
            if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE)
                return $post_id;
            // check permissions
            if (isset($_POST['page']) == isset($_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 ($this->fields as $field) {
                $old = get_post_meta($post_id, $field['id'], true);
                $new = isset($_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
            if($field['type'] == 'tax_select') continue;
            // save taxonomies
          $post = get_post($post_id);
          $category = $_POST['category'];
          wp_set_object_terms( $post_id, $category, 'category' );
        }



    /**
   * Check if current page is edit page.
   *
   * @since 1.0
   * @access public
   */
     public function is_edit_page() {
        global $pagenow;
        return in_array( $pagenow, array( 'post.php', 'post-new.php' ) );
      }







}


 // End Class
endif; // End Check Class Exists



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire