Skip Navigation

[Resolved] Types Field API no longer calling wpcf_fields_[field name]_meta_box_form()

This is the technical support forum for Toolset - a suite of plugins for developing WordPress sites without writing PHP.

Everyone can read this forum, but only Toolset clients can post in it. Toolset support works 6 days per week, 19 hours per day.

This topic contains 17 replies, has 3 voices.

Last updated by Adriano 6 years, 3 months ago.

Assigned support staff: Adriano.

Author
Posts
#292356

Hi Guys,

Last year I created a little plugin using your Types Field API to add a custom field to integrate a jQuery Google Map location picker in the post edit screen.

This worked fine but a few months ago one of your updates changed how the API works and it broke the plugin.

I'm basing my work on your wp-content\plugins\wp-types\embedded\includes\examples\google_map.php example file.

The problem I'm seeing in my code is also happening when I run your example code.

The issue that I is that the wpcf_fields_[field name]_meta_box_form function is not doing it's job.

The following explanation is based on your google_map.php example:

Having added a Google Map custom field to a field group for a CPT, on an 'add new post' admin page I should be able to see a field with the title "Add Google Map coordinates" followed by the text "Your input should look something like "41.934146,12.455821" (as per lines 137-138 of your sample code).

Instead, I see the default output for a text field and nothing more. I'm seeing the same problem with my code, which is as follows:

function wpcf_fields_tws_google_map_meta_box_form( $data ) {
    $form = array();

    $form['map-picker'] = array(
        '#name' => 'twsgeo', // Set this to override default output
        '#type' => 'markup',
        '#markup' => '
Location: <input type="text" id="tws-gm-address" style="width: 100%"/>
<div id="twsgeoselector" style="width: 100%; height: 400px;"></div>
'
    );

    return $form;
}

I also only get a default text field instead of my custom markup.

I've run this through xdebug and the wpcf_fields_[field name]_meta_box_form does not get called when loading an 'Add New Post' page. The only time I've seen it called is when saving the custom fields. Is this correct behaviour?

Can you please try to replicate my problem by adding this to a functions.php file of a test site:

include_once WP_PLUGIN_DIR.'/wp-types/embedded/includes/examples/google_map.php';

Then add a Google Map custom field to a CPT of your choice and go to the 'Add New' page for that CPT. Does the field show the 'Add Google Map coordinates' text as described above?

Regards,
Andy Brennenstuhl

#292566

Luo Yang
Supporter

Languages: English (English ) Chinese (Simplified) (简体中文 )

Timezone: Asia/Hong_Kong (GMT+08:00)

Thanks for the feedback, I have been able to duplicate same problem in my localhost, and put it into our to-do list, our developers will take care of it.

#292767

That's great. Could I please get an ETA for this? I need to fix the plugin I created as soon as possible or find an alternative solution to the problem.

Regards,
Andy

#293834

Can I please get an update and ETA on this? I have several clients of mine waiting for an answer.

If this is going to take more than a week then can you please help me implement a temporary workaround?

Regards,
Andy

#296341

Hello Andrzej,

This is Adriano, team leader. I'm sorry it's taking so long, we were very busy releasing Layouts and Views. I'll personally handle this one. Please give me some time to discuss that with our development team, I'll update you very soon.

#296346

I've replicated that issue on my localhost, talking with our Types developer right now, please hold on.

#296349

Thanks Adriano.

#296354
Captura de Tela 2015-04-16 às 11.32.19.png
Captura de Tela 2015-04-16 às 11.30.49.png

The way we define custom fields has been changed indeed, now the only way to have this title and description is by editing the definition of the custom field directly on Types GUI, see the attached image. Please let me know if does it solve your issue.

#296507

Hi Adriano,

I don't need to just change the title and description, but the whole output markup for that field.

Basically, I need to create a custom container for a jQuery location picker plugin, which then injects Long/Lat values into separate custom fields.

As per my original ticket, the original code I was running is as follows:

function wpcf_fields_tws_google_map_meta_box_form( $data ) {
    $form = array();

    $form['map-picker'] = array(
        '#name' => 'twsgeo', // Set this to override default output
        '#type' => 'markup',
        '#markup' => '
Location: <input type="text" id="tws-gm-address" style="width: 100%"/>
<div id="twsgeoselector" style="width: 100%; height: 400px;"></div>
'
    );    
    return $form;
}

So, can you please advise on how I can get the above markup to be displayed in conjunction with a set of long/lat fields?

On a sidenote, I should point out that it seems that you have made a very significant change to a documented part of Types without preserving functionality or backward compatibility. Should I be worried that other things on my projects will break due to future upgrades such as this?

Regards,
Andy

#296786

Hello Andrzej,

I see what you mean. I'm working on this right now, trying to understand why that function is being ignored. I'm working with our Types developer, I'm sure we'll find a solution for you. Please give me some more time.

We do care about preserving functionalities, we always think in all the possibilities before doing some complex change. Unfortunately we missed that one, when adding new kind of fields. I'm sorry about that.

#296894

Ok. Thanks Adriano.

#298059

Hello Andrzej,

Could you share the full code of your Types field definition? Not only the "_meta_box_form" function.

We are doing some tests in our API code.

#298234

Here's the code...

Note that the purpose of this code is to load the jQuery location picker plugin and load the script.js script, which uses the output of the location picker to populate a longitude and latitude field.

At the moment, I'm not using any functionality to render the field on the front-end as I'm doing that manually for now in a content template.


function tws_gm_admin_scripts() {
    wp_register_script('google-maps-api', '<em><u>hidden link</u></em>');
    wp_enqueue_script('google-maps-api');

    wp_register_script('locationpicker', plugin_dir_url( __FILE__ ) . '/vendor/jquery-locationpicker/locationpicker.jquery.js');
    wp_enqueue_script('locationpicker');

    wp_register_script('tws-gm-script', plugin_dir_url( __FILE__ ) . '/js/script.js');
    wp_enqueue_script('tws-gm-script');
}
add_action("admin_enqueue_scripts", "tws_gm_admin_scripts");

// Add registration hook
add_filter( 'types_register_fields', 'tws_google_map' );

/**
 * Register custom post type on 'types_register_fields' hook.
 * 
 * @param array $fields
 * @return type
 */
function tws_google_map( $fields ) {
    // $fields['tws_google_map'] = wpcf_fields_tws_google_map();
    $fields['tws_google_map'] = __FILE__;
    return $fields;
}

/**
 * Define field.
 * 
 * @return type
 */
function wpcf_fields_tws_google_map() {
    return array(
        'path' => __FILE__,
        'id' => 'google_map',
        'title' => __( 'Google Map', 'wpcf' ),
        'description' => __( 'This is additional field', 'wpcf' ),
        'validate' => array('required'),
        // meta key type
        'meta_key_type' => 'INT',
        // Required WP version check
        'wp_version' => '3.3',
    );
}

/**
 * Types Group edit screen form.
 * 
 * Here you can specify all additional group form data if nedded,
 * it will be auto saved to field 'data' property.
 * 
 * @return string
 */
function wpcf_fields_tws_google_map_insert_form() {
    $form['additional'] = array(
        '#type' => 'markup',
        '#name' => 'comment',
        '#markup' => 'Please remember to add two numeric fields to this Custom Field group named "latitude" and "longitude"',
    );
    return $form;
}

/**
 * Overrides form output in meta box on post edit screen.
 */
function wpcf_fields_tws_google_map_meta_box_form( $data ) {
    $form = array();

    $form['map-picker'] = array(
        '#name' => 'twsgeo', // Set this to override default output
        '#type' => 'markup',
        '#markup' => '
Location: <input type="text" id="tws-gm-address" style="width: 100%"/>
<div id="twsgeoselector" style="width: 100%; height: 400px;"></div>
'
    );    
    return $form;
}

/**
 * Adds editor popup callnack.
 * 
 * This form will be showed in editor popup
 */
function wpcf_fields_tws_google_map_editor_callback() {
    wpcf_admin_ajax_head( __( 'Field not available', 'wpcf' ) );
    wpcf_admin_ajax_footer();
}

/**
 * Processes editor popup submit
 */
function wpcf_fields_tws_google_map_editor_submit() {
    // Nothing here
    }
    die();
}

/**
 * Renders view
 * 
 * Useful $data:
 * $data['field_value'] - Value of custom field
 * 
 * @param array $data
 */
function wpcf_fields_tws_google_map_view( $data ) {
    return '';
    }

Incidentally, if you can advise me on how to integrate storing the longitude and latitude data into this custom field, then that would be great as ideally this solution should not rely on the presence of other fields.

#298380

Hello Andrzej,

First of all there are some issues on your code:

1. PHP Parse error: syntax error, unexpected '}' in g.php on line 99
2. It won't work '#name' => 'twsgeo'. You have to use something like that wpcf[' . $data['twsgeo'] . ']
3. An important function is missing WPToolset_Field_[FIELD]_loader

Thing is that we provide an API which can be used for different purposes, but we are not allowed to write custom codes to our clients. If you need that, I'd recommend you our service: custom.wp-types.com

Please let me know if you are satisfied with my reply and any other questions you may have.

Regards,

Adriano Ferreira

#298599

Adriano,

The code I pasted is somewhat modified from the original for brevity, so the syntax error is because of that. I did not expect you to try to debug that seeing as you've already managed to replicate the issue in your own code.

More importantly, note that the code worked just fine when I first implemented it, and it was based on the Google Map template you provided.

Lastly, your own example code for the API currently does not work.

So, I understand that you cannot write code for me, but there is obviously a problem with your code, and that is what I'm asking you to fix.

Once I have working copy of your example Google map custom field code (where the 'Add Google Map coordinates' is correctly displayed when rendered on a dashboard edit page) then and I can base my implementation off of that.

Regards,
Andy