将摘录移动到管理员中的帖子内容的正下方

时间:2015-01-15 作者:iltdev

有时,由于插件添加了元框,摘录会在管理页面中按顺序向下移动。

有没有办法让它总是直接位于帖子内容的下方?

以下是我尝试的方式:

function remove_wordpress_meta_boxes() {
   remove_meta_box( \'postexcerpt\', \'page\', \'normal\' );
   add_meta_box(\'postexcerpt\', __(\'Excerpt\'), \'post_excerpt_meta_box\', \'page\', \'normal\', \'high\');
}
add_action( \'admin_menu\', \'remove_wordpress_meta_boxes\' );
remove_meta_box() 按预期工作,但重新添加摘录元框似乎对排序没有任何作用-我仍然有一个插件元框出现在摘录上方。

2 个回复
最合适的回答,由SO网友:birgire 整理而成

代码不起作用的原因很可能是您之前已经对元数据库进行了排序,并且该顺序已保存到meta-box-order_page 元值。这将覆盖默认设置。

下面是meta-box-order_post 元值:

a:3:{
     s:4:"side";    
         s:61:"submitdiv,formatdiv,categorydiv,tagsdiv-post_tag,postimagediv";
     s:6:"normal";  
         s:96:"revisionsdiv,postexcerpt,trackbacksdiv,postcustom,
               commentstatusdiv,commentsdiv,slugdiv,authordiv";
     s:8:"advanced";
         s:0:"";
}
这里,我刚刚重新格式化了序列化数组,以提高可读性。

粘性元盒:

要将给定元盒移动到顶部位置,对于给定的上下文和post类型,可以使用以下代码段:

/**
 * Set some sticky metaboxes
 */

add_action( \'admin_init\', function()
{
    if( function_exists( \'wpse_sticky_metabox\' ) )
    {
         // Sticky metabox #1:
         wpse_sticky_metabox(
             array(
                 \'cpt\'      => \'page\',
                 \'context\'  => \'normal\',
                 \'metabox\'  => \'postexcerpt\'
             )
         );

         // Sticky metabox #2:
         wpse_sticky_metabox(
             array(
                 \'cpt\'      => \'post\',
                 \'context\'  => \'side\',
                 \'metabox\'  => \'authordiv\' 
             )
         );
    }
});
您可以根据自己的需要进行调整。

有关输入参数的一些信息:

  • cpt 是编辑屏幕的自定义贴子类型(即贴子、页面……)
  • context 是要使元数据库粘滞的部分。(即正常、侧面、高级、…)
  • metabox 是粘滞元框的id(即PostExtract、authordiv、…)

粘性元盒-插件:

以下演示插件支持此功能:

/**
 * Plugin Name: Sticky Meta-Boxes
 * Description: Set a given meta-box to the top, for a given cpt and context.
 * Plugin URI:  http://wordpress.stackexchange.com/a/174980/26350
 * Author:      Birgir Erlendsson (birgire)
 * Version:     0.0.2
 */

function wpse_sticky_metabox( $args = array() )
{
    if( class_exists( \'MetaBoxSticker\' ) )
    {
        $o = new MetaBoxSticker;
        $o->setup( $args )->activate();
    }
}

class MetaBoxSticker
{
    private $args;

    public function setup( $args = array() )
    {
        $default = array(
            \'cpt\'      => \'post\',
            \'context\'  => \'normal\',
            \'metabox\'  => \'postexcerpt\'
        );
        $this->args = wp_parse_args( $args, $default );
        return $this;
    }

    public function activate()
    {
        add_filter(
            sprintf(
                \'get_user_option_meta-box-order_%s\',
                $this->args[\'cpt\']
            ),
            array( $this, \'filter\' ),
            PHP_INT_MAX
        );

        add_action( 
            \'add_meta_boxes\', 
            array( $this, \'relocate\' ), 
            PHP_INT_MAX 
        );
    }

    public function relocate()
    {
        //-----------------------
        // Get the user input:
        //-----------------------
        $_cpt      = sanitize_key( $this->args[\'cpt\']     );
        $_metabox  = sanitize_key( $this->args[\'metabox\'] );
        $_context  = sanitize_key( $this->args[\'context\'] );

        //-----------------------
        // Relocate \'high\' metaboxes to \'default\' in the current context
        //-----------------------                  
        global $wp_meta_boxes;
        if( isset( $wp_meta_boxes[$_cpt][$_context][\'high\'] ) )
        {                                                           
            foreach( $wp_meta_boxes[$_cpt][$_context][\'high\'] as $id => $item )
            {
                if( isset( $item[\'callback\'] ) )
                {
                    remove_meta_box( 
                        $id, 
                        $_cpt, 
                        $_context 
                    );

                    add_meta_box( 
                        $id, 
                        $item[\'title\'], 
                        $item[\'callback\'], 
                        $_cpt, 
                        $_context, 
                        \'default\', 
                        $item[\'args\'] 
                    );
                }
            }
        }
    }

    public function filter( $order )
    {
        //-----------------------
        // Get the user input:
        //-----------------------                               
        $_cpt      = sanitize_key( $this->args[\'cpt\']     );
        $_metabox  = sanitize_key( $this->args[\'metabox\'] );
        $_context  = sanitize_key( $this->args[\'context\'] );

        //-----------------------
        // Handle the case if the current user hasn\'t made any meta-box ordering before:
        //-----------------------
        if( empty( $order ) )
        {
            global $wp_meta_boxes;
            if( ! isset( $wp_meta_boxes[$_cpt][$_context] ) )
               return $order;

            $order = array();
            foreach( $wp_meta_boxes[$_cpt] as $context_key => $context_item )
            {
                $tmp = array();
                foreach( $context_item as $priority_key => $priority_item )
                {
                    foreach( $priority_item as $metabox_key => $metabox_item )
                    {
                        $tmp[] = $metabox_key;
                    }
                }
                $order[$context_key] = join( \',\', $tmp );
            }
        }

        //-----------------------
        // Let\'s make sure the context exists:
        //-----------------------
        if( ! isset( $order[$_context] ) )
            return $order;

        //-----------------------
        // Remove the given meta-box from the whole order array:
        //-----------------------
        foreach( $order as $context_key => $string )
        {
            $tmp = explode( \',\', $string );
            $key = array_search( $_metabox, $tmp );
            if( ! empty( $key ) )
            {
                unset( $tmp[$key] );
                $order[$context_key] = join( \',\', $tmp );
            }
        }

        //-----------------------
        // Make the given meta-box sticky for a given context
        //-----------------------
        $tmp = explode( \',\', $order[$_context] );
        array_unshift( $tmp, $_metabox );
        $order[$_context] = join( \',\', $tmp );

        return $order;
    }

} // end class
这个插件也应该可以工作,即使你以前没有订购过。

它还尊重屏幕选项,即元框是否可见。

我希望你能进一步满足你的需要。

SO网友:OzTheGreat

正如所指出的那样,您只能更改默认顺序,如果用户修改了它,那么不编写重置脚本,您就无法完成大量操作。

不管怎样,下面是如何正确更改默认顺序的方法(免责声明,我写了这篇文章)https://ozthegreat.io/wordpress/wordpress-how-to-move-the-excerpt-meta-box-above-the-editor/

/**
 * Removes the regular excerpt box. We\'re not getting rid
 * of it, we\'re just moving it above the wysiwyg editor
 *
 * @return null
 */
function oz_remove_normal_excerpt() {
    remove_meta_box( \'postexcerpt\' , \'post\' , \'normal\' );
}
add_action( \'admin_menu\' , \'oz_remove_normal_excerpt\' );

/**
 * Add the excerpt meta box back in with a custom screen location
 *
 * @param  string $post_type
 * @return null
 */
function oz_add_excerpt_meta_box( $post_type ) {
    if ( in_array( $post_type, array( \'post\', \'page\' ) ) ) {
        add_meta_box(
            \'oz_postexcerpt\',
            __( \'Excerpt\', \'thetab-theme\' ),
            \'post_excerpt_meta_box\',
            $post_type,
            \'after_title\',
            \'high\'
        );
    }
}
add_action( \'add_meta_boxes\', \'oz_add_excerpt_meta_box\' );

/**
 * You can\'t actually add meta boxes after the title by default in WP so
 * we\'re being cheeky. We\'ve registered our own meta box position
 * `after_title` onto which we\'ve regiestered our new meta boxes and
 * are now calling them in the `edit_form_after_title` hook which is run
 * after the post tile box is displayed.
 *
 * @return null
 */
function oz_run_after_title_meta_boxes() {
    global $post, $wp_meta_boxes;
    # Output the `below_title` meta boxes:
    do_meta_boxes( get_current_screen(), \'after_title\', $post );
}
add_action( \'edit_form_after_title\', \'oz_run_after_title_meta_boxes\' );

结束