0

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' );
?>

1 Answer 1

1

I believe esc_url() is what you're looking for?

here's shortcode code:

add_shortcode('urlencode','shortcode_urlencode');
function shortcode_urlencode($atts,$content = '') {
    return esc_url(do_shortcode($content));
}

not tested, but should work.

9
  • I think this is exactly what was needed, however I'm running into an issue. The values in the $_REQUEST string are shortcodes where it's being output, and those shortcodes are from another plugin. When surrounded by [urlencode] and [/urlencode], the other plugin's shortcodes are losing their shortcodiness...and it's the name of the shortcode itself being "encoded", rather than the data the shortcode [name] is intending to output. Commented Sep 17, 2014 at 15:05
  • e.g., if [name] is equal to ABC Widgets & Co., we're seeing something like this [urlencode][name][/urlencode] output as name instead of ABC+Widgets+%26+Co....which is my hopeful output. Commented Sep 17, 2014 at 15:08
  • updated answer; try that.
    – Caleb
    Commented Sep 17, 2014 at 17:00
  • Thanks. Unless I'm missing something, though, that's the same as I put in my update at the top of the post. For some reason, that still doesn't work. It's still outputting name when I do [urlencode][name][/urlencode] rather than ABC+Widgets+%26+Co.. What are we missing?! :| Commented Sep 17, 2014 at 17:21
  • can you update your question with the code that handles the name shortcode?
    – Caleb
    Commented Sep 17, 2014 at 17:22

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.