How to ‘clone’ select metabox options with a callback function?

The question:

Here is my metabox

      array(
        'id'    => 'all_btns',
        'name'  => 'Button Select',
        'type'  => 'select',
        'options' => array(
            'button'         => 'Button',
            'button_red'     => 'Button Red',
            'button_yellow'  => 'Button Yellow',

        ),             
        'callback' => 'metabox_clone_options',
      ),

I want to clone this options to another metabox array

The Solutions:

Below are the methods you can try. The first solution is probably the best. Try others if the first one doesn’t work. Senior developers aren’t just copying/pasting – they read the methods carefully & apply them wisely to each case.

Method 1

Add a simple function in your functions.php file.

function get_button_styles(){
    return array(
        'button'         => 'Button',
        'button_red'     => 'Button Red',
        'button_yellow'  => 'Button Yellow',

    );
}

Use it to get button styles in different metabox fields

array(
    'id'    => 'all_btns',
    'name'  => 'Button Select',
    'type'  => 'select',
    'options' => get_button_styles(),             
    'callback' => 'metabox_clone_options',
),


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

Leave a Comment