Saltar navegación

[Resuelto] Retrieving information about a custom field via a PHP function

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.

Sun Mon Tue Wed Thu Fri Sat
- 10:00 – 13:00 10:00 – 13:00 10:00 – 13:00 10:00 – 13:00 10:00 – 13:00 -
- 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 -

Zona horaria del colaborador: Asia/Kolkata (GMT+05:30)

Este tema contiene 7 respuestas, tiene 1 mensaje.

Última actualización por Saul Baizman 5 days, 4 hours ago.

Asistido por: Minesh.

Autor
Mensajes
#2865250

Hi there,

I had a question about the Types API in PHP. I'm aware of types_render_field() and similar functions (https://toolset.com/documentation/customizing-sites-using-php/functions/), but I was wondering if there were any PHP functions for programmatically retrieving information about Toolset fields. For example, given a field slug, can one obtain the field type? Is that possible, and if so, how?

Thanks very much.

Saul

#2865253

Minesh
Colaborador

Idiomas: Inglés (English )

Zona horaria: Asia/Kolkata (GMT+05:30)

Hello. Thank you for contacting the Toolset support.

Please check the following Doc for Custom Field Definition:
- https://toolset.com/documentation/programmer-reference/toolset-custom-field-api/#customfielddefinition

#2865287

Minesh,

Thanks for sharing the documentation link. I'm having trouble instantiating the CustomFieldDefinition class. Here's a snippet of code:

$field_info = toolset_get_field_instance(
				element_source: $post_id,
				domain: \OTGS\Toolset\Common\PublicAPI\ElementDomain::POSTS,
				field_slug: 'wpcf-phone-number',
			);

The PHP error log reports this issue:
PHP Fatal error: Uncaught Error: Class "OTGS\Toolset\Common\PublicAPI\ElementDomain" not found in...

Any guidance?

Saul

#2865300

Minesh
Colaborador

Idiomas: Inglés (English )

Zona horaria: Asia/Kolkata (GMT+05:30)

Here is the usage:

Please add the following code to "Custom Code" section offered by Toolset:
- https://toolset.com/documentation/programmer-reference/adding-custom-code/using-toolset-to-add-custom-code/

use OTGS\Toolset\Common\PublicAPI;

function toolset_field_type_shortcode( $atts ) {

	$atts = shortcode_atts(
		array(
			'field' => '',
		),
		$atts
	);

	$field_slug = sanitize_key( $atts['field'] );

	if ( empty( $field_slug ) ) {
		return '';
	}

	$factory = Toolset_Field_Definition_Factory::get_factory_by_domain(
		Toolset_Element_Domain::POSTS
	);

	$field_definition = $factory->load_field_definition( $field_slug );

	if ( ! $field_definition instanceof PublicAPI\CustomFieldDefinition ) {
		return '';
	}

	return $field_definition->get_type_slug();
}
add_shortcode( 'toolset_field_type', 'toolset_field_type_shortcode' );

You can call the above shortcode as given under. For exmaple, you have created a custom field with slug "my-book" and to get its field type, you can call the following shortcode:

 [toolset_field_type field="my-book"]   

It should return you the custom field type.

If you want to get specific field within the specific custom field group then, for instance, if the custom field group slug is "my-custom-field-group" and the custom field added to this group and its slug is "my-custom-field" then you can use the following code:

use OTGS\Toolset\Common\PublicAPI;

$group = toolset_get_field_group( 'my-custom-field-group', Toolset_Element_Domain::POSTS );

$field_definition = $group->get_field_definition( 'my-custom-field' );

$field_type = $field_definition->get_type_slug();
  
#2865307

Minesh
Colaborador

Idiomas: Inglés (English )

Zona horaria: Asia/Kolkata (GMT+05:30)

Here is another example if you may want to get first field instance and then get the field type:

use OTGS\Toolset\Common\PublicAPI\ElementDomain;

$field_instance = toolset_get_field_instance(
	$post_id,
	ElementDomain::POSTS,
	'book-title'
);

if ( $field_instance ) {
	$field_type = $field_instance
		->get_definition()
		->get_type_slug();

	echo $field_type;
}
#2865319

Minesh,

Thanks for providing the snippets of PHP code.

I'm still running into the same fatal PHP error ("Uncaught Error: Class 'OTGS\Toolset\Common\PublicAPI\ElementDomain' not found"). I suspect it's because the shortcode is executing before the Toolset plugins have loaded. I tried changing loading my shortcodes on the "plugins_loaded" hook, but the PHP error persisted. Any advice?

Saul

#2865322

Minesh,

I sort of gave up using the PHP API and resorted to retrieving the "wpcf-fields" option in wp_options. The array contains the custom fields and their field types (as well as a bunch of other info). Good enough for me!

Saul

#2865376

Minesh
Colaborador

Idiomas: Inglés (English )

Zona horaria: Asia/Kolkata (GMT+05:30)

Ok great - you are welcome to mark resolve this ticket.

#2865475

Thanks again for your help, Minesh!