2017-08-13 19 views
0

カスタムメタボックスをワードプレスページに追加するには、次のようなコードがありますが、何らかの理由で選択したオプションを保存しません。どんなアイデアが間違っていますか?WordPressカスタムメタボックスは保存されません

ありがとうございました。

// Add Header Select Option MetaBox 
function alfie_header_select_meta() { 
add_meta_box('alfie_header_select_meta', __('Header Style', 'alfie'), 'alfie_header_select_meta_callback', 'page', 'side'); 
} 
add_action('add_meta_boxes', 'alfie_header_select_meta'); 

function alfie_header_select_meta_callback($post) { 
$values = get_post_custom($post->ID); 
$selected = isset($values['alfie_selected_header']) ? esc_attr($values['alfie_selected_header'][0]) : ”; 
wp_nonce_field('alfie_header_select_meta_nonce', 'alfie_header_select_meta_nonce'); 
?> 
    <p> 
     <select name="alfie_selected_header" id="alfie_selected_header"> 
      <option value="alfie-header-default" <?php selected($selected, 'default'); ?>>Default</option> 
      <option value="alfie-header-style-minimal" <?php selected($selected, 'minimal'); ?>>Minimal</option> 
      <option value="alfie-header-test" <?php selected($selected, 'test'); ?>>Just a test option</option> 
     </select> 
    </p> 
<?php 
} 

function alfie_meta_save($post_id) { 
$is_autosave = wp_is_post_autosave($post_id); 
$is_revision = wp_is_post_revision($post_id); 
$is_valid_nonce = (isset($_POST[ 'alfie_header_select_meta_nonce' ]) && wp_verify_nonce($_POST[ 'alfie_header_select_meta_nonce' ], basename(__FILE__))) ? 'true' : 'false'; 

if ($is_autosave || $is_revision || !$is_valid_nonce) { 
    return; 
} 

if(isset($_POST[ 'alfie_selected_header' ])) { 
    update_post_meta($post_id, 'alfie_selected_header', sanitize_text_field($_POST[ 'alfie_selected_header' ])); 
} 
} 
add_action('save_post', 'alfie_meta_save'); 
+0

おそらく、alfie_meta_save()内の自動保存/ノンスチェックにあります。それがブロックされた後にコードが実行されているのを見ましたか?あなたはそれを試してみるべきです。 – Kris

+0

ダムになって申し訳ありませんが、どうすればいいですか? –

+0

1つのアイデアは、ifブロックのあとにデフォルトのメタを保存し、それが追加されるかどうかを確認することです。 – Kris

答えて

1

ここに、動作しているデバッグコードがあります。 ポストメタが保存されました。しかし、それは文字列の値の違いとあなたがメタを取得した方法のために表示されませんでした。単一のフィールドにはget_post_metaを使ってください。

// Add Header Select Option MetaBox 
function alfie_header_select_meta() { 
add_meta_box('alfie_header_select_meta', __('Header Style', 'alfie'), 'alfie_header_select_meta_callback', 'page', 'side'); 
} 
add_action('add_meta_boxes', 'alfie_header_select_meta'); 

function alfie_header_select_meta_callback($post) { 
$selected = get_post_meta($post->ID,'alfie_selected_header',true); 
wp_nonce_field('alfie_header_select_meta_nonce', 'alfie_header_select_meta_nonce'); 
?> 
    <p> 
     <select name="alfie_selected_header" id="alfie_selected_header"> 
      <option value="alfie-header-default" <?php selected($selected, 'alfie-header-default'); ?>>Default</option> 
      <option value="alfie-header-style-minimal" <?php selected($selected, 'alfie-header-style-minimal'); ?>>Minimal</option> 
      <option value="alfie-header-test" <?php selected($selected, 'alfie-header-test'); ?>>Just a test option</option> 
     </select> 
    </p> 
<?php 
} 

function alfie_meta_save($post_id) { 
$is_autosave = wp_is_post_autosave($post_id); 
$is_revision = wp_is_post_revision($post_id); 
$is_valid_nonce = (isset($_POST[ 'alfie_header_select_meta_nonce' ]) && wp_verify_nonce($_POST[ 'alfie_header_select_meta_nonce' ], basename(__FILE__))) ? 'true' : 'false'; 

if ($is_autosave || $is_revision || !$is_valid_nonce) { 
    return; 
} 

if(isset($_POST[ 'alfie_selected_header' ])) { 
    update_post_meta($post_id, 'alfie_selected_header', sanitize_text_field($_POST[ 'alfie_selected_header' ])); 
} 
} 
add_action('save_post', 'alfie_meta_save'); 
関連する問題