justintadlock / butterbean

A neat little post meta framework.
GNU General Public License v2.0
204 stars 31 forks source link

Add metabox via filter #39

Closed nicolas92 closed 7 years ago

nicolas92 commented 7 years ago

Hello, first of all, thank you for creating butterbean, it is very simple to understand and manipulate :) I have a question, is it possible to add a metabox with a filter? I would like to allow to add the metabox to any post type wanted via a child theme.

I tried this code but the metabox appears only in posts:

$this->post_types = apply_filters( 'prefix_metabox', array(
    'post'         => 'post',
    'page'        => 'page',
    'product'    => 'product',
) );

foreach( $this->post_types as $key => $val ) {

    if ( $key !== $post_type ) {
        return;
    }

    $butterbean->register_manager(
        'settings',
        array(
            'label'     => esc_html__( 'Settings', 'textdomain' ),
            'post_type' => $key,
            'context'   => 'normal',
            'priority'  => 'high'
        )
    );

    // Gets the manager object we want to add controls to.
    $manager = $butterbean->get_manager( 'settings' );

    // Register section.
    $manager->register_section(
        'section',
        array(
            'label'         => esc_html__( 'Section', 'textdomain' ),
            'dashicon'      => 'dashicons-admin-generic'
        )
    );

    // Register control.
    $manager->register_control(
        'test',
        array(
            'label'         => esc_html__( 'Content Layout', 'textdomain' ),
            'type'          => 'select',
            'section'       => 'section',
            'choices'       => array(
                ''              => esc_html__( 'Default', 'textdomain' ),
                'option1' => esc_html__( 'Option1', 'textdomain' ),
            ),
        )
    );

    // Register setting.
    $manager->register_setting(
        'test',
        array( 'sanitize_callback' => 'sanitize_key' )
    );

}

Can you help me? Thank you a lot :)

justintadlock commented 7 years ago

This isn't really the place for support questions. There's the forum for that: http://themehybrid.com/board/topics

Nevertheless, here's a quick answer. ButterBean uses OOP principles and has less reliance on hooks. You'd want to get the manager object and manipulate its $post_type property with something like this in your plugin:

$butterbean->get_manager( 'your_manager_name' )->post_type[] = 'your_post_type';
nicolas92 commented 7 years ago

Okay, thank you very much :)