Skip Navigation

[Resuelto] Using content filter on Types fields causes issues with Video fields rendering

This support ticket is created hace 7 años, 4 meses. 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
- 7:00 – 14:00 7:00 – 14:00 7:00 – 14:00 7:00 – 14:00 7:00 – 14:00 -
- 15:00 – 16:00 15:00 – 16:00 15:00 – 16:00 15:00 – 16:00 15:00 – 16:00 -

Supporter timezone: Europe/London (GMT+00:00)

Etiquetado: 

This topic contains 4 respuestas, has 2 mensajes.

Last updated by Branigan hace 7 años, 4 meses.

Assisted by: Nigel.

Autor
Mensajes
#455385

I'm using a plugin called CM Tooltip Glossary which creates rollover glossary tooltips in your text content. It does this by hooking into the the main wordpress the_content filter.

The only way I could get this glossary tooltip plugin to recognise "glossary" terms that were in standard custom text fields set up in Types was to run all the Types fields through the main the_content filter like this in my functions.php:

add_filter( 'wpcf_fields_value_display', 'apply_the_content' );
function apply_the_content( $value ) {
    return apply_filters( 'the_content', $value );
}

It would appear that this is affecting the way the string is parsed to the Types function that renders the video, and thus the $args parameters are not being understood.

For example I've noticed in other places where this filter has been applied that the string that's stored in a standard types text field is being warpped in <p></p> tags when it's rendered using the types_render_field function.

I'm totally aware that this has taken us away from our initial problem, but I'm wondering if there's a way I can hook into an action in the Types plugin so that this filter is only applied to certain Types fields rather than ALL of them?

Many thanks

Dan

#455756

Nigel
Supporter

Languages: Inglés (English ) Español (Español )

Timezone: Europe/London (GMT+00:00)

Hi Dan

The filter you are hooking into is undocumented, but let me reproduce the relevant line of the source code here:

$params['field_value'] = apply_filters( 'wpcf_fields_value_display', $params['field_value'], $params, $post->ID, $field['id'], $meta_id );

That comes from the types_render_field_single function.

You are only using the first parameter—the field value.

I added the following to examine what is available if we use all the parameters:

add_filter( 'wpcf_fields_value_display', 'filter_types_output', 101, 5 );
function filter_types_output( $value, $params, $post_id, $field_id, $meta_id ){
	error_log(print_r($value, true));
	error_log(print_r($params, true));
	error_log(print_r($post_id, true));
	error_log(print_r($field_id, true));
	error_log(print_r($meta_id, true));

	return $value;
}

What I saw was the the field slug is available as one of the $params and directly as $field_id.

So you can test for which field is being filtered before deciding whether or not to pass $value through the_content filters.

That should do it, no?

#456033

Hi Nigel,

Thanks so much for investigating this. That looks to be exactly what I'm after, somehow only running particular fields through that filter.

Sadly PHP isn't my strong point - how would I adapt my existing function to incorporate such a conditional test?

Hoping you can help...

Dan

#456157

Nigel
Supporter

Languages: Inglés (English ) Español (Español )

Timezone: Europe/London (GMT+00:00)

Hi Dan

You need either an inclusive or an exclusive test for which field-slugs you want to apply the_content filter to.

Below I have assumed that you want to inclusively apply the_content filter to custom fields 'slug1', 'slug2', and 'slug3', and not leave other custom fields alone.

/**
 * Custom Types field render filter
 */
add_filter( 'wpcf_fields_value_display', 'filter_types_output', 101, 4 );
function filter_types_output( $value, $params, $post_id, $field_id ){

	if ( in_array( $field_id, array( 'slug1', 'slug2', 'slug3' ) ) ) {
		return apply_filters( 'the_content', $value );
	} else {
		return $value;
	}
}

PHP isn't my strong point either, but I think that should work 😉

#457254

Hi Nigel

Great it works! Thank you SO much for your help with both my recent queries, I really appreciate you going the extra mile with the code example.

Dan

This ticket is now closed. If you're a WPML client and need related help, please open a new support ticket.