wordpress 如何向wordpress页面添加元框

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6890617/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 18:47:54  来源:igfitidea点击:

how to add a meta box to wordpress pages

wordpressmeta-boxes

提问by smashp

i want to make this code for pages

我想为页面制作此代码

add_action( 'add_meta_boxes', 'meta_box_video' );
function meta_box_video()
{
    add_meta_box( 'video-meta-box-id', 'Video Embed', 'meta_box_callback', 'post', 'normal', 'high' );
}

function meta_box_callback( $post )
{
    $values = get_post_custom( $post->ID );
    $selected = isset( $values['meta_box_video_embed'] ) ? $values['meta_box_video_embed'][0] : '';

    wp_nonce_field( 'my_meta_box_nonce', 'meta_box_nonce' );
    ?>
    <p>
        <label for="meta_box_video_embed"><p>Video Embed</p></label>
        <textarea name="meta_box_video_embed" id="meta_box_video_embed" cols="62" rows="5" ><?php echo $selected; ?></textarea>
    </p>
    <p>Leave it Empty ( if you want to use an image thumbnail ) .</p>
    <?php   
}

add_action( 'save_post', 'meta_box_video_save' );
function meta_box_video_save( $post_id )
{
    // Bail if we're doing an auto save
    if( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) return;

    // if our nonce isn't there, or we can't verify it, bail
    if( !isset( $_POST['meta_box_nonce'] ) || !wp_verify_nonce( $_POST['meta_box_nonce'], 'my_meta_box_nonce' ) ) return;

    // if our current user can't edit this post, bail
    if( !current_user_can( 'edit_post' ) ) return;

    // now we can actually save the data
    $allowed = array( 
        'a' => array( // on allow a tags
            'href' => array() // and those anchords can only have href attribute
        )
    );

    // Probably a good idea to make sure your data is set

    if( isset( $_POST['meta_box_video_embed'] ) )
        update_post_meta( $post_id, 'meta_box_video_embed', $_POST['meta_box_video_embed'] );

}

回答by Peter Ajtai

This:

这个:

function meta_box_video()
{
    add_meta_box( 'video-meta-box-id', 'Video Embed', 'meta_box_callback', 'post', 'normal', 'high' );
}

Should specify pagenot post.

应该指定pagenot post

function meta_box_video()
{                                      // --- Parameters: ---
    add_meta_box( 'video-meta-box-id', // ID attribute of metabox
                  'Video Embed',       // Title of metabox visible to user
                  'meta_box_callback', // Function that prints box in wp-admin
                  'page',              // Show box for posts, pages, custom, etc.
                  'normal',            // Where on the page to show the box
                  'high' );            // Priority of box in display order
}

Take a look at the Codex for add_meta_box(). The examples are very helpful. The portion you are interested in is under "Parameter". The fourth parameter allows you to specify whether you want the metabox on pages, posts, etc.

查看add_meta_box()Codex。这些例子非常有帮助。您感兴趣的部分在“参数”下。第四个参数允许您指定是否要在页面、帖子等上使用元框。