2014-08-27 47 views
1

我創建了一個自定義元框,您可以從某些單選按鈕中選擇一個值並將其保存到wordpress數據庫中的post_meta表中。用下面的代碼保存我的價值:如何設置自定義元框中的單選按鈕被選中?

function save_value_of_my_custom_metabox ($post_id, $post){ 
    $post_id = get_the_ID(); 
    $new_meta_value = (isset($_POST['the_name_of_the_radio_buttons']) ? sanitize_html_class($_POST['the_name_of_the_radio_buttons']) : ''); 
    $meta_key = 'my_key'; 
    update_post_meta($post_id, $meta_key, $new_meta_value); 
} 

但如果職位將被再次編輯我要與當前值的單選按鈕來設置檢查。什麼是最好的方式來做到這一點?這裏是顯示元框的功能:

function my_custom_meta_box($object, $box) { 
    $post_id=get_the_ID(); 
    $key='my_key'; 
    $the_value_that_should_be_set_to_checked=get_post_meta($post_id, $key); 
    //$the_value_that_should_be_set_to_checked[0] returns the value as string 
    ?>  
    <label for="my_custom_metabox"><?php _e("Choose value:", 'choose_value'); ?></label> 
    <br /> 
     <input type="radio" name="the_name_of_the_radio_buttons" value="value1">Value1<br> 
     <input type="radio" name="the_name_of_the_radio_buttons" value="value2">Value2<br> 
     <input type="radio" name="the_name_of_the_radio_buttons" value="value3">Value3<br> 
     <input type="radio" name="the_name_of_the_radio_buttons" value="value4">Value4<br> 

     <?php 
} 

我可以寫類似if(isset($the_value_that_should_be_set_to_checked[0])=="value of that line") echo "checked='checked'";中的每一行,但似乎並不很優雅給我。在WordPress中使用javascript也非常複雜,因爲我必須使用鉤子,排隊腳本,只是用一行javascript來更改checked屬性,這不值得。最佳做法是什麼?

+0

提供您使用的動作鉤子。 – 2014-08-27 08:32:42

+0

add_action('load-post.php','my_custom_meta_box_setup'); add_action('load-post-new.php','my_custom_meta_box_setup');並在設置函數中:add_action('add_meta_boxes','my_custom__meta_box');和add_action('save_post','save_value_of_my_custom_metabox',10,2); – user2718671 2014-08-27 08:38:15

回答

6

我假設您正在嘗試爲'Posts'添加自定義元框。下面的代碼將爲你工作。它將在添加新帖子或編輯帖子屏幕上顯示單選按鈕。請閱讀代碼中的註釋。它會幫助你理解代碼。

您可以使用WordPress的checked函數來決定是否選擇單選按鈕。

隨意問你是否有任何疑問。

/** 
* Adds a box to the main column on the Post add/edit screens. 
*/ 
function wdm_add_meta_box() { 

     add_meta_box(
       'wdm_sectionid', 'Radio Buttons Meta Box', 'wdm_meta_box_callback', 'post' 
     ); //you can change the 4th paramter i.e. post to custom post type name, if you want it for something else 

} 

add_action('add_meta_boxes', 'wdm_add_meta_box'); 

/** 
* Prints the box content. 
* 
* @param WP_Post $post The object for the current post/page. 
*/ 
function wdm_meta_box_callback($post) { 

     // Add an nonce field so we can check for it later. 
     wp_nonce_field('wdm_meta_box', 'wdm_meta_box_nonce'); 

     /* 
     * Use get_post_meta() to retrieve an existing value 
     * from the database and use the value for the form. 
     */ 
     $value = get_post_meta($post->ID, 'my_key', true); //my_key is a meta_key. Change it to whatever you want 

     ?> 
     <label for="wdm_new_field"><?php _e("Choose value:", 'choose_value'); ?></label> 
     <br /> 
     <input type="radio" name="the_name_of_the_radio_buttons" value="value1" <?php checked($value, 'value1'); ?> >Value1<br> 
     <input type="radio" name="the_name_of_the_radio_buttons" value="value2" <?php checked($value, 'value2'); ?> >Value2<br> 
     <input type="radio" name="the_name_of_the_radio_buttons" value="value3" <?php checked($value, 'value3'); ?> >Value3<br> 
     <input type="radio" name="the_name_of_the_radio_buttons" value="value4" <?php checked($value, 'value4'); ?> >Value4<br> 

     <?php 

} 

/** 
* When the post is saved, saves our custom data. 
* 
* @param int $post_id The ID of the post being saved. 
*/ 
function wdm_save_meta_box_data($post_id) { 

     /* 
     * We need to verify this came from our screen and with proper authorization, 
     * because the save_post action can be triggered at other times. 
     */ 

     // Check if our nonce is set. 
     if (!isset($_POST['wdm_meta_box_nonce'])) { 
       return; 
     } 

     // Verify that the nonce is valid. 
     if (!wp_verify_nonce($_POST['wdm_meta_box_nonce'], 'wdm_meta_box')) { 
       return; 
     } 

     // If this is an autosave, our form has not been submitted, so we don't want to do anything. 
     if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE) { 
       return; 
     } 

     // Check the user's permissions. 
     if (!current_user_can('edit_post', $post_id)) { 
       return; 
     } 


     // Sanitize user input. 
     $new_meta_value = (isset($_POST['the_name_of_the_radio_buttons']) ? sanitize_html_class($_POST['the_name_of_the_radio_buttons']) : ''); 

     // Update the meta field in the database. 
     update_post_meta($post_id, 'my_key', $new_meta_value); 

} 

add_action('save_post', 'wdm_save_meta_box_data'); 
+0

非常感謝!我希望避免檢查每一行,但我想這是不可能的。但我使用WP的selected()來動態添加行,因此很棒。並感謝關於wp nonce安全問題的評論。我仍然必須整合這一點。 – user2718671 2014-08-27 11:11:29

0

我發現了一個工作解決方案,但我認爲這不是你應該怎麼做的。還開着更好的解決方案;)

PHP代碼下添加該代碼從上面:

if(isset($$the_value_that_should_be_set_to_checked[0])){ 
    $the_value_that_should_be_set_to_checked= $the_value_that_should_be_set_to_checked[0]; 
} 
else{ 
    $the_value_that_should_be_set_to_checked=''; 
} 

下面是我添加的單選按鈕下面的代碼:

<script type="text/javascript"> 
    jQuery(document).ready(function() {    
     var checked_value= <?php echo json_encode($the_value_that_should_be_set_to_checked);?>; 
     if(checked_value!==''){      
      jQuery("input[name=the_name_of_the_radio_buttons][value="+checked_value+"]").attr('checked', 'checked');      
     } 
    }); 
</script> 

PS:在$選擇器不起作用,但可能取決於您使用的主題。

相關問題