2016-04-08 15 views
0

次のコードを試しましたが、カスタムファイルの価値を保存するのではありません。上記でカスタムファイルを追加してワードプレスに保存するには?

add_action('add_meta_boxes', 'cd_meta_box_add'); 
function cd_meta_box_add() 
{ 
add_meta_box('my-meta-box-id', 'My First Meta Box', 'cd_meta_box_cb', 'product', 'side', 'high'); 
} 

私はposttype上記のコードでは、製品

function cd_meta_box_cb($product) 
{ 
    $values = get_post_custom($product->ID); 
    $text = isset($values['my_meta_box_text']) ? esc_attr($values['my_meta_box_text'][0]) : ”; 

?> 
<p> 
<label for="my_meta_box_text">Text Label</label> 
<input type="text" name="my_meta_box_text" id="my_meta_box_text" value="<?php echo $text; ?>" /> 
    </p> 
<?php   
} 

はmetaboxの値が、それはディスプレイ

add_action('save_post', 'cd_meta_box_save'); 
function cd_meta_box_save($product_id) 
{ 
// Bail if we're doing an auto save 
if(defined('DOING_AUTOSAVE') && DOING_AUTOSAVE) return; 

// if our nonce isn't there, or we can't verify it, bail 
if(!isset($_POST['meta_box_nonce']) || !wp_verify_nonce($_POST['meta_box_nonce'], 'my_meta_box_nonce')) return; 

// if our current user can't edit this post, bail 
if(!current_user_can('edit_post')) return; 
} 
ある場合、それはテキストボックスでmetaboxを追加しますである場合、それは表示されたコードを追加有します

データをwp_postmetaテーブルに保存します。上記のコードは試しました。私はワードプレスで初心者です。私に何か提案してもらえますか?

+0

のためにこれを従って、あなたは、フォームが送信されたかどうかを確認かどうか、あなたが保存するときにしてくださいことはできますか?もしあれば、 '$ _REQUEST'の値をエコーし​​て、すべてのデータを取得しているかどうかを確認してください。 – Milap

答えて

0

(それが正常に動作してもpostmetaテーブルにカスタムフィールドを保存しています)などがあります。

class Rational_Meta_Box { 
     private $screens = array(
      'post', 
     ); 
     private $fields = array(
      array(
       'id' => 'custom-field-1', 
       'label' => 'custom field 1', 
       'type' => 'text', 
      ), 
      array(
       'id' => 'custom-field-2', 
       'label' => 'custom field 2', 
       'type' => 'text', 
      ), 
     ); 

/** 
* 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('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(
      'my-custom-fields', 
      __('my custom fields', 'wordpress'), 
      array($this, 'add_meta_box_callback'), 
      $screen, 
      'advanced', 
      'high' 
     ); 
    } 
} 

    /** 
    * Generates the HTML for the meta box 
    * 
    * @param object $post WordPress post object 
    */ 



public function add_meta_box_callback($post) { 
    wp_nonce_field('my_custom_fields_data', 'my_custom_fields_nonce'); 
    echo 'its for custom fields for post typle'; 
    $this->generate_fields($post); 
} 

    /** 
    * 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, 'my_custom_fields_' . $field['id'], true); 
       switch ($field['type']) { 
        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['my_custom_fields_nonce'])) 
       return $post_id; 

      $nonce = $_POST['my_custom_fields_nonce']; 
      if (!wp_verify_nonce($nonce, 'my_custom_fields_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, 'my_custom_fields_' . $field['id'], $_POST[ $field['id'] ]); 
       } else if ($field['type'] === 'checkbox') { 
        update_post_meta($post_id, 'my_custom_fields_' . $field['id'], '0'); 
       } 
      } 
     } 
    } 
    new Rational_Meta_Box; 

これで、投稿タイプにカスタムフィールドが作成され、保存されます。

参照https://developer.wordpress.org/plugins/metadata/creating-custom-meta-boxes/

+0

それは完璧なソリューションを動作しています.. – Dipika

-1

ポッドをご覧ください。 これはカスタマイズのための非常に強力なプラグインです。 http://pods.io/

+0

私はプラグインを使いたくありません。 – Dipika

0

add_action( 'save_post'、 'cd_meta_box_save')を置き換えます。以下のコードを入力してください。

add_action('save_post', 'cd_meta_box_save'); 
function cd_meta_box_save($product_id) 
{ 
    if(isset($_POST[ 'my_meta_box_text' ])) { 
     update_post_meta($product_id,'my_meta_box_text', $_POST['my_meta_box_text']); 
    } 
} 

全コードのポストのためのカスタムフィールドを作成するためのこのコードを使用する

add_action('add_meta_boxes', 'cd_meta_box_add'); 
function cd_meta_box_add() 
{ 
add_meta_box('my-meta-box-id', 'My First Meta Box', 'cd_meta_box_cb', 'post', 'side', 'high'); 

} 

function cd_meta_box_cb($product) 
{ 
    $values = get_post_custom($product->ID); 
    $text = isset($values['my_meta_box_text']) ? esc_attr($values['my_meta_box_text'][0]) : ''; 

?> 
<p> 
<label for="my_meta_box_text">Text Label</label> 
<input type="text" name="my_meta_box_text" id="my_meta_box_text" value="<?php echo $text; ?>" /> 
    </p> 
<?php   
} 


add_action('save_post', 'cd_meta_box_save'); 
function cd_meta_box_save($product_id) 
{ 

    if(isset($_POST[ 'my_meta_box_text' ])) { 
     update_post_meta($product_id,'my_meta_box_text', $_POST['my_meta_box_text']); 
    } 
} 
関連する問題