I'm not sure how to describe this, so sorry if the title isn't correct for this question.

I have a file with a bunch of metaboxes but i'm trying to reduce the duplicate entries. So what I'm trying to do is create a variable containing an array which creates the metabox, then trying to update a variable within this array when I call it later on.

The example below i'm trying to update the $title but have no idea how to achieve this or what it's called to google a solution?

<?php
$metabox = array(
        'name' => "{$title}",
        'id'   => "column_content",
        'type' => 'wysiwyg',
        'raw'  => false,
        'options' => array(
            'textarea_rows' => 3,
            'teeny'         => true,
            'media_buttons' => false,
        ),
    );

$meta_boxes[] = array(
    'id'         => 'page_builder', 
    'title'      => 'Metabox Example',
    'fields'     => array(
        // Output the above array here:
        $metabox,

     ),
);

Update

I'm not sure I completely explained how I would like to achive this, I wanted to update the $title for the $metabox within the $meta_boxes array()

<?php
$metabox = array(
        'name' => "{$title}",
        'id'   => "column_content",
        'type' => 'wysiwyg',
        'raw'  => false,
        'options' => array(
            'textarea_rows' => 3,
            'teeny'         => true,
            'media_buttons' => false,
        ),
    );

$meta_boxes[] = array(
    'id'         => 'page_builder', 
    'title'      => 'Metabox Example',
    'fields'     => array(
        // Output the above array here and update the title on the fly:
        $title = 'NEW TITLE';
        $metabox,

     ),
);

3 Answers

2
Cody604 On Best Solutions

If I understand you correctly you could access the $title value ('name' field) like this:

<?php
$metabox = array(
        'name' => "initial title",
        'id'   => "column_content",
        'type' => 'wysiwyg',
        'raw'  => false,
        'options' => array(
            'textarea_rows' => 3,
            'teeny'         => true,
            'media_buttons' => false,
        ),
    );

$metaboxFields = array();

//this is an example for 5 metaboxes.
for ($i = 1; $i <= 5; $i++) {
    $metaboxFields['metabox'.$i] = $metabox;
}   


$meta_boxes = array(
    'id'         => 'page_builder', 
    'title'      => 'Metabox Example',
    'fields'     => $metaboxFields,
);

$newTitleForBox1 = 'NEW TITLE';
$meta_boxes['fields']['metabox1']["name"] = $newTitleForBox1;

This will access the "name" field in metabox1 in the "fields" array of the first item in the "$meta_boxes" array.

Though of course this is not a very pretty solution.

1
eBourgess On

It's actually easy, all you need to do is add this to your code.

$meta_boxes[$i]['title'] = 'NEW TITLE';
-1
Jalil On

You can do something like

foreach ($metabox as $box){
    $box['title'] = 'yourValue';
    //here you can change the values that you want
}