I have a string of data that's getting passed to the page via shortcode, but the original developer of the shortcode did not account for how I'm necessarily planning on using the data on the page, unfortunately. I'm creating a "leaving this website" type of page, and the user will be presented with a message, and then automatically redirected to the offsite page.
In short, the custom field holds a URL for the offsite page, which I'm then passing as a $_REQUEST
variable to another "you're about to leave" page. Because there are multiple &
and ?
, the URL breaks...as do my vars. Not always...just when there's multiple &
and ?
.
I am hoping there's some way to urlencode easy solution from a shortcode, where I can just wrap the shortcode around my text and have it encoded. I'm aware there are PHP shortcode plugins out there, but from a security standpoint, I'm hoping to stay away from that one.
Perhaps maybe I'm thinking about this wrong, though?
Example of what I have:
<a href="/leaving?company=[name]&url=[website]">Visit Website</a>
What I'm hoping to end up with:
<a href="/leaving?company=[urlencode][name][/urlencode]&url=[urlencode][website][/urlencode]">
Visit Website
</a>
Should I just be creating a custom shortcode in my Theme's functions.php
for something like this? The functionality seems fairly straightforward. Just getting hung up.
UPDATE
Attempting to use Caleb's solution, but ran into issues. Now I'm doing do_shortcode()
on the content, but still running into the same encoding issue mentioned in the comments below.
<?php
function shortcode_urlencode( $atts, $content = '' ) {
return esc_url(do_shortcode($content));
}
add_shortcode( 'urlencode', 'shortcode_urlencode' );
?>