0

I am trying to create two additional add_meta_box context locations ('after_title' and 'after_editor') within posts.

This doesn't appear to do what I think it should. The meta box does appear on the page just within the normal context, not in the new defined context locations. Does anyone have any insight on this?

Thank you in advance for your time and consideration,

Tim

REFERENCES:

http://adambrown.info/p/wp_hooks/hook/edit_form_after_editor?version=4.4&file=wp-admin/edit-form-advanced.php

http://adambrown.info/p/wp_hooks/hook/edit_form_after_title?version=4.4&file=wp-admin/edit-form-advanced.php

ACTION HOOKS:

    public function initialize_hooks() {

        add_action( 'edit_form_after_editor', array( $this, 'add_after_editor_meta_boxes' ) );
        add_action( 'edit_form_after_title', array( $this, 'add_after_title_meta_boxes' ) ) ;

    }

    /**
     * Register meta box context location: after_editor
     *
     * @return null
     **/
    public function add_after_editor_meta_boxes() {
        global $post, $wp_meta_boxes;
        # Output the `after_editor` meta boxes:
        do_meta_boxes( get_current_screen(), 'after_editor', $post );
    }

    /**
     * Register meta box context location: after_title
     *
     * @return null
     **/
    public function add_after_title_meta_boxes() {
        global $post, $wp_meta_boxes;
        # Output the `after_title` meta boxes:
        do_meta_boxes( get_current_screen(), 'after_title', $post );
    }

ADD META BOX:

        /**
         * The function responsible for creating the actual meta box.
         *
         * @since    0.2.0
         **/
        public function add_meta_box() {

        add_meta_box(
            'new-meta-box',
            "New Meta Box",
            array( $this, 'display_meta_box' ),
            'after_editor',
            'high',
            'default'
        );
4
  • Where did you call the add_meta_box () function, I can't see it in the code you've added. Have you added the complete code in here?
    – Kumar
    Commented Mar 16, 2016 at 9:33
  • @sven Hi, no I didn't add the code for the entire plugin. In an effort to keep it minimal and still verifiable I chose to give the relevant info. If you'd like I can code a shell plugin and edit/insert it at the bottom of the post. Commented Mar 16, 2016 at 11:26
  • You don't have to add your entire plugin code, but at least as @sven ask, you need to add code how you call add_meta_box. Please edit yours.
    – Jevuska
    Commented Mar 16, 2016 at 13:19
  • Solved it. Answered below. Thank you to everyone who spent any time on it. Commented Mar 16, 2016 at 13:41

2 Answers 2

1

Here is a fully working dummy plugin to show how to add the new meta box contexts.

STEPS:

  1. Create the plugin structure below and copy the code into plugin-shell.php.

  2. Leave the style sheets empty.

  3. Install and activate the plugin.

  4. Navigate to the plugin from the admin menu item 'Plugin Shell | Add New'.

  5. Directions on use are in the meta box.

I hope this is easy to follow. If you have any questions ask them in the comments.

Best,

Tim

PLUGIN DIRECTORY STRUCTURE:

plugin-shell > plugin-shell.php
plugin-shell > assets > css > admin > style.css
plugin-shell > assets > css > frontend > style.css

PLUGIN CODE:

<?php

/*
   Plugin Name: PluginShell
   Plugin URI: http://www.pluginshell.com/
   Description: Aenean Vestibulum Risus Commodo Ullamcorper
   Author: PluginShell
   Author URI: http://www.pluginshell.com/
   Version: 0.0.0
*/

mb_internal_encoding("UTF-8");
class Plugin_Shell {
   /**
    *
    * CONSTRUCTOR
    * Adds all actions and filters to the class
    *
    * @since 0.0.0
    *
    **/ 
    function __construct() {
        //Actions
        add_action( 'init',                  array( &$this, 'plugin_shell_register_post_type'       ) );
        add_action( 'admin_init',            array( &$this, 'plugin_shell_register_and_build_fields') );
        add_action( 'wp_enqueue_scripts',    array( &$this, 'plugin_shell_enqueue_style'            ) );
        add_action( 'admin_enqueue_scripts', array( &$this, 'plugin_shell_enqueue_options_style'    ) );
        add_action( 'admin_menu',            array( &$this, 'plugin_shell_add_meta_boxes'           ) );
        add_action( 'admin_menu',            array( &$this, 'plugin_shell_options_page'             ) );
        add_action( 'add_meta_boxes',        array( &$this, 'plugin_shell_add_contextable_meta_box' ) );
        add_action( 'save_post',             array( &$this, 'plugin_shell_meta_box_save' ), 1, 2      );
        add_action( 'edit_form_after_editor',array( &$this, 'add_after_editor_meta_boxes'           ) );
        add_action( 'edit_form_after_title', array( &$this, 'add_after_title_meta_boxes'            ) );

    }

   /**
    *
    * PHP4 CONSTRUCTOR
    * Calls __construct to create class
    *
    * @since 0.0.0
    *
    **/ 
    function plugin_shell() {
        $this->__construct();
    }

   /**
    *
    * LOAD CSS INTO THE WEBSITE'S FRONT END
    *
    * @since 0.0.0
    *
    **/ 
    function plugin_shell_enqueue_style() {
        wp_register_style( 'plugin-shell-style',  plugin_dir_url( __FILE__ ) . 'assets/css/frontend/style.css' );
        wp_enqueue_style( 'plugin-shell-style' ); 
    }

   /**
    *
    * LOAD CSS INTO THE ADMIN PAGE
    *
    * @since 0.0.0
    *
    **/ 
    function plugin_shell_enqueue_options_style() {
        wp_register_style( 'plugin-shell-options-style',  plugin_dir_url( __FILE__ ) . 'assets/css/admin/style.css' );
        wp_enqueue_style( 'plugin-shell-options-style' ); 
    }

   /**
    * Register meta box context location: after_editor
    *
    * @return null
    **/
    function add_after_editor_meta_boxes() {
        global $post, $wp_meta_boxes;
        # Output the `after_editor` meta boxes:
        do_meta_boxes( get_current_screen(), 'after_editor', $post );
    }

   /**
    * Register meta box context location: after_title
    *
    * @return null
    **/
    function add_after_title_meta_boxes() {
        global $post, $wp_meta_boxes;
        # Output the `after_title` meta boxes:
        do_meta_boxes( get_current_screen(), 'after_title', $post );
    }

   /**
    *
    * REGISTER CUSTOM POST TYPE: plugin_shell
    *
    * @since 0.0.0
    *
    **/
    function plugin_shell_register_post_type() {
        $options = get_option('plugin_shell_options');

        register_post_type( 'plugin_shell',
            array(
                'labels' => array(
                    'name' => __( 'Plugin Shell' ),
                    'singular_name' => __( 'term' ),
                    'add_new' => __( 'Add New' ),
                    'add_new_item' => __( 'Add New Term' ),
                    'edit' => __( 'Edit' ),
                    'edit_item' => __( 'Edit Term' ),
                    'new_item' => __( 'New Term' ),
                    'view' => __( 'View Term' ),
                    'view_item' => __( 'View Term' ),
                    'search_items' => __( 'Search Term' ),
                    'not_found' => __( 'No Terms found' ),
                    'not_found_in_trash' => __( 'No Terms found in Trash' )
                ),
                'public' => true,
                'query_var' => true,
                'show_in_menu' => true,
                'show_ui' => true,
                'menu_icon' => 'dashicons-book-alt',
                'supports' => array( 'title', 'editor' ),
                'rewrite' => array( 'slug' => $options['plugin_shell_slug_url_setting'] ? get_post($options['plugin_shell_slug_url_setting'])->post_name : 'plugin-shell', 'with_front' => false )
            )
        );
    }

/*********************************************************
    BEGIN: CONTEXTABLE META BOX
 *********************************************************/

   /**
    *
    * CALLS ALL OF THE FUNCTIONS RESPONSIBLE FOR RENDERING THE CONTEXTABLE PLUGIN SHELL META BOX
    *
    * @since 0.0.0
    *
    **/
    function plugin_shell_add_contextable_meta_box() {
        $this->_plugin_shell_add_contextable();
    }

   /**
    *
    * RENDERS THE META BOX 
    * Responsible for allowing the user to enter the plugin shell term.
    *
    * @since 0.0.0
    *
    **/
    function _plugin_shell_add_contextable() {
        add_meta_box( 
            'contextable-meta-box', 
            __( 'Extended Context Meta Box', 'pluginshell-textdomain' ), 
            array( &$this, '_display_contextable_meta_box' ), 
            'plugin_shell', // CHANGE TO DESIRED post-type
            'after_title', // CHANGE THIS TO 'after_editor' || 'after_title' || OR OTHER VALID CONTEXT LOCATION
            'default' 
        );
    }

   /**
    * 
    * DISPLAYS THE CONTENTS OF THE PLUGIN SHELL TERM META BOX
    *
    * @since 0.0.0
    *
    **/ 
    function _display_contextable_meta_box() {
        printf( '<em>Move this meta box with the extended context values</em><br /><strong>after_title</strong><br /><strong>after_editor</strong><br />&nbsp;<br />To dettach the meta box to the bottom of the editor open <em>assets/css/admin/style.css</em><br />&nbsp;<br />Add the style:<br /><strong>#after_editor-sortables {</strong><br /><strong>
&nbsp;&nbsp;&nbsp;&nbsp;padding-top: 40px;</strong><br />
<strong>}</strong><br />&nbsp;<br />See <strong>comments</strong> in <em>function _plugin_shell_add_contextable()</em>' );
    }

/*********************************************************
    END: CONTEXTABLE META BOX
 *********************************************************/

   /**
    * 
    * CALLS ALL OF THE FUNCTIONS RESPONSIBLE FOR RENDERING THE PLUGIN SHELL META BOX
    *
    * @since 0.0.0
    *
    **/
    function plugin_shell_add_meta_boxes() {
        $this->_plugin_shell_add_meta_box();
    }

   /**
    *
    * RENDERS THE META BOX 
    * Responsible for allowing the user to enter the plugin shell term.
    *
    * @since 0.0.0
    *
    **/
    function _plugin_shell_add_meta_box() {
        add_meta_box( 'plugin_shell', __('Plugin Shell Extra Meta Box', 'pluginshell-textdomain'), array( &$this, '_plugin_shell_definiton_meta_box' ), 'plugin_shell', 'normal', 'high' );
    }

   /**
    * 
    * DISPLAYS THE CONTENTS OF THE PLUGIN SHELL TERM META BOX
    *
    * @since 0.0.0
    *
    **/ 
    function _plugin_shell_definiton_meta_box() {
        ?>
        <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Nulla vitae elit libero, a pharetra augue. Morbi leo risus, porta ac consectetur ac, vestibulum at eros. Integer posuere erat a ante venenatis dapibus posuere velit aliquet. Nullam id dolor id nibh ultricies vehicula ut id elit. Donec id elit non mi porta gravida at eget metus.</p>

<p>Praesent commodo cursus magna, vel scelerisque nisl consectetur et. Nullam quis risus eget urna mollis ornare vel eu leo. Vestibulum id ligula porta felis euismod semper. Curabitur blandit tempus porttitor. Duis mollis, est non commodo luctus, nisi erat porttitor ligula, eget lacinia odio sem nec elit.</p>

<p>Vestibulum id ligula porta felis euismod semper. Cras mattis consectetur purus sit amet fermentum. Vestibulum id ligula porta felis euismod semper. Vestibulum id ligula porta felis euismod semper. Fusce dapibus, tellus ac cursus commodo, tortor mauris condimentum nibh, ut fermentum massa justo sit amet risus. Vivamus sagittis lacus vel augue laoreet rutrum faucibus dolor auctor.</p>
<?php
    }

   /**
    * 
    * SAVES PLUGIN SHELL TERM
    *
    * @since 0.0.0
    *
    **/
    function plugin_shell_meta_box_save( $post_id, $post ) {
        $key = '_plugin_shell_term';

        //  verify the nonce
        if ( !isset($_POST['_plugin_shell_nonce']) || !wp_verify_nonce( $_POST['_plugin_shell_nonce'], plugin_basename(__FILE__) ) )
            return;

        //  don't try to save the data under autosave, ajax, or future post.
        if ( defined('DOING_AUTOSAVE') && DOING_AUTOSAVE ) return;
        if ( defined('DOING_AJAX') && DOING_AJAX ) return;
        if ( defined('DOING_CRON') && DOING_CRON ) return;

        //  is the user allowed to edit the URL?
        if ( ! current_user_can( 'edit_posts' ) || $post->post_type != 'plugin_shell' )
            return;

        $value = isset( $_POST[$key] ) ? $_POST[$key] : '';

        if ( $value ) {
            //  save/update
            $my_post = array();
            update_post_meta($post->ID, $key, $value);
        } else {
            //  delete if blank
            delete_post_meta($post->ID, $key);
        }

    }

   /**
    *
    * ADMIN MENU AND
    * SETTING FEILDS
    *
    **/

   /**
    * 
    * BUILD THE SETTINGS OPTIONS PAGE
    *
    * @since 0.0.0
    *
    **/
    function _plugin_shell_build_options_page() { 
        ?>
        <div id="plugin-shell-options-wrap">
            <div class="icon32" id="icon-tools"> <br /> </div>
            <form method="post" action="options.php" enctype="multipart/form-data">
                <?php settings_fields('plugin_shell_option_group'); ?>
                <?php do_settings_sections(__FILE__); ?>
                <p class="submit">
                    <input name="Submit" type="submit" class="button-primary" value="<?php esc_attr_e('Save Changes'); ?>" />
                </p>
            </form>
            <p><hr></p>
            <p>* After editing the Slug the Permalinks Settings must be saved again.</p>
        </div>
        <?php 
    }

   /**
    * 
    * REGISTER SETTINGS AND FIELDS FOR OPTIONS PAGE
    *
    * @since 0.0.0
    *
    **/
    function plugin_shell_register_and_build_fields() {
        register_setting('plugin_shell_option_group', 'plugin_shell_option_mainpage', 'plugin_shell_validate_setting');
        add_settings_section('plugin_shell_settings_general', 'Plugin Shell General Settings', array( &$this, '_plugin_shell_settings_fields'), __FILE__);
    }

   /**
    * 
    * RENDER THE SLUG SELECTION SETTINGS FIELD
    *
    * @since 0.9.2
    *
    **/
    function _plugin_shell_settings_fields() {
        add_settings_field('slug', 'Mainpage:', array( &$this, '_plugin_shell_slug_url_setting'), __FILE__, 'plugin_shell_settings_general');
    } 

   /**
    * 
    * SANITIZE OPTIONS
    *
    * @since 0.0.0
    *
    **/
    function plugin_shell_validate_setting($plugin_shell_options) {
        return $plugin_shell_options;
    }


   /**
    * 
    * GET DROPDOWN OF ALL PAGES FOR SLUG SETTING OPTION
    *
    * @since 0.0.0
    *
    **/
    function _plugin_shell_slug_url_setting() {
        $options = get_option('plugin_shell_options');
        wp_dropdown_pages(array('name' => 'theme_options[plugin_shell_slug_url_setting]', 'selected' => $options['plugin_shell_slug_url_setting'] ));
    }

   /**
    * 
    * ADD THE OPTIONS PAGE
    *
    * @since 0.0.0
    *
    **/
    function plugin_shell_options_page() { 
        add_options_page('Plugin Shell', 'Plugin Shell', 'administrator', __FILE__, array( &$this, '_plugin_shell_build_options_page' ) );
    }
};

/**
 * 
 * INSTANTIATE CLASS plugin_shell
 *
 * @since 0.0.0
 *
 **/
$PluginShell = new plugin_shell;

?>
1
  • I haven't tried this code in-full but I was able to use a few lines to improve my own plugin. TYVM
    – TonyG
    Commented Oct 25, 2021 at 23:00
0

these metaboxes appears as normal because add_meta_box() adds them in the loop of the others metaboxes.
and to use a real metaboxe fully functional (in order to hide / show it and move it arround the page) you have to put the metabox in the loop.

so the last time I had to put a meta box before the editor, I choose to hide the default editor (in the properties of the post type) and to add another metaboxe with a new editor in it with wp_editor() http://codex.wordpress.org/Function_Reference/wp_editor.

1
  • Yes, I have done that too. However, I actually have need to force something immediately below the editor. Since you don't know what other plugins could be placing meta boxes on the page the only option for getting something there is to use the add_after_editor_meta_boxes hook. Which led me to also looking at add_after_title_meta_boxes. Regarding the priority, I have tried "low", "normal", "high". None had any effect. Commented Mar 16, 2016 at 7:48

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.