Skip Navigation

[Resolved] Clean custom field string and display it

This support ticket is created 5 years, 5 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
- - 14:00 – 20:00 14:00 – 20:00 14:00 – 20:00 14:00 – 20:00 14:00 – 20:00
- - - - - - -

Supporter timezone: Asia/Ho_Chi_Minh (GMT+07:00)

Tagged: 

This topic contains 2 replies, has 2 voices.

Last updated by Beda 5 years, 5 months ago.

Assisted by: Beda.

Author
Posts
#1334093

Is there a way to clean a custom field and display it?

I have this single line text cf with the value (added by user): "Nice & Good Title" and I would like to transform it to "nice-good-title" so I can use it in HTML attributes.
All this inside a view using cf shortcodes.

Any idea?

thanks

#1334115

It's not natively possible with Toolset Views to somehow alter a value coming from the database in this way, Toolset only offers a sanitize option for the relevant shortcodes, which will not allow you to customize how the content is actually sanitized, but will follow common WordPress sanitization rules instead.

What you can do is creating a Custom ShortCode with 2 attributes:
"data" and "separator".
Your Custom ShortCode then should expand what comes from "data" (this will be your Custom Field ShortCode), and take that string (Nice and Good Title), then strtolower() it, and replace empty spaces with the "separator" you add to your Custom ShortCode.
This will require the usage of Custom PHP code, using methods as shown here hidden link and hidden link

The ShortCode itself can be crafted following the WordPress Codex here https://codex.wordpress.org/shortcode

This ShortCode can then be used in your View, and since it will feature 2 attributes (data and separator) you can then do something like this:
[your_custom_shortcode data="[wpv-post-title]" separator="-"]
That would then process whatever inserted for "data" through your custom code, and strtolower() it, then replace the empty with - using the custom code above linked (adapted to your case).

Please let me know if something is unclear on the general process.

#1334117

Changing state...