1

I have researched many tutorials on how to create/add Custom Meta Boxes using the add_meta_box() Wordpress function. I can not find any example where the finished Meta Box has the option for an Admin User to input both key/value pairs like Custom_Fields?

EDIT: Removed plugin link URL as not enough rep points:

I want to add this capability to my own Custom Meta Boxes and to the WebDevStudios/Custom-Metaboxes-and-Fields-for-WordPress plugin's Meta Box fields.

To clarify, I want to create a Meta Box that Admin Users can add Name and Value to - like is shown in this image taken from the WP's own Custom Fields Meta Box :

enter image description here

Edit 19.06.14:

My understanding of Custom Fields was incorrect, when querying on the front end:

        printf( '<pre>%s</pre>', var_export( get_post_meta($post->ID,"my_custom_filed_key"), true ) );

I see that my_custom_field_key produces an index array:

array (
0 => 'val  1',
1 => 'val  2',
2 => 'val  3',
)

I suppose what I am trying to achieve is an associative array?

array (
'my key name 1' => 'my val 1',
'my key name 2' => 'my val 2',
'my key name 3' => 'my val 3',
)

Here is the add_meta_box function part of my code:

function my_meta_boxes($post) {
        add_meta_box(
            'my_meta',          // Unique ID
            esc_html__( 'Reviews', 'example' ), // Title
            'my_meta_box',  // Callback function
            'my-post',          // Admin page (or post type)
            'side',               // Context
            'default'             // Priority
        );
 }

/* Display the post meta box. */
function my_meta_box() { 
        wp_nonce_field( basename( __FILE__ ), 'my_post_nonce' ); 

        $meta = get_post_custom($post->ID);
        $meta['my_meta'] = maybe_unserialize($meta['my_meta'][0]);
        ?>
        <table style="width:100%;">
                <tr style="width:100%;">
                <td><textarea style="width:90%;" rows="1" name="my_meta[]"> <?php echo $meta['my_meta'][0] ?></textarea></td>
                </tr>
                <tr style="width:100%;">
                <td><textarea style="width:90%;" rows="1" name="my_meta[]"> <?php echo $meta['my_meta'][1] ?></textarea></td>
                </tr>
                <tr style="width:100%;">
                <td><textarea style="width:90%;" rows="1" name="my_meta[]"> <?php echo $meta['my_meta'][2] ?></textarea></td>
                </tr>
        </table>
<?php }

I am wondering if the add_meta_box $callbacks_args would help me here, but I am yet to full understand it?

Thank you.

  • Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking. See the How to Ask page for help clarifying this question. – engelen Jun 15 '14 at 11:21
  • Thanks, I have edited out text from my post which may not have been needed and have added a visual image of the Name/Value fields I am trying to create. I hope that I am understood now? – user1575949 Jun 15 '14 at 11:55
  • I understand what you need now, but I don't know what you've tried yourself and what went wrong :-). You're unlikely to get any real help if you haven't tried it yourself. – engelen Jun 16 '14 at 7:38
  • Really? I see many examples where a poster receives 'real help' with less effort: minimal post with help Not that I do not agree with the general benefits of self-help ;) I am looking in a new direction to try and achieve my goal - as per post edits. – user1575949 Jun 19 '14 at 9:09
  • @user1575949 Different sites have a different understanding and agreement of the minimum needed quality and prerequisites for questions. The one you are currently looking at has a very high expectation. Thanks for your edit and for respect for the expected quality. – kaiser Jun 19 '14 at 9:22
1

To help you understanding how forms work: They add to the $_POST array by a form fields name argument.

<input type="text" name="foo" value="Fooo!" />

would produce

$_POST (array) =>
    foo => Fooo!

while

<input type="text" name="foo[bar]" value="Bar." />
<input type="text" name="foo[baz]" value="Baz." />

would produce

$_POST (array) =>
    foo => (array) =>
        bar => Bar.
        baz => Baz.

and so on.

  • Thanks for your help. I had thought it would be possible for an admin user to input text into the name attribute and the value attribute, then I would output both key and value of the input tag blush. – user1575949 Jun 25 '14 at 14:23

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.