首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >自定义元框> 18+弹出确认

自定义元框> 18+弹出确认
EN

Stack Overflow用户
提问于 2014-09-03 17:41:02
回答 1查看 918关注 0票数 2

我的第一篇帖子。

我想问一问,是否有人可以看看这个。

它应该是什么样子:,我希望在创建新页面时,在Wordpress管理中包含复选框的自定义元框。如果我选中这个复选框并发布页面,并且有人希望看到它,弹出的18+确认就会出现。

  • 这应该是一个插件,或者发生在函数中。

我试图找出如何连接它或让它分配一个属性,它将有弹出确认内置。

这里是我到目前为止所拥有的:(感谢wordpress代码)

代码语言:javascript
复制
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' );

?>

正如你所看到的,到目前为止这是一场混乱。我将感激任何帮助或改进。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2014-09-03 18:25:54

我建议您使用高级自定义字段-plugin来为页面和帖子制作metabox。你可以在他们的网站http://www.advancedcustomfields.com/上读到更多关于ACF的信息。

对于这个Adult page输入,您可能要检查这个http://www.advancedcustomfields.com/resources/true-false/

使用高级自定义字段,您可以很容易地在管理面板上创建那些元数据。例如,您可以创建标记为Adult page的复选框,其中有键adult_page。然后您可以打开主题header.php并添加以下内容:

代码语言:javascript
复制
<?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
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/25650512

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档