2015-12-03 41 views
0

我已經創建了一個插件來保存使用WordPress自定義字段和元框的Authorz數據。插件沒有錯誤並且正在運行,但Meta盒未顯示。Wp自定義插件元提交框不可見

插件正在運行,我附上了屏幕截圖。

http://i63.tinypic.com/2czebyh.png

下面是我的插件代碼

<?php 
/*Plugin Name: CNSLounge 
Description: This plugin registers the 'Authors CNSLounge' post type. 
Version: 1.0 
Author: Anita Mandal 
Author URI: http://cosmoread.com/ 
License: GPLv2 
*/ 


// Register Custom Post Type 
function CNSLounge() { 
    register_post_type('CNSLounge', array(
    'labels' => array(

     'name' => 'Authorz', 
     'singular_name' => 'Authorz', 
     'add_new' => 'Add New', 
     'add_new_item' => 'Add New Authorz', 
     'edit' => 'Edit', 
     'edit_item' => 'Edit Authorz', 
     'new_item' => 'New Authorz', 
     'view' => 'View', 
     'view_item' => 'View Authorz', 
     'search_items' => 'Search Authorz', 
     'not_found' => 'No Authorz found', 
     'not_found_in_trash' => 'No Authorz found in Trash', 
     'parent' => 'Parent Authorz' 

     ), 
     'public' => true, 
     'menu_position' => 15, 
     'supports' => array(
     'title', 
     'editor', 
     'comments', 
     'thumbnail' 
    ), 
    'taxonomies' => array(
     '' 
    ), 
    'menu_icon' => plugins_url('images/image.png', __FILE__), 
    'has_archive' => true 
    )); 

    register_post_type('post_type', $args); 

    } 
    add_action('init', 'CNSLounge', 0); 


class Rational_Meta_Box { 
    private $screens = array(
     'post', 
    ); 
    private $fields = array(
     array(
      'id' => 'authorz-name', 
      'label' => 'Authorz Name', 
      'type' => 'text', 
     ), 
     array(
      'id' => 'author-photo', 
      'label' => 'Author Photo', 
      'type' => 'media', 
     ), 
     array(
      'id' => 'active-since', 
      'label' => 'Active Since', 
      'type' => 'date', 
     ), 
     array(
      'id' => 'languages-expression', 
      'label' => 'Languages Expression', 
      'type' => 'text', 
     ), 
     array(
      'id' => 'now-based-at', 
      'label' => 'Now Based at', 
      'type' => 'text', 
     ), 
     array(
      'id' => 'location-of-author', 
      'label' => 'Location of Author', 
      'type' => 'text', 
     ), 
     array(
      'id' => 'mostly-writes', 
      'label' => 'Mostly Writes', 
      'type' => 'text', 
     ), 
     array(
      'id' => 'about-author', 
      'label' => 'About Author', 
      'type' => 'textarea', 
     ), 
     array(
      'id' => 'magazines', 
      'label' => 'Magazines', 
      'type' => 'media', 
     ), 
     array(
      'id' => 'publication', 
      'label' => 'Publication', 
      'type' => 'media', 
     ), 
     array(
      'id' => 'gallery', 
      'label' => 'Gallery', 
      'type' => 'media', 
     ), 
     array(
      'id' => 'author-s-website', 
      'label' => 'Author\'s Website', 
      'type' => 'url', 
     ), 
     array(
      'id' => 'author-s-email', 
      'label' => 'Author\'s Email', 
      'type' => 'email', 
     ), 
     array(
      'id' => 'author-s-phone', 
      'label' => 'Author\'s Phone', 
      'type' => 'number', 
     ), 
    ); 

/** 
* Class construct method. Adds actions to their respective WordPress hooks. 
*/ 
public function __construct() { 

    add_action('add_meta_boxes', array($this, 'add_meta_boxes')); 
    add_action('admin_footer', array($this, 'admin_footer')); 
    add_action('save_post', array($this, 'save_post')); 
} 

/** 
* Hooks into WordPress' add_meta_boxes function. 
* Goes through screens (post types) and adds the meta box. 
*/ 
public function add_meta_boxes() { 
    foreach ($this->screens as $screen) { 
     add_meta_box(
      'cnslounge', 
      __('CNSLounge', 'rational-metabox'), 
      array($this, 'add_meta_box_callback'), 
      $screen, 
      'advanced', 
      'default' 
     ); 
    } 
} 

/** 
* Generates the HTML for the meta box 
* 
* @param object $post WordPress post object 
*/ 
public function add_meta_box_callback($post) { 
    wp_nonce_field('cnslounge_data', 'cnslounge_nonce'); 
    echo 'CNSLounge Author Meta Information'; 
    $this->generate_fields($post); 
} 

/** 
* Hooks into WordPress' admin_footer function. 
* Adds scripts for media uploader. 
*/ 
public function admin_footer() { 
    ?><script> 
     // https://codestag.com/how-to-use-wordpress-3-5-media-uploader-in-theme-options/ 
     jQuery(document).ready(function($){ 
      if (typeof wp.media !== 'undefined') { 
       var _custom_media = true, 
       _orig_send_attachment = wp.media.editor.send.attachment; 
       $('.rational-metabox-media').click(function(e) { 
        var send_attachment_bkp = wp.media.editor.send.attachment; 
        var button = $(this); 
        var id = button.attr('id').replace('_button', ''); 
        _custom_media = true; 
         wp.media.editor.send.attachment = function(props, attachment){ 
         if (_custom_media) { 
          $("#"+id).val(attachment.url); 
         } else { 
          return _orig_send_attachment.apply(this, [props, attachment]); 
         }; 
        } 
        wp.media.editor.open(button); 
        return false; 
       }); 
       $('.add_media').on('click', function(){ 
        _custom_media = false; 
       }); 
      } 
     }); 
    </script><?php 
} 

/** 
* Generates the field's HTML for the meta box. 
*/ 
public function generate_fields($post) { 
    $output = ''; 
    foreach ($this->fields as $field) { 
     $label = '<label for="' . $field['id'] . '">' . $field['label'] . '</label>'; 
     $db_value = get_post_meta($post->ID, 'advanced_options_' . $field['id'], true); 
     switch ($field['type']) { 
      case 'media': 
       $input = sprintf(
        '<input class="regular-text" id="%s" name="%s" type="text" value="%s"> <input class="button rational-metabox-media" id="%s_button" name="%s_button" type="button" value="Upload" />', 
        $field['id'], 
        $field['id'], 
        $db_value, 
        $field['id'], 
        $field['id'] 
       ); 
       break; 
      case 'textarea': 
       $input = sprintf(
        '<textarea class="large-text" id="%s" name="%s" rows="5">%s</textarea>', 
        $field['id'], 
        $field['id'], 
        $db_value 
       ); 
       break; 
      default: 
       $input = sprintf(
        '<input %s id="%s" name="%s" type="%s" value="%s">', 
        $field['type'] !== 'color' ? 'class="regular-text"' : '', 
        $field['id'], 
        $field['id'], 
        $field['type'], 
        $db_value 
       ); 
     } 
     $output .= $this->row_format($label, $input); 
    } 
    echo '<table class="form-table"><tbody>' . $output . '</tbody></table>'; 
} 

/** 
* Generates the HTML for table rows. 
*/ 
public function row_format($label, $input) { 
    return sprintf(
     '<tr><th scope="row">%s</th><td>%s</td></tr>', 
     $label, 
     $input 
    ); 
} 
/** 
* Hooks into WordPress' save_post function 
*/ 
public function save_post($post_id) { 
    if (! isset($_POST['cnslounge_nonce'])) 
     return $post_id; 

    $nonce = $_POST['cnslounge_nonce']; 
    if (!wp_verify_nonce($nonce, 'cnslounge_data')) 
     return $post_id; 

    if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE) 
     return $post_id; 

    foreach ($this->fields as $field) { 
     if (isset($_POST[ $field['id'] ])) { 
      switch ($field['type']) { 
       case 'email': 
        $_POST[ $field['id'] ] = sanitize_email($_POST[ $field['id'] ]); 
        break; 
       case 'text': 
        $_POST[ $field['id'] ] = sanitize_text_field($_POST[ $field['id'] ]); 
        break; 
      } 
      update_post_meta($post_id, 'cnslounge_' . $field['id'], $_POST[ $field['id'] ]); 
     } else if ($field['type'] === 'checkbox') { 
      update_post_meta($post_id, 'cnslounge_' . $field['id'], '0'); 
     } 
    } 
    } 
} 
new Rational_Meta_Box; 

?> 

回答

1

私人$屏幕=陣列( '後', );

因此,您要添加元框來發布,而不是您的自定義帖子類型。 在這裏使用您的自定義帖子類型slu 012。就像

private $ screens = array( 'CNSLounge', );