簡體   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