WordPress: remove #038 from shortcode

Simple example of a shortcode:

function s_print( $atts ){
    return 'http://abc.com/?foo=1&bar=2';
}
add_shortcode( 'my_shortcode', 's_print' );

And it returns:

http://abc.com/?foo=1&bar=2

This function inserts a link to page's body via shortcode [my_shortcode], but & is always changed to &#038, and this breaks the link (it's not working anymore). I googled a lot. There are some solutions:

wp_specialchars_decode($foo);
remove_filter('the_content','wptexturize');

But those seems to be only for use in theme (functions.php) and it doesn't work for a shortcode (I tried adding it before or inside the shortcode function).

I don't want to fall to last solution, which is commenting some lines in WordPress formatting.php file because I'm working on a plugin which will be used by many people.

Answers


I had a similar problem that I addressed with the clean_url filter. See the edit on my answer here.

It wasn't in a shortcode, so I can't guarantee it'll work in your particular situation. Might be worth a shot though.

EDIT by oyatek (modified solution from the link aboove):

function so_handle_038($content) {
    $content = str_replace(array("&","&"), "&", $content); 
    return $content;
}
add_filter('the_content', 'so_handle_038', 199, 1);

The fact is when you use the Visual Editor, the & will be changed to &. But if you use the Text Editor, single & followed by no character remains the same while &sth will be changed to &sth.

Altogether & will be changed to either & or &. I think the solution above:

function so_handle_038($content) {
    $content = str_replace(array("&","&"), "&", $content); 
    return $content;
}
add_filter('the_content', 'so_handle_038', 199, 1);

is a sort of overkill, because so_handle_038(); decodes all &s and &s in the $content while in your case, you need to decode only those in the $atts array. Your shortcode entry is probably like this:

[my_shortcode url="http://abc.com/?foo=1&bar=2" /]

and the $atts will be:

array( 'url' => "http://abc.com/?foo=1&bar=2" )

or:

array( 'url' => "http://abc.com/?foo=1&bar=2" )

so you only need to decode $atts['url']:

html_entities_decode($atts['url']);

before you try to do anything on it.


This one was a bandaid hack which I used, basically to fix the issue in wordpress (As I had my link inside of an iFrame), I went to Bitly and created a link WITHOUT the ampersand & sign! So finally I ended up getting a link that DIDN'T have any of the & signs but STILL pointed at the same location (Plus I got free tracking via Bit.ly for the link so DOUBLE bonus as I can check how many times the link was clicked). Not the "Best" solution, but heck it worked for me and I didn't have to waste time trying to figure out some other solution for the & symbol in Wordpress.


Need Your Help

Div overlapping Bootstrap Column

html css twitter-bootstrap-3

I'm just starting to use Bootstrap. Building my own page from scratch with it.

Spring Boot doesn't work on JBoss

java jboss spring-boot

I'm staring with Spring Boot and I tried to create the example available in the website (http://projects.spring.io/spring-boot/#quick-start). Everything worked fine when I ran the example as a stan...