WordPress: remove #038 from shortcode - wordpress

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.

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.

Related

Get the title of the original page in your default language and add it as body class to the translated page on WordPress with WPML

Here's my problem:
add_filter( 'body_class', 'wpml_body_class');
function wpml_body_class( $class ) {
global $sitepress, $post;
if( $sitepress->get_default_language() != ICL_LANGUAGE_CODE ) {
$original_id = icl_object_id( $post->ID, get_post_type(), true, $sitepress->get_default_language() );
$class[] = strtolower(get_the_title( $original_id ));
}
return $class;
}
This code works fine. Essentially, I use $sitepress as a global to get my default language and then I extract the ID to match it with get_the_title, so, at the end of the day, I added the title as a class name to the body, so I can easily replicate the style of the original page without adding a line on my CSS stylesheet file on the translated page, in this case in French.
So far so good, except for a caveat:
Since this is the title, if I have a title like Our Team, I have to add a dash to the style, and it is going to change base on how many words I have. If I have to use the URL instead, the process to extract it with WordPress is more complex, so I was wondering if it is possible to add a regular expression to add a dash if I have any space. Or if everyone else knows how to extract the URL instead of get_the_title I couldn't be more grateful.
what you need is sanitize_title_with_dashes() for your purpose :) which is provided by WP . Reference https://codex.wordpress.org/Function_Reference/sanitize_title_with_dashes

How to deactivate past shortcodes in Wordpress

Recently I changed the theme of my site, and I found many of my articles use a shortcode like this
[box]
....
[/box]
My new theme does not support it and I actually don't need this shortcode to function. I thought I could just write a empty function for the shortcode in function.php, like this
function shortcode_box() {
return "";
}
add_shortcode('box', 'shortcode_box');
but it's not working.
Do you know any method to deactivate this short code?
So, you want to leave the [box] bits in the posts and/or pages, but have them not do anything? Try a shortcode that passes through the content unchanged:
function shortcode_box( $atts, $content = null ) {
return $content;
}
add_shortcode( 'box', 'shortcode_box' );
(For enclosing shortcodes, the return value of the function is used to replace the entire shortcode.)
Use remove_shortcode()
remove_shortcode('box');
Reference: http://codex.wordpress.org/Function_Reference/remove_shortcode

Disabling HTML on Comments

I've been researching for the past few hours, trying to find a way to disable HTML in WordPress comments. So far this one consistently appeared on top of Google search results numerous times:
// This will occur when the comment is posted
function plc_comment_post( $incoming_comment ) {
// convert everything in a comment to display literally
$incoming_comment['comment_content'] = htmlspecialchars($incoming_comment['comment_content']);
// the one exception is single quotes, which cannot be #039; because WordPress marks it as spam
$incoming_comment['comment_content'] = str_replace( "'", ''', $incoming_comment['comment_content'] );
return( $incoming_comment );
}
// This will occur before a comment is displayed
function plc_comment_display( $comment_to_display ) {
// Put the single quotes back in
$comment_to_display = str_replace( ''', "'", $comment_to_display );
return $comment_to_display;
This code did not work with the latest version of WordPress. I also found many more codes that again, did not work. So how would one go about disabling HTML in WordPress 3.6 (the latest version) comments?
This removed the ability for users to post HTML (but not links for some strange reason) within comments:
add_filter( 'pre_comment_content', 'wp_specialchars' );
This removed the ability for users to post links within comments:
remove_filter('comment_text', 'make_clickable', 9);
To disable HTML tags in comments, put the following code into your theme's functions.php:
add_filter('comment_text', 'wp_filter_nohtml_kses');
add_filter('comment_text_rss', 'wp_filter_nohtml_kses');
add_filter('comment_excerpt', 'wp_filter_nohtml_kses');

paginate_links() outputs "paged=" to link instead of "page="

I've been having a sever and completely unsolvable problem with pagination.
I did notice however this interesting point:
I search a keyword like cute:
?s=cute&submit=Search&post_type=image&paged=2
...is where the link leads to. "Paged=" gives 404 on random pages. But if I modify the URL to say page=
?s=cute&submit=Search&post_type=image&page=2
Every page destination works, bringing joy to my heart, but the pagination tab list always sticks at 0 (not reflecting current page).
I feel like I'm playing a "shell game" with code and settings, with wordpress bamboozling me.
Question reduces to:
How can I get &page= to be output in every case? Conversely, if &paged= should be the way it goes, how do I get it to work without 404s?!?!
This is a problem I've dealt with for almost 3 months now. People are shy to answer.
update:
Trying to deal with this paged variable in the URL which breaks pagination, I created a filter to simply replace it with page.
function symbiostock_pagination_mod( $args ){
$args = str_replace('paged=', 'page=', $args);
return $args;
}
add_filter( 'paginate_links', 'symbiostock_pagination_mod', 1 );
Would seem like a great idea! But then this happens with every new click:
?s=cute&post_type=image&page=2&page=2
?s=cute&post_type=image&page=2&page=3
So where is this being modded, and why?
update:
I'm not sure if I'm the only one that has ever had this problem, but I did solve it (at least for myself) you can see the solution below. Also thanks for the help given. :D
So here is what I came up with...we might as well call it a hack or work-around because its unknown if this was indeed a bug I'm dealing with (from wordpress itself) or just a perfectly hidden problem in my theme's code.
The below function filters the 'paginate_links()' function and achieves this:
I see paged variable generates 404 randomly on search pages, but works fine on archive and taxonomy pages. So, we check that. if_search(), we do our changes...
If this is a search, we get the would-be page # destination using regex (or simply string replace paged to page, depending on permalink structure. We use remove_query_var() to drop faulty variable, and add_query_var() to put the working one.
This is being done because in this strange case, page always generates proper results.
Then we return the edited destination link, and life is good.
Function below:
add_filter( 'paginate_links', 'my_search_pagination_mod', 1 );
function my_search_pagination_mod( $link )
{
if ( is_search() ) {
$pattern = '/page\/([0-9]+)\//';
if ( preg_match( $pattern, $link, $matches ) ) {
$number = $matches[ 1 ];
$link = remove_query_arg( 'paged' );
$link = add_query_arg( 'page', $number );
} else {
$link = str_replace( 'paged', 'page', $link );
}
}
return $link;
}
Also thanks to some help I got here with the regular expression: PHP Regular Expression Help: A quick way to extract the 2 from "page/2/"

How to remove empty tags left by <the_date> in Wordpress

Wordpress doesn't support adding styles to the_date the way wp_nav_menu does, so we have to code the style outside the function.
While it's handy that the_date will only show up once each day, it does, however leaves empty tags in the code that would mess up the layout, particularly the marging/padding.
After looking for solutions, the best option is to write a function in the theme's function.php that hooks up with the_content, so this is what I came up with:
function remove_empty_date($string)
{
$string = str_replace('/<small class="date">\s*</small>/', '',$string);
return $string;
}
add_filter('the_content','remove_empty_date');
The culprit is <small class="date"></small>, which manifests in the page as a date-styled field with no date in it.
If there's no better solution than the above, where did the code go wrong so it doesn't remove the unwanted string?
Update: false alarm, Wordpress does support adding tags from the function calls, http://codex.wordpress.org/Function_Reference/the_date.
Solution: add the tag from within the function:
From: http://codex.wordpress.org/Function_Reference/the_date —
<?php the_date( $format, $before, $after, $echo ); ?>

Resources