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
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'
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