最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

functions - Auto close (hide) custom metaboxset default state

programmeradmin1浏览0评论

I am using the WordPress function add_meta_box() to add my own custom metabox.

So how can I auto close/hide on page open ?

Currently, I just add a CSS class closed to metabox's <div> element via jQuery like so:

HTML : (example)

<div id="my_metabox" class="postbox"></div>

jQuery :

$("#my_metabox.postbox").addClass("closed");

But is there a WordPress function to do this ?

I am using the WordPress function add_meta_box() to add my own custom metabox.

So how can I auto close/hide on page open ?

Currently, I just add a CSS class closed to metabox's <div> element via jQuery like so:

HTML : (example)

<div id="my_metabox" class="postbox"></div>

jQuery :

$("#my_metabox.postbox").addClass("closed");

But is there a WordPress function to do this ?

Share Improve this question edited Apr 3, 2013 at 10:46 Johannes P. 11.1k3 gold badges42 silver badges53 bronze badges asked Apr 3, 2013 at 10:25 l2aelbal2aelba 85715 silver badges29 bronze badges
Add a comment  | 

3 Answers 3

Reset to default 8

Hook into postbox_classes. postbox_classes is the function which will output the classes for the metabox.

apply_filters( "postbox_classes_{$page}_{$id}", $classes )

Your code could look like this:

add_action( 'add_meta_boxes', 'add_my_metabox' );

function add_my_metabox() {
  $id       = 'my-metabox';
  $title    = 'My Metabox';
  $callback = 'my_metabox_content';
  $page     = 'post';

  add_meta_box( $id, $title, $callback, $page );

  add_filter( "postbox_classes_{$page}_{$id}", 'minify_my_metabox' );
}

function my_metabox_content() { ... }


/*
 * $classes is an array
 */
function minify_my_metabox( $classes ) {
  if ( isset( $_POST['my_condition'] ) && 'my_condition' == $_POST['my_condition'] ) {
    array_push( $classes, 'closed' );
  }

  return $classes;
}

The toggling of a metaboxes' open/close state is done via JS by WP as well - obviously so, since it happens on the client side in his/her browser.

The script that does this goes by the handle 'postbox' *

That script does the same thing you do manually (adds a closed class to close metaboxes). Also, it checks the state of a metabox via if(e.hasClass("closed")).
Hence your way of setting the default state is totally fine.

* As an aside: i.e. should you ever need this functionality on custom admin pages, that's what needs to be enqueued ( wp_enqueue_scripts( 'postbox' ); )

The first hook fires when you are editing a post. The second hook when you are adding a new post. This code will automatically close the SEO meta box.

add_action( 'admin_head-post.php', 'closed_fields_meta_boxes_admin_head' );
add_action( 'admin_head-post-new.php',  'closed_fields_meta_boxes_admin_head' );
function closed_fields_meta_boxes_admin_head() {
    ?>

    <script type="text/javascript">
        (function($){
            $(document).ready(function(){
                $('#wpseo_meta').addClass('closed');
            });
        })(jQuery);
    </script>

    <?php
}
发布评论

评论列表(0)

  1. 暂无评论