Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to add a meta box to wordpress pages

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

}
like image 415
smashp Avatar asked Jul 31 '11 16:07

smashp


People also ask

Where is meta box in WordPress?

Metaboxes. When you open a WordPress post to edit it you'll see the page is broken up into different sections (many of which are located in the sidebar on the right hand side). All of these sections are technically 'meta boxes'.

How do I make a meta box?

To create a meta box use the add_meta_box() function and plug its execution to the add_meta_boxes action hook. The following example is adding a meta box to the post edit screen and the wporg_cpt edit screen.

How do I add a meta box to a custom post type in WordPress?

To add a meta box to a number of post types screens – post , page and a book custom post type; create an array of the post types, iterate over the array and use add_meta_box() to add the meta box to them.


1 Answers

This:

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

Should specify page not 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.

like image 97
Peter Ajtai Avatar answered Sep 22 '22 14:09

Peter Ajtai