Skip Navigation

[Resolved] remove inline style from custom field

This support ticket is created 3 years, 7 months ago. 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.

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

Supporter timezone: America/Jamaica (GMT-05:00)

This topic contains 3 replies, has 2 voices.

Last updated by Shane 3 years, 6 months ago.

Assisted by: Shane.

Author
Posts
#2070251

Tell us what you are trying to do?

I have a feed that imports content into custom fields. Some of the imports include html with inline style. Is there a function that can strip style from a custom field? In the instance my custom field slug is "the-role"

What is the link to your site? hidden link

#2070391

Shane
Supporter

Languages: English (English )

Timezone: America/Jamaica (GMT-05:00)

Hi Adam,

Thank you for getting in touch.

You're saying that your source provides the content with HTML in it and you want to strip those out ?

In this case you will need a hook from the feed plugin to strip out the inline html content from the text. Not much we can do here on our side about this one.

I would recommend that you contact the plugin author to see if they have a method to strip out the HTML while the text is being imported.

Thanks,
Shane

#2070969

Hi Shane,

Unfortunately we don't want to remove all the html, just any inline style elements. I had seen something like this elsewhere.

add_filter('the_content', function( $content ){
//--Remove all inline styles--
$content = preg_replace('/ style=("|\')(.*?)("|\')/','',$content);
return $content;
}, 20);

Is there a way to do something like the above for specific custom fields or create a shortcode that actions the above when the custom field is wrapped in the shortcode?

#2071195

Shane
Supporter

Languages: English (English )

Timezone: America/Jamaica (GMT-05:00)

Hi Adam,

You may be able to do it with a custom shortcode. Try this one below that i've made.

// Add Shortcode
function wp_remove_inlinestyle( $atts ) {

	// Attributes
	$atts = shortcode_atts(
		array(
			'text' => '',
		),
		$atts
	);

	$content = preg_replace('/ style=("|\')(.*?)("|\')/','',$atts['text']);
	return $content;

}
add_shortcode( 'wp_remove_inlinestyle', 'wp_remove_inlinestyle' );

All that is needed is for you to pass your types shortcode into this shortcode like below.

[wp_remove_inlinestyle text="[types field='my-field'][/types]"]

Just add this custom shortcode to Toolset -> Settings -> Custom Code and activate it.

Please let me know if this helps.
Thanks,
Shane