How to remove empty tags left by <the_date> in Wordpress - 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 ); ?>

Related

removing p tag around images from wordpress term_description

Should be pretty straight forward but placing my filter in the themes function file is not having any affect on the template:
add_filter('term_description', 'filter_ptags_on_images');
function filter_ptags_on_images($content){
return preg_replace('/<p>\s*(<a .*>)?\s*(<img .* \/>)\s*(<\/a>)?\s*<\/p>/iU', '\1\2\3', $content);
}
and my markup:
<?php echo term_description(); ?>
EDIT: I tried testing the filter by returning dummy content from the function and nothing changes, so the filter isn't running on the content for some reason
It turns out I was trying to target the wrong template and the actual function was an Advanced Custom Field wysiwg custom content tag:
<?php the_field('fieldname'); ?>
It took me a little while to figure out how to filter this but with a little googling I found out you can use this to target an ACF wysiwyg field from your functions file:
add_filter('acf_the_content', 'your_function');

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

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.
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.

Single Page Navigation Menu Dynamically Generated

hHi all! I have posted this question on the WP support forums, but the community doesn't seem to be as active as stack's, so I am taking a chance here!
I am looking for a plugin that would automatically create a navigation menu (through the use of shortcodes for example) on a long single page documentation page.
The long page is divided into sections. I can imagine using a shortcode at the beginning of every section, and this will create a menu that would be displayed in a sidebar for example (called through a second shortcode perhaps, or a widget)
Any thoughts? Advice?
Thanks!
Use [section]Section Title[/section] shortcodes, then [section_navigation] where you want the navigation links output.
This works, but with a massive caveat -- that [section_navigation] needs to be in your post/page after the other [section] shortcodes... otherwise it generates an empty list.
You should be ok to use it in your theme by putting <?php echo do_shortcode("[section_navigation]");?> in sidebar.php. It will work as long as get_sidebar() is after the_content() in your theme templates (it usually is).
This to go in functions.php
$whit_sections = "";
// [section]My Section Title[/section]
function whit_section_shortcode( $atts, $title = null ) {
// $content is the title you have between your [section] and [/section]
$id = urlencode(strip_tags($title));
// strip_tags removes any formatting (like <em> etc) from the title.
// Then urlencode replaces spaces and so on.
global $whit_sections;
$whit_sections .= '<li>'.$title.'</li>';
return '<span id="'.$id.'">'.$title.'</span>';
}
add_shortcode('section', 'whit_section_shortcode');
// [section_navigation]
function whit_section_navigation_shortcode( $atts, $title = null ) {
global $whit_sections;
return '<ul class="section-navigation">'.$whit_sections.'</ul>';
}
add_shortcode('section_navigation', 'whit_section_navigation_shortcode');

Adding filters to child theme in wordpress

I have a child theme in wordpress that is based on twentyten.
Some of my authors have hardcoded URLs in their post titles and I want to remove those URLs.
I put the following code in my functions.php file in the child theme, but it has no effect on the display of the post title:
add_filter( ‘the_title’, ‘ib_strip_tags_from_titles’ );
function ib_strip_tags_from_titles( $title ) {
$title = strip_tags( $title );
return $title;
}
Any suggestions?
strip_tags() only removes HTML tags - in your case it will change the title from
Some Text LINK Other Text
to Some Text LINK Other Text
If I understand you correctly, this is what you want:
function ib_remove_links_from_titles($title) {
$title = preg_replace('/<a([^<]*)">([^<]*)<\/a>/', '', $title);
return $title;
}
add_filter( 'the_title', 'ib_remove_links_from_titles' );
going with the above example it will output Some Text Other Text
Note that given that you tried to accomplish the task with strip_tags(), I am assuming the "harcoded URLs", as you described them, are enclosed in <a [...] ></a> tags. If that's not the case you would need a regular expression that matches URLs. That is much more tricky, depending on whether the URLs your authors use are internationalized / have different domains, are not all just http:// prefaced and so on.
I vouch for the above to work if they are enclosed in tags, if not, this regex will catch most URLs, but comes without my guarantee to work in every case:
(([A-Za-z]{3,9})://)?([-;:&=\+\$,\w]+#{1})?(([-A-Za-z0-9]+\.)+[A-Za-z]{2,3})(:\d+)?((/[-\+~%/\.\w]+)?/?([&?][-\+=&;%#\.\w]+)?(#[\w]+)?)?
You'd have put that between the '/ and /' in the above function.

Resources