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

My first post here.

I would like to ask if anyone can take a look at this.

How it should be like: I want custom meta-box containing checkbox in Wordpress administration while creating a new page. If I check this checkbox and publish the page and someone will want to see it, pop-up 18+ confirmation will appear.

  • This should be a plugin or take place in Functions.

I am trying to figure out how to connect it or make it assign an attribute which will have pop-up confirmatiion built-in.

Here is what I have so far: (thanks to 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' );

?>

As you can see, it is chaos so far. I will appreciate ANY help or improvement.

I would suggest you to use Advanced Custom Fields -plugin for making metabox for pages and posts. You can read more about ACF on their website http://www.advancedcustomfields.com/.

For this Adult page input you may want to check this one http://www.advancedcustomfields.com/resources/true-false/

With advanced custom fields you can create those metaboxes on admin panel quite easily. For example you could create checkbox labeled Adult page which have key adult_page. Then you could open your theme header.php and add this:

<?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; ?>