1

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,

     ),
);
13
  • 1
    What do you mean by "update the $title? What keeps you from changing that static content within that static array? Commented May 13, 2019 at 7:51
  • @NicoHaase he needs to update it dynamically Commented May 13, 2019 at 7:52
  • That looks strange to me - there is nothing dynamic in that code Commented May 13, 2019 at 7:52
  • I have a file with a bunch of metaboxes but i'm trying to reduce the duplicate entries That single metabox is repeated through the file in multiple places, I don't want to keep duplicating that in the meta_boxes[] array below. So want to create the single field once and update some simple fields. @NicoHaase Commented May 13, 2019 at 7:55
  • 2
    If you need to pull the title and populate it into the fields, use a loop to populate it. Commented May 13, 2019 at 7:58

3 Answers 3

2

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.

Sign up to request clarification or add additional context in comments.

7 Comments

Can you explain further how that single line updates something?
Well, of course you need to declare $newTitle. But this single line of code would replace the current value "{$title}" given to the 'name' field with the value given to the $newTitle variable.
And what should one do with that variable afterwards? How can it be used to run a replacement like given in the question?
I see I made a mistake in my answer, not setting the title like the OP asked. I changed the answer so that it should work. Thank you for pointing this out.
As far as I understand the question, there should be some kind of "template array" - that's why there is a value {$title} which should get replaced with the content of the current array's element title
|
1

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

$meta_boxes[$i]['title'] = 'NEW TITLE';

3 Comments

I do understand how arrays work @DarkBee, but I still don't get why I need the [$i]. Also Would this update the title from within the array? I feel like this would update the $title outside of the meta_box[] array(), no?
Because you have multiple metabox in the array metaboxes? You'd u need to target the right metabox of which you want to update the title
@Aaron as far as I can see you have a multi dimensional array, that's why I added the [$i] so here you are ordering each title of each element
-1

You can do something like

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

1 Comment

This won't work as the default behavior of a foreach on an array is to copy the array

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.