1
votes

I would like to be able to add a new 'widget' to the sidebar of the admin section, specifically on the "New Page" and "Edit Page" pages.

I am going through the Wordpress API Documentation, but I can't seem to find where to add a new section in the first "postbox-container-1" section.

If you go into a wordpress admin section, then click add page, I would like to add a new widget under "Publish/Page Attributes/Featured Image" widgets.

The closest I have come is this: add_action('dbx_post_sidebar', 'pluginFunctionHere');. Unfortunately, this adds all of my content under the main page WYSIWYG editor.

I know how to add it regularly, but I would like to make this a plugin for general use. Any ideas? or is there a specific hook for this section?

2
Do you mean you want to add Metabox to "Edit Page" & "Create New Page" pages ? - Shazzad
@Shazzad Is a metabox the same thing as "Publish", "Page Attributes" and "Featured Image"? I know programming, but this is my first visit into wordpress... themergency.com/wordpress-meta-box-roundup - ntgCleaner

2 Answers

3
votes

I tried the above example but it didn't work. Then I searched what it is missing and found it. I have tweaked the original a little.

/**
 * Register meta box(es).
 */
function wpdocs_register_meta_boxes() {
    add_meta_box( 'meta-box-id', __( 'Metabox Title', 'textdomain' ), 'wpdocs_my_display_callback', 'post', 'side', 'high');
}
add_action( 'add_meta_boxes', 'wpdocs_register_meta_boxes' );

/**
 * Meta box display callback.
 *
 * @param WP_Post $post Current post object.
 */
function wpdocs_my_display_callback( $post ) {
    // Display code/markup goes here. Don't forget to include nonces!
    ?> <h1>Test</h1><?php
}

/**
 * Save meta box content.
 *
 * @param int $post_id Post ID
 */
function wpdocs_save_meta_box( $post_id ) {
    // Save logic goes here. Don't forget to include nonce checks!
}
add_action( 'save_post', 'wpdocs_save_meta_box' );
1
votes

First, you need to register your meta box. You could use add_meta_boxes_page hook.

add_action( "add_meta_boxes_page", "se20892273_add_meta_boxes_page" );

// Register Your Meta box
function se20892273_add_meta_boxes_page( $post )
{
    add_meta_box( 
       'se20892273_custom_meta_box', // this is HTML id
       'Metabox Title', 
       'se20892273_custom_meta_box', // the callback function
       'page', // register on post type = page
       'side', // 
       'core'
    );
}

Then use the callback function to generate the template

function se20892273_custom_meta_box( $post )
{
    // you will get the $post object
    // do your stuff here
}

To save any input value used on your meta box, use 'save_post_page' hook.

add_action( "save_post_page", "se20892273_save_post_page" );
function se20892273_save_post_page( $post_ID )
{
    if ( defined('DOING_AUTOSAVE') && DOING_AUTOSAVE ) 
        return $post_ID ;

    if( isset( $_POST['input_name'] ))
    {
        update_post_meta( $post_ID, '_w4_template', $_POST['input_name'] );
    }
}