Input fields not (fully) filling in custom meta boxes

WordPress Event Management, Calendars & Registration Forums Frontend Submissions Input fields not (fully) filling in custom meta boxes

This topic contains 7 replies, has 2 voices, and was last updated by  Matthew Tarzwell 8 years, 3 months ago.

Viewing 8 posts - 1 through 8 (of 8 total)
  • Author
    Posts
  • #20346

    Hi Stephen,

    I’m using IP Metaboxes (https://en-ca.wordpress.org/plugins/ip-metaboxes/) to create extra meta boxes for the admin area – Contact Name, Phone #, Cost, Website, Email, etc). This works really well when entered manually – I’ve added them to output via event-meta-event-single.php.

    However, I added several input fields to the FES that then pointed to these fields. This works, kinda. All I get in the admin area once submitted is the first character in all circumstances. For instance, if for Contact Name I submit John Doe, all that comes through is ‘J’. The same thing goes for all the other fields.

    Any ideas?

    Thanks!

    Oliver

    Matthew Tarzwell
    #20353

    Hi Oliver,

    How are you linking to two, are you just giving the field the appropriate meta key? Or have you needed to do anything else.

    What do you see in the database, ‘John Doe’ or ‘J’? I ask because you typically would retrieve the meta value as follows:

    $value = get_post_meta( get_the_ID(), 'my_key', false ); //returns an array
    echo $value[0]; //Prints value for my_key
    

    Or, if you set the third argument to true (its default value), you’ll get back only the first value for the specify key (as keys can have multiple values). In which case in the above $value would be a string and $value[0] would be the first letter of that string.

    This is one possibility – but it depends on what you’ve had to do to connect the two plug-ins.

    Can you provide the field type and settings you’ve used for your ‘John Doe’ example so I can attempt to replicate this?

    Thanks!

    Stephen Harris
    #20403

    Hi Stephen,

    I tried to reply but I’m not sure if it got through as it’s not showing – let me know if you didn’t see the reply. Thanks.

    Matthew Tarzwell
    #20404

    Resubmitted:

    Hi Stephen, Sorry for the delayed response. Here’s how I’m doing it.

    I set the metaboxes up in the plugin; it looks like the following: http://positivebias.com/ipmb_edit.png

    Once I’ve done that, I setup my form, like so: http://positivebias.com/event_form.png

    Then when I go check the submission, it looks like this: http://positivebias.com/ipmb_fromform.png

    However, when I enter the data manually, it’s fine. This is how I’m inserting it in-page on events-meta-events-single.php

            // pull values from custom ipmetabox plugin
        $values = ipmb_get_metabox_values('ipmb_metabox_0');
        $current_events_venue_id = eo_get_venue();
        $gmaps_link = eo_get_venue_meta( $current_events_venue_id, '_venue_gmaps');
            if ($values || $gmaps_link) {
                echo '<h4>Additional Info</h4><ul class="no-bullet">';
                    foreach($values as $i => $value) {
                        if ($value['cost']) echo "<li><strong>Cost:</strong> {$value['cost']}</li>";
                        if ($value['audience']) {
                            $audiencearray=$value['audience'];
                            $count=count($audiencearray);
                            echo "<li><strong>Audience:</strong> ";
                            foreach($audiencearray as $i => $audience) {
                                if ($i+1<$count) { echo $audience.", "; } else { echo $audience; }
                            }
                            echo "</li>";
                        }
                        if ($value['contact_name']) echo "<li><strong>Contact Name:</strong> {$value['contact_name']}</li>";
                        if ($value['phone_number']) echo "<li><strong>Phone Number:</strong> {$value['phone_number']}</li>";
                        if ($value['email_address']) echo "<li><strong>Email Address:</strong> <a href="mailto:{$value['email_address']}">{$value['email_address']}</a></li>";
                        if ($value['website_url']) echo "<li><strong>Website / URL:</strong> <a href="{$value['website_url']}">{$value['website_url']}</a></li>";
                        if ($value['room']) echo "<li><strong>Room #:</strong> {$value['room']}</li>";
                    }
                    if ($gmaps_link) echo '<li><strong>Google Maps Link:</strong> <a href="'. $gmaps_link .'">'. $gmaps_link .'</a></li>';
                echo '</ul>';
            } ?>
    

    Finally, it also looks like the form simply won’t update on the submission page. I change it (moving fields around, adding a fieldset, but no matter what I do, it just won’t update. Check it out compared to the above form in the back end: http://positivebias.com/event_form_front.png

    Matthew Tarzwell
    #20423

    Hi Oliver,

    I’ve just checked out that plug-in and I can see why you’re getting the above issue. It’s do with how each plug-in inserts/expects data in the post meta table.

    With the event form, if you create a field with key 'ipmb_metabox_0_cost', and the user enters ‘£3.50’, then the value '£3.50' is stored against that key.

    If however, you enter that value using the metabox you create with IP Metabox, that plug-in will not store '£3.50', but instead:

     array( '£3.50' );
    

    So you see, when IP retrieves a value for the key 'ipmb_metabox_0_cost' it expects the return value (call it $value) to be serialised array, and so the actual user entered-value is stored in $value[0].

    Values entered into the post meta field by FES will not be array, but the entered string – and so $value[0] will return the first character of that string.

    I’m not entirely sure why IP Metabox is storing data that way – doing makes sorting/filtering by meta key impossible, but it means that the plug-ins are essentially incompatible.

    One thing you could do is give FES a different meta key and then use the hook eventorganiser_fes_submitted_event to convert those meta key-value pairs to a format IP Metabox understands. For example (and untested):

     add_action( 'eventorganiser_fes_submitted_event', function( $event_id ) {
           //Define an array mapping the key used by FES to the one used by IPMB
           $fes_key_to_ip_key = array( '_cost' => 'ipmb_metabox_0_cost' );
    
           foreach( $fes_key_to_ip_key as $key => $ip_key ) {
                  $value = get_post_meta( $event_id, $key, true );
                  if ( $value ) {
                         update_post_meta( $event_id, $ip_key, array( $value ) );
                  }
           }
     } );
    

    This has some obvious disadvantages. For example you have to maintain that list of meta keys. There are clever ways around this. E.g. pulling out all keys of format _fes_{xyz} and mapping it to ipmb_metabox_0_{xyz}.

    Stephen Harris
    #20476

    Hi Stephen,

    Okay, I’ve mostly gotten this working. As per your note, that plugin was not reading the data correctly so I’ve decided to not use a plugin for this at all. Instead, I’m going to use a class so that it is a bit easier to setup multiple instances; I need metaboxes elsewhere as well.

    I’ve chosen this one and it works well: https://github.com/mrfoto/mr-meta-box

    However, I’m still running into one oddity. I can get everything working except a multi-checkbox field. In our case, I’m using an Audience label on a checkbox (with 5 options) and inserting it into the form. It is set to submit to mr_audience (as per the class; I didn’t change it much). It works – the values all get submitted to the database.

    I’m retrieving it like so in event-meta-event-single.php:

            $moreInfo['audience'] = get_post_meta( get_the_ID(), 'mr_audience', false );
    

    This gets me an array, but it’s a bit odd – it’s multi-tiered. Instead of just {1,4}, it looks like this in PHPmyadmin:

    a:2:{i:0;s:1:"1";i:1;s:1:"4";}
    

    I can work with that. I can also switch the previous get_post_meta to true and then it gives me a simple string array, which also works. The problem Is that none of this shows properly in the admin area – it simply shows my first value: All. I’ve created this field as a CheckboxGroup in mr_meta_boxes:

        $metaBox->addField(array('type' => 'CheckboxGroup', 'id' => 'audience', 'label' => 'Audience: ', 'options' => array(0 => 'All', 1 => 'Students', 2 => 'Faculty', 3 => 'Staff', 4 => 'Public')));
    

    Thoughts?? I’m stumped, after trying it several different ways.

    Thanks,

    Oliver

    • This reply was modified 8 years, 4 months ago by  Matthew Tarzwell.
    Matthew Tarzwell
    #20486

    Hi Oliver,

    It looks like it’s storing the selected options as a serialised array – but that’s not what the plug-in does.

    As an example I added a checkbox to a FES form, and didn’t change any of the settings, except to give it the key fes_checkbox_test). I then submitted an event and selected ‘Option A’ and ‘Option C’. In the database the corresponding event as two entries in wp_postmeta for the key fes_checkbox_test: ‘Option A’ and ‘Option B’.

    Could you follow the above steps to confirm what you see (don’t set up the meta key for use in mr-meta-box just yet in case this affecting the behaviour).

    Stephen Harris
    #20487

    Yes, that works.

    I changed the checkbox Audience items to populate a new meta key in the FES, called my_checkbox_test.

    Then I put this in my event-meta-event-single:

        $checkTest = get_post_meta( get_the_ID(), 'my_checkbox_test', false );
    

    And got this (correct result) on a print_r($checkTest);

        Array ( [0] => Students [1] => Faculty [2] => Staff )
    

    So what am I doing wrong with that checkbox Group in mr meta boxes? I’d have preferred to use a multi-select box anyway, but it wasn’t available. I feel like I’m missing something really obvious, but am not sure what.

    Frankly, it’s something I can almost overlook since FES submits the correct values to me via email upon a form submission; I can then just update them manually in the event. But it’s just kind of bugging me and I’d like to sort it.

    Thanks for your help.

    Matthew Tarzwell
Viewing 8 posts - 1 through 8 (of 8 total)
To enable me to focus on Pro customers, only users who have a valid license for the Pro add-on may post new topics or replies in this forum. If you have a valid license, please log-in or register an account using the e-mail address you purchased the license with. If you don't you can purchase one here. Or there's always the WordPress repository forum.