2014-09-03 107 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