Skip Navigation

[Resolved] replace all the spaces in a string with ‘%20’

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 – 13:00 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 9:00 – 13:00 -
- 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 14:00 – 18:00 -

Supporter timezone: Asia/Karachi (GMT+05:00)

This topic contains 1 reply, has 2 voices.

Last updated by Waqar 9 months, 2 weeks ago.

Assisted by: Waqar.

Author
Posts
#2641573

I'm trying to inject data from a custom field text string in the middle of a manually coded html link URL to be used as a parameter, but because the database text includes spaces and commas, the link does not work properly.

Can you suggest a way I can make the output for a custom text field ignore the commas and replace the spaces with %20 for just this one output instance? I may need to to use the text data from that same custom field in another way at a later point, but was trying to avoid creating a duplicate custom field for the same info and having to add %20 in the text string manually.

Example:
hidden link field='address-parameter'][/types]" target="_blank" >Beachfront Units

The 'address-parameter' custom field data holds a value like 'Charleston County, South Carolina, USA'
Is there a way to output the data just for this case to be 'Charleston%20County%20South%20Carolina%20USA'

#2641623

Hi,

Thank you for contacting us and I'd be happy to assist.

To achieve this, you can register a custom shortcode ( ref: https://codex.wordpress.org/Shortcode_API ), that gets the value from the target custom field and returns it using the 'urlencode' function ( hidden link ).

For example:


function custom_URL_encode_func( $atts ) {
	$field = $atts['field'];
	$content = types_render_field($field, array('output' => 'raw'));
	if(!empty($content)) {
		return urlencode($content);
	}
}
add_shortcode( 'custom_URL_encode', 'custom_URL_encode_func' );

The above code snippet can be included through either Toolset's custom code feature ( ref: https://toolset.com/documentation/adding-custom-code/using-toolset-to-add-custom-code/ ) or through the active theme's "functions.php" file.

Next, please add "custom_URL_encode" in the "Third-party shortcode arguments" section, at WP Admin -> Toolset -> Settings -> Front-end Content.

After that you'll be able to use this newly registered shortcode, like this:
(This example targets the field with slug 'address-parameter', but you can also use it for other fields )


[custom_URL_encode field='address-parameter']

I hope this helps and for more personalized assistance around custom code, you can also consider hiring a professional from our list of recommended contractors:
https://toolset.com/contractors/

regards,
Waqar

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