Skip Navigation

[Resolved] Encode URL in View Template

This thread is resolved. Here is a description of the problem and solution.

Problem:

The issue here is that the user wanted to encode their URL on the frontend.

Solution:

This can be done by using the following custom shortcode.

// Add Shortcode
function wpc_encode_url( $atts ) {
 
    // Attributes
    $atts = shortcode_atts(
        array(
            'url' => '',
        ),
        $atts
    );
 
    return urlencode($atts['url']);
 
}
add_shortcode( 'wpc_encode_url', 'wpc_encode_url' );

Then be triggered like below.

[wpc_encode_url url='[types field="url-produit" output="raw"][/types]']

Add the function to your Toolset custom code settings at Toolset -> Settings -> Custom Code.

100% of people find this useful.

This support ticket is created 3 years, 3 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 4 replies, has 2 voices.

Last updated by Guillaume 3 years, 3 months ago.

Assisted by: Shane.

Author
Posts
#2190071

Hello,

I would like to encode a URL field in a view template.

I don't know if I can use the JS function encodeURIComponent() to do it or if there is a better option.

Thanks

#2190111

Shane
Supporter

Languages: English (English )

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

Hi Guillaume,

Thank you for getting in touch.

When you say encode a URL are you referring to when you're displaying the URL on the frontend ?

Are you using the types shortcode to display the URL? If so then you can perhaps create a custom shortcode and pass our Types shortcode into it to use the php urlencode() function to encode the URL before displaying it.

hidden link.

Please let me know if this helps.
Thanks,
Shane

#2190193

Thanks for your reply,

Yes, I need to display the encoded url in the frontend.

Today I use a shortcode like this to display my url in the template : [types field="url-produit" output="raw"][/types].

So if I understand, I need to create a shortcode to display something like this :
echo urlencode('[types field="url-produit" output="raw"][/types]') . "\n";

It is correct ?

Thanks

#2190229

Shane
Supporter

Languages: English (English )

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

Hi Guillaume,

Here is a custom shortcode that I wrote for this scenario.

// Add Shortcode
function wpc_encode_url( $atts ) {

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

	return urlencode($atts['url']);

}
add_shortcode( 'wpc_encode_url', 'wpc_encode_url' );

Add this to your Toolset Custom code settings at Toolset -> Settings -> Custom Code and activate it.

Now all you need to do is call the shortcode like this.

[wpc_encode_url url='[types field="url-produit" output="raw"][/types]']

Thanks,
Shane

#2190285

My issue is resolved now. This code works perfectly. Thank you!