2014-09-03 103 views
1

我的第一篇文章在這裏。自定義元框> 18+彈出確認

我想問問有沒有人可以看看這個。

它應該是什麼樣的:我想創建一個新頁面時,在Wordpress管理中包含複選框的自定義元框。如果我選中此複選框併發布該頁面,並且有人希望看到該頁面,則會彈出18+確認窗口。

  • 這應該是一個插件或發生在函數中。

我想弄清楚如何連接它或使它分配一個屬性,將有彈出式confirmmatiion內置。

這是我到目前爲止有:(感謝WordPress的抄本)

function adult_add_meta_box() { 

    $screens = array('post', 'page'); 

    foreach ($screens as $screen) { 

     add_meta_box(
      'adult_sectionid',__('Adult content', 'adult_textdomain'), 'adult_meta_box_callback', $screen, 'side'); 
    } 
} 
add_action('add_meta_boxes', 'adult_add_meta_box'); 

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

    // Add an nonce field so we can check for it later. 
    wp_nonce_field('adult_meta_box', 'adult_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_meta_value_key', true); 

    echo '<label for="adult_new_field">'; 
    _e('<input type="checkbox" name="Adult" value="adult-content">' . " 18+"); 
    echo '</label> '; 
} 

/** 
* When the post is saved, saves our custom data. 
* 
* @param int $post_id The ID of the post being saved. 
*/ 
function adult_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['adult_meta_box_nonce'])) { 
     return; 
    } 

    // Verify that the nonce is valid. 
    if (! wp_verify_nonce($_POST['adult_meta_box_nonce'], 'adult_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 (isset($_POST['post_type']) && 'page' == $_POST['post_type']) { 

     if (! current_user_can('edit_page', $post_id)) { 
      return; 
     } 

    } else { 

     if (! current_user_can('edit_post', $post_id)) { 
      return; 
     } 
    } 

    /* OK, it's safe for us to save the data now. */ 

    // Make sure that it is set. 
    if (! isset($_POST['adult_new_field'])) { 
     return; 
    } 

    // Sanitize user input. 
    $my_data = sanitize_text_field($_POST['adult_new_field']); 

    // Update the meta field in the database. 
    update_post_meta($post_id, '_my_meta_value_key', $my_data); 
} 
add_action('save_post', 'adult_save_meta_box_data'); 

?> 

正如你所看到的,它是混亂爲止。 我會感謝任何幫助或改進。

回答

0

我建議你使用高級自定義字段 - 插件來製作頁面和帖子的met​​abox。您可以在他們的網站http://www.advancedcustomfields.com/上閱讀更多關於ACF的信息。

對於您可能要檢查這一項http://www.advancedcustomfields.com/resources/true-false/

憑藉先進的自定義字段,你可以很容易地創建管理面板上那些metaboxes此Adult page輸入。例如,您可以創建標記爲Adult page的複選框,其中包含密鑰adult_page。然後你可以打開你的主題header.php並添加:

<?php if (get_field("adult_page")) : ?> 

    <script type="text/javascript"> 

     if (window.confirm('Are you older than 18?')) { 
      // user pressed 'ok' 
     } else { 
      // user pressed 'cancel' 
     } 

    </script> 

<?php endif; ?> 
+0

你是認真的嗎? :D謝謝!我試圖弄清楚這幾個小時,你像一個聰明的球進來。 感謝您的回答。它工作正常。 我只是想知道,包含PHP元素基於你寫入到header.php中的元素是否理想? 或者有另外一種方法如何將field_check加入網站的功能? – JNV 2014-09-03 19:42:39

+0

這真的取決於。我會這樣做:如果字段'adult_page'設置爲true,用CSS隱藏頁面內容。然後顯示樣式化的JavaScript彈出式窗口(例如,您可以使用http://dimsemenov.com/plugins/magnific-popup/),它會顯示一些文本(您需要確認您的年齡大於18 ...等),並且如果用戶確認,您可以通過使用javascript更改CSS來顯示頁面內容。 Header.php是好的地方添加這個,但你需要小心。在存檔頁面上,如果第一個帖子/頁面的字段'adult_page'設置爲true,則存檔頁面的字段'adult_page'設置爲true。 – 2014-09-04 07:49:44