Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to access the 'value' keys in multi-dimensional array?
    primarykey
    data
    text
    <p>I am creating multiple meta boxes in WordPress, and I want the user to be able to toggle certain boxes on or off.</p> <p>So, they click a radio button (in a separate meta box), update the options, and an additional meta box attached to that radio value will appear.</p> <p>So it basically goes from one meta box (the selector box) to two meta boxes (the selector box, AND the new box they just selected).</p> <p>In my code, you can create a meta box by setting something like this up (this is the radio box where the user selects for other meta boxes to toggle on/off):</p> <pre><code>// Create meta box that gives user ability to select additional meta box, // which will then show upon saving the post $meta_boxes[] = array( 'id' =&gt; 'meta_box_id', 'title' =&gt; 'Box title', 'pages' =&gt; array('page'), 'context' =&gt; 'side', 'priority' =&gt; 'low', 'fields' =&gt; array( array( 'name' =&gt; 'Select:', 'id' =&gt; $prefix . 'meta_box_id', 'type' =&gt; 'radio', 'options' =&gt; array( array('name' =&gt; 'Value 1', 'value' =&gt; 'value_one'), array('name' =&gt; 'Value 2', 'value' =&gt; 'value_two'), ) ) ) ); </code></pre> <p>Here is what it looks like in WordPress:</p> <p><img src="https://i.stack.imgur.com/xR69s.png" alt="Box title in WordPress"></p> <p>And here is another meta box that, once selected in the meta box above (let's say value_one is selected), will appear on the post screen:</p> <pre><code>// This meta box will only show if 'value_one' is selected from the radio box above $meta_boxes[] = array( 'id' =&gt; 'standard_lead', 'title' =&gt; 'Standard Lead', 'pages' =&gt; array('page'), 'context' =&gt; 'normal', 'priority' =&gt; 'high', 'lead' =&gt; 'value_one', 'fields' =&gt; array( array( 'type' =&gt; 'text', 'id' =&gt; $prefix . 'standard_title', 'name' =&gt; 'Lead Title', 'desc' =&gt; 'The title of your Standard Lead', 'width' =&gt; '100' ), array( 'type' =&gt; 'textarea', 'id' =&gt; $prefix . 'standard_content', 'name' =&gt; 'Lead Content', 'desc' =&gt; 'The content of your Standard Lead (you can use HTML)', 'width' =&gt; '100' ) ) ); </code></pre> <p>The important piece from that code is this:</p> <pre><code>'lead' =&gt; 'value_one', </code></pre> <p>My plan was to have the ['lead'] value (from the meta box code directly above) to match the ['value'] value (from the radio meta box), so they could be connected, and then test with an IF statement to make sure they equal the same thing, and then display ONLY if they both equal value_one.</p> <p>The function below is what actually adds the meta boxes into WordPress. Within that function, I tried creating this IF statement to match those two together:</p> <pre><code>if($this-&gt;_meta_box['value'] == $this-&gt;_meta_box['lead']) </code></pre> <p>but it doesn't work, and I'm not sure how to target ['value'] as it's nested within multiple arrays (or I assume, is the problem).</p> <p>Here's the full function:</p> <pre><code>function add_meta_boxes() { $this-&gt;_meta_box['context'] = empty($this-&gt;_meta_box['context']) ? 'normal' : $this-&gt;_meta_box['context']; $this-&gt;_meta_box['priority'] = empty($this-&gt;_meta_box['priority']) ? 'high' : $this-&gt;_meta_box['priority']; foreach($this-&gt;_meta_box['pages'] as $page) { if($this-&gt;_meta_box['value'] == $this-&gt;_meta_box['lead']) { // adds meta box to WP add_meta_box($this-&gt;_meta_box['id'], $this-&gt;_meta_box['title'], array(&amp;$this, 'show_meta_boxes'), $page, $this-&gt;_meta_box['context'], $this-&gt;_meta_box['priority']); } } } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload