Tag: WordPress functions.php

How to add a posts tags cloud to end of posts

An alternative way of showing related content in your posts is to include a TAG CLOUD at the end of your article. You can create this with an easy shortcode function that will identify the post ID, get the post’s tags and then display the tags as a tag cloud.

Simply add the following shortcode code to your functions.php file, then add the shortcode [dtposts-tagcloud] in your posts where you would like the tag cloud to display.

// add a posts tags cloud to end of posts
function dtposts_tagcloud($echo=false) {
	global $post;
	$tag_ids= wp_get_post_tags($post->ID, array('fields'=>'ids'));
	if($tag_ids) {
		return wp_tag_cloud(array(
			'unit' => 'pt',
			'smallest' => '10',
			'largest' => '22',
			'order' => 'RAND',
			'include' => $tag_ids,
			'echo'=> $echo,
		));
		} }
add_shortcode('dtposts-tagcloud','dtposts_tagcloud');

This uses the standard wp_tag_cloud() function as explained in the WordPress Codex.

This shortcode is demonstrated below. I have added a heading, some css styling, and then called the function using the shortcode. The code is entered as text in the text editor in a WordPress post:

<div style="text-align: center; padding: 20px; background-color: rgba(60,60,60,0.75); margin: 30px;"><b style="color: white; font-size: 18px; font-weight: bold;">POST TAGS</b>
[dtposts-tagcloud]</div>

Here is how it looks:

Check out some more tips and code snippets using the above tags.

Filed under: WordPressTagged with: , , ,

Hide WPMU dashboard plugin alert and other nag updates

A quick and simple way to hide WPMU dashboard plugin alerts or other annoying “nag” updates in the WordPress admin dashboard can be achieved with a simple piece of css display:none code in your childtheme’s functions.php file.

This is probably not the “ideal” way to remove these “nags” however sometimes other options can be less effective. For example in the case of the WPMU dashboard plugin alert, it’s automatically triggered when you install WPMU plugins. The recommended method to remove this alert- is to install the free dashboard plugin or to make changes to lines in the plugin file that is triggering this.

Of course we all know that when we make changes to theme files or plugin files, that when the next update comes along- those changes are overwritten, so then we have to go through the painstaking process of trying to remember what “mini” changes we made along the way.

This is why we use “childthemes”! Just in case you are new to WordPress and don’t know what a childtheme is –> we create a childtheme that allows us to customise a WordPress parent theme to suit our needs or the needs of our client. When the parent theme is updated, then our customisations aren’t lost.

This isn’t an option for WordPress plugins, so minor tweaks can sometimes be made by adding code into the childtheme’s functions.php file- which is safe from updates to the parent theme.

Remember to always have a backup of your WordPress files before making any changes.

So here’s your code snippet to remove the following alert:

“Easily get updates, support, and one-click WPMU DEV plugin/theme installations right from in your dashboard – install the free WPMU DEV Dashboard plugin. (find out more)”

[php]
//add a custom css change to hide something in the dashboard
function change_css_hide_wpmu_dash()
{
echo ‘<style type="text/css"> .wp-admin div.error {display: none !important;}</style>’;
}
add_action(‘all_admin_notices’, ‘change_css_hide_wpmu_dash’);
[/php]

BEFORE:
Hide WPMU dashboard plugin alert

AFTER:
remove the alert - WPMU dashboard plugin

Filed under: WordPressTagged with: , ,

WordPress Related Posts Shortcode

So you’ve managed to get people to read your post, that’s fantastic. Traffic to your blog is the first step in promoting your blog or business online. But what about when they reach the end of the article? What happens next?

Well this is an ideal location to provide links to other pages on your site or even a “call to action”. Just don’t leave them hanging!

Also adding recent related posts to the end of your WordPress blog post, is great for internal linking, which is good for SEO.

Yes there are plenty of plugins out there that can add “Related Posts” to the end of your blog post, but it’s always a good idea to try to minimise the number of plugins when you can. Remember that the more plugins, the slower your blog will load. And a slow loading blog is a big NO-NO.

So here’s some code that will allow you to add a “Related Posts Shortcode” to your blog posts.

WordPress Related Posts Shortcode

[php]
<?php
/*related posts by tag shortcode */
function dtwd_related_posts_shortcode($atts){
extract(shortcode_atts(array(
‘count’ => ‘3’,
‘title’ => ‘More useful tips’,
), $atts));
global $post;
$current_cat = get_the_category($post->ID);
$current_cat = $current_cat[0]->cat_ID;
$this_cat = ”;
$tag_ids = array();
$tags = get_the_tags($post->ID);
if ($tags) {
foreach($tags as $tag) {
$tag_ids[] = $tag->term_id;
}
} else {
$this_cat = $current_cat;
}
$args = array(
‘post_type’ => get_post_type(),
‘numberposts’ => $count,
‘orderby’ => ‘date’,
‘order’ => ‘DESC’,
‘tag__in’ => $tag_ids,
‘cat’ => $this_cat,
‘exclude’ => $post->ID
);
$dtwd_related_posts = get_posts($args);
if ( empty($dtwd_related_posts) ) {
$args[‘tag__in’] = ”;
$args[‘cat’] = $current_cat;
$dtwd_related_posts = get_posts($args);
}
if ( empty($dtwd_related_posts) ) {
return;
}
$post_list = ”;
foreach($dtwd_related_posts as $dtwd_related) {
$post_list .= ‘<li><a href="’ . get_permalink($dtwd_related->ID) . ‘">’ . $dtwd_related->post_title . ‘</a></li>’;
}
return sprintf(‘
<div class="dtwd_related-posts">
<h4>%s</h4>
<ul>%s</ul>
</div> <!– .dtwd_related-posts –>
‘, $title, $post_list );
}
add_shortcode(‘dtwd_related_posts_sc’, ‘dtwd_related_posts_shortcode’);
?>
[/php]

Basically this php code defines a function called “dtwd_related_posts_shortcode” by accessing the current post’s data using “global $post”, then it identifies the current category and tags, grabs the related posts’ links and outputs them onto the page when the function is called.

If the current post doesn’t have tags, or is the only post with that tag, then the function searches for the recent posts from the current category.

Finally you can use this code by simply inserting the shortcode [dtwd_related_posts_sc] onto your post page.

Note that the defaults in the code are count=3 for number of related posts and title=”More useful tips” for the heading for the related posts. You can change this by inserting some attributes into the shortcode like this:
[dtwd_related_posts_sc title=”insert a new title in here” count=”insert a new number in here”]

Feel free to use this shortcode on your blog and amend as required. As you can see below, I am using it on this blog.

Filed under: WordPressTagged with: , , ,

Change the Default Gravatar in WordPress

Today’s tutorial is a simple trick to

changing the default gravatar in WordPress that shows up next to the comments.

Why do this, you might ask? Well there are 2 reasons, one of which you may not be aware of, and actually is surprisingly important.

But firstly lets make sure everyone knows what a gravatar is- the grey mystery man, the blue box with what looks like a power button inside it- that is a gravatar or a globally recognised avatar. These are the default images that load when a viewer leaves a comment on a blog.

If you see a custom image of the person, or a logo or another image, then this is because they have set up a Gravatar account. See here for more info on this.

So the first reason for changing the default is that default gravatars are pretty ugly up there on a blog in the comments. Wouldn’t it be much nicer to make your own customised image that ties in with your theme?

The second reason, and this is important, is that every time a gravatar image is displayed on your page it is sending out a new HTTP request to get that image. So if you have lots of comments, then that could be extra loading time accessing the gravatars from the gravatar site. It would be far better to have your own local image loading.

So how do we do it? Well it’s really simple.

Step 1: create a very small size image file. I used an image of 48px by 48px and saved it with a filesize of 2.76kb

Step 2: upload this image into your childtheme’s images folder (create one if it doesn’t already exist)

Step 3: add the code snippet below to your childtheme’s functions.php file. Replace NAME with the name of your Gravatar for displaying in the discussion settings in your wordpress admin and IMAGENAME.png with your filename.

[php]
add_filter( ‘avatar_defaults’, ‘newgravatar’ );
function newgravatar ($avatar_defaults) {
$myavatar = get_bloginfo(‘stylesheet_directory’) . ‘/images/IMAGENAME.png’;
$avatar_defaults[$myavatar] = "NAME";
return $avatar_defaults;
}
[/php]

Filed under: WordPressTagged with: ,

Customise WordPress footer content using footer hook

One way to adjust the default footer content with themes such as Genesis and Thesis, is to go in and delete the current theme footer code and add your own. However this often means you actually have to go into the parent theme’s footer.php file and make the hacks there. Not a good idea as those sorts of changes will be lost when you upgrade the theme.

Instead the better way, when possible, is to use your childtheme’s functions file and target the footer hook, remove what is being added by the Theme’s default settings, and then add your own customised content.

So how do you do this?

Well you need to identify the theme’s footer hooks and then use “remove_action”. For eg for Genesis:
[php]remove_action(‘genesis_footer’, ‘genesis_do_footer’);
[/php]
and for Thesis:
[php]remove_action(‘thesis_hook_footer’, ‘thesis_attribution’);
[/php]

then you need to create a function for your new footer content, say something like:
[php]function dtwd_custom_footer() {
?><p>&copy; <?php echo date(‘Y’); ?> <a href="<?php echo get_option(‘siteurl’); ?>" ><?php echo get_option(‘blogname’); ?></a></p>
<?php
}
[/php]
This creates a function called “dtwd_custom_footer” and then adds copyright symbol, current year; site URL and name of your blog.

Then you simply need to hook this function onto the theme’s footer hook, so in the example for Genesis theme, the complete code to add to the childtheme’s function file would look like this:

[php]
/** customise footer */
remove_action(‘genesis_footer’, ‘genesis_do_footer’);
function dtwd_custom_footer() {
?><p>&copy; <?php echo date(‘Y’); ?> <a href="<?php echo get_option(‘siteurl’); ?>" ><?php echo get_option(‘blogname’); ?></a></p>
<?php
}
add_action(‘genesis_footer’, ‘dtwd_custom_footer’);
[/php]

Filed under: WordPressTagged with: ,

How to Remove a Category from rss blog feed

One of the great things about WordPress is that it is an excellent tool for blogging. If you visit your blog page, you’ll see that, the WordPress Loop will automatically list recent posts from all your categories.

However sometimes you might not want a certain category to show up in the list of posts. This is where the “pre_get_posts” action is so useful. Perhaps you are using a particular category of posts for your image slider, and so you don’t want them to show up in the main page. Simply add the following code into your childtheme’s function.php file and replace with the IDs for the category you want to hide.

Note: This will hide that category from both your blog page and also from your RSS feed. (replace ’13’ with your category ID)

[php]
/** Remove category from feed and blog page */
function dtwd_exclude_category($query) {
if ( $query->is_feed || $query->is_home ) {
$query->set(‘category__not_in’, ’13’ );
}
return $query;
}
add_filter(‘pre_get_posts’, ‘dtwd_exclude_category’);
[/php]

Filed under: WordPressTagged with: ,

How to add Post and Page ID column to WordPress Admin

WordPress pages and posts all have a unique post ID.

This ID allows us to individually change the CSS styling of a particular post or page. It also is useful when working with certain WordPress plugins such as SEO and sitemaps.

So we may want to find out the ID and the easiest way is simply to hover over the post name when looking at the list of posts in the admin section. But another way to have this information readily available is to show the IDs as a new custom column.

post-ids

To add this new column, simply add these lines of code to your childtheme’ss functions.php file:

[php]
add_filter(‘manage_posts_columns’, ‘posts_columns_postpageID’, 5);
add_action(‘manage_posts_custom_column’, ‘posts_custom_columns_postpageID’, 5, 2);
add_filter(‘manage_pages_columns’, ‘posts_columns_postpageID’, 5);
add_action(‘manage_pages_custom_column’, ‘posts_custom_columns_postpageID’, 5, 2);
function posts_columns_postpageID($defaults){
$defaults[‘wps_post_id’] = __(‘post_page_ID’);
return $defaults; }
function posts_custom_columns_postpageID($column_name, $id){
if($column_name === ‘wps_post_id’){
echo $id; }

}
[/php]

Filed under: WordPressTagged with: ,