admin管理员组

文章数量:1279042

I am trying to add classes to my metabox. How could I apply this to both post and pages? $posts variable? Do I need to add filter twice, once for each post type?

    $id = 'my-metabox-api';
    $posts = array('post', 'page');
    add_meta_box($id, 'My Video Player', 'add_metabox_api', $posts, 'normal', 'low'); 

    add_filter( "postbox_classes_{$posts}_{$id}", 'my_custom_mb' );

function my_custom_mb( $classes ) {
    array_push( $classes, 'closed' );
    return $classes;
}

I am trying to add classes to my metabox. How could I apply this to both post and pages? $posts variable? Do I need to add filter twice, once for each post type?

    $id = 'my-metabox-api';
    $posts = array('post', 'page');
    add_meta_box($id, 'My Video Player', 'add_metabox_api', $posts, 'normal', 'low'); 

    add_filter( "postbox_classes_{$posts}_{$id}", 'my_custom_mb' );

function my_custom_mb( $classes ) {
    array_push( $classes, 'closed' );
    return $classes;
}
Share Improve this question asked Nov 6, 2021 at 20:55 ToniqToniq 4476 silver badges15 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 0

Do I need to add filter twice, once for each post type?

Yes! you can't insert an array into a string, if you could it wouldn't result in a matching filter name anyway. The filters for posts and pages are different filters. You need to add them separately.

e.g. postbox_classes_post_my-metabox-api versus postbox_classes_page_my-metabox-api

If I had to guess, your current code results in this filter name: postbox_classes_array_my-metabox-api

本文标签: plugin developmentaddfilter postboxclasses multiple post types