繁体   English   中英

自定义meta-box> 18+弹出确认

[英]Custom meta-box > 18+ pop-up confirmation

我的第一篇文章在这里。

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

效果如何:我想在创建新页面时在Wordpress管理中包含复选框的自定义元框。 如果我选中此复选框并发布页面,但有人想要查看它,则会弹出18+确认。

  • 这应该是一个插件或在Functions中进行。

我试图弄清楚如何连接它或使它分配一个将内置弹出式确认的属性。

这是我到目前为止的内容:(感谢wordpress Codex)

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' );

?>

如您所见,到目前为止还很混乱。 我将不胜感激。

我建议您使用Advanced Custom Fields -plugin为页面和帖子制作metabox。 您可以在其网站http://www.advancedcustomfields.com/上了解有关ACF的更多信息。

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

使用高级自定义字段,您可以非常轻松地在管理面板上创建这些元框。 例如,您可以创建标签为“ 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; ?>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM