Skip Navigation

[Gelöst] Show and Update Types Checkboxes in REST API

This support ticket is created vor 7 Jahre, 4 Monate. There's a good chance that you are reading advice that it now obsolete.

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.

No supporters are available to work today on Toolset forum. Feel free to create tickets and we will handle it as soon as we are online. Thank you for your understanding.

Sun Mon Tue Wed Thu Fri Sat
- 9:00 – 10:00 - - - - -
- - - - - - -

Supporter timezone: Africa/Cairo (GMT+02:00)

This topic contains 4 Antworten, has 2 Stimmen.

Last updated by George vor 7 Jahre, 4 Monate.

Assisted by: Mohammed.

Author
Artikel
#470926

Hello,

I am working on an app that uses the REST API to pull data from our website. We're almost done with our project, but have stumbled on a problem with checkbox fields. When we show the field in REST, it returns this:

//Other post data
    "wpcf_languages_learning": {
      "wpcf-fields-checkboxes-option-06b39b00d9f8e9a8b41b37bd3049f205-1": [
        "English"
      ],
      "wpcf-fields-checkboxes-option-a468c600aa12d7f070d2a5931568f2c9-1": [
        "German"
      ]
    },

The best way to show that data would be like this:

 "wpcf_languages_learning": [
       {
           "Name": "English", 
       },
       {
           "Name": "German",
       }
  ]

Now, I can search on how to ignore the serialized key and just pull the value, but the problem is updating these fields with data from the app. The only solution I've seen on the forum is to first get and hardcode the keys for each option and then update that way, but that doesn't seem too rational. What would be the best way to not only modify the data being returned from REST, but also update checkboxes through a REST POST request?

#470991

Hello,

I’m Mohammed: the Toolset support team leader. I’ll give my best to help you to achieve your needs through Toolset components.

Currently, there is no ongoing development work related to the REST API in the toolset plan.

We have major priorities at the moment and we will start working on the REST API after finishing them.

Anyway, I will consult one of our developers to see if we can do something regarding this.

Thanks.

#471002

Thank you Mohammed. For now I have figured out how to show the fields in the way I want (so far) with this code:

function slug_get_aaa( $object, $field_name, $request ) {
	$values = get_post_meta( $object[ 'id' ], $field_name, true );
	$return = array();
	foreach ($values as $key => $value) {
		$return[] = ['name' => $value[0]];
	}
	return $return;
}

But updating the fields from REST is still a huge issue. I look forward to future updates!

#471017

Thanks George for sharing this.

I've consulted the development team leader and he confirmed what I mentioned before.

Anyway, you can close this ticket for now and follow the Types plugin changelog page that contains the bug fixes and the newly added features in each version we launch: https://toolset.com/download/toolset-types/#changelog

Thanks.

#471021

Alright, not the answer I was expecting, especially since REST is part of the WP core now. Luckily I figured it out myself. People who understand REST a little bit should understand the below functions:

My post type is 'exchange', so change that to yours, and fields are 'wpcf_languages_spoken' and 'wpcf_languages_learning', which are multi-select checkboxes. Please note that I am only allowing the user to select one box per field in the app, so not sure if this code works for more.

Also please note that if you are updating the field, check your Types post field settings to see what you should be saving the field value as in your database. For me, it was just the value name as a string, but I believe the default option is a number.

//Fields for Language Exchanges
add_action( 'rest_api_init', 'slug_register_exchange' );
function slug_register_exchange() {
	foreach (array('wpcf_languages_spoken', 'wpcf_languages_learning') as $field) {
		register_rest_field( 'exchange',
			$field,
			array(
				'get_callback'    => 'slug_get_exchange',
				'update_callback' => 'slug_update_exchange',
				'schema'          => null,
			)
		);
	}
}

//Get custom exchange fields
function slug_get_exchange( $object, $field_name, $request ) {
         //note that the fields were added with underscores rather than dashes. This is because in JS, dashes cause problems, so here we need to convert them back.
	$field_name = str_replace('_', '-', $field_name); 
	$values = get_post_meta( $object[ 'id' ], $field_name, true );
	$return = array();

        //This is going to output an array for each field with the value of each selected checkbox
	foreach ($values as $key => $value) {
		$return[] = ['name' => $value[0]];
	}
	return $return;
}

//For posting a language exchange with custom field checkboxes. not sure if it works for multiple values
function slug_update_exchange( $value, $object, $field_name ) {
    if ( ! $value || ! is_string( $value ) ) {
        return;
    }

        //again, we have to replace the underscores with dashes
	$field_name = str_replace('_', '-', $field_name);
	$prefix = 'wpcf_';
	//here we strip the prefix so we can use it to get field options in the Types function below
        $field_name_nopre = substr($field_name, strlen($prefix));
	$field_data = types_get_field($field_name_nopre);
	$options = $field_data['data']['options'];
	$return = array();
	$value = strip_tags( $value );
	foreach ($options as $key => $val) {
		if ($val['title'] === $value) {
			$return[$key] = array(0 => $value);
		}
	}
    return update_post_meta( $object->ID, $field_name, $return );
}
This ticket is now closed. If you're a WPML client and need related help, please open a new support ticket.