Archive

Archive for the ‘Wordpress’ Category

Create A Tag Cloud Page In WordPress

April 28th, 2009 No comments

A tag cloud is a name for a collection of keywords that are displayed as a big block of text. Usually the most commonly occurring keyword is the largest, and the least commonly occurring keyword is the smallest. Tag clouds are a neat way of allowing your users to navigate your content in a different way, simply be letting them look over the cloud and linking each keyword to sections of your site that contain that word.

WordPress comes with this function built in and is already available as a widget. However, after adding a few hundred posts this tag cloud can get rather large and this widget can stretch your sidebar to stupid proportions. A way around this is to create a page that can be used to display this large tag cloud. The first step is to create a page template that will be used to run the correct WordPress function to display the tags. This is done by making a copy of your normal page template (call it tagpage.php) and adding the following code at the top.

<?php
/*
Template Name: Tags
*/
?>

When you go to create a page you will see a list of available templates to select from, one of these will be called Tags. Add in your title and some descriptive text and select this template.

Next, we need to add a call to the wp_tag_cloud() function, this basically prints out a small tag cloud. Add the following code into your page template so that is fits in with the rest of your design.

<?php wp_tag_cloud(); ?>

As with most WordPress functions, wp_tag_cloud() allows you to add lots of parameters to change the output of the function. There are lots of different options available, but the main ones that will be used are as follows:

  • smallest – This designates the font size to be used for the least commonly occurring tag. The units are by default in 'pt' or points, but this can be changed via the unit parameter.
  • largest – This designates the font size to be used for the most commonly occurring tag.
  • unit – Unit of measure as pertains to the smallest and largest values. This can be any CSS length value, e.g. pt, px, em, %; default is pt (points).
  • number – This tells the function how many tags to include in the tag cloud. The default is 45, but to print out all tags just use 0.

These parameters are given as an encoded string, for example, to change the number of tags printed from the default of 45 to 20 use the following code.

<?php wp_tag_cloud('number=20'); ?>

Multiple parameters are separated by the & character.

<?php wp_tag_cloud('number=20&largest=200'); ?>

Take a look at the WordPress manual for more information about the wp_tag_cloud() function

To demonstrate I have created a tag cloud page on Talk In Code, using the following code to create the cloud.

<?php wp_tag_cloud('smallest=7&largest=50&number=500'); ?>

Finally, there is also a function called wp_generate_tag_cloud() that has the same functionality as wp_tag_cloud() but will always return the HTML string that makes the tag cloud.

Displaying WordPress Authors

February 26th, 2009 2 comments

WordPress has a couple of rarely used functions that allow author information to be displayed for the current post and a list of all of the authors on the blog.

Adding a written by message to your posts is not difficult at all. Just use the the_author_posts_link() function inside the post loop.

<?php the_author_posts_link(); ?>

This function shouldn’t be confused with the the_author_link() function that prints out the link in the author’s profile.

The second way of printing out author information is by using the wp_list_authors() function. This can take a number of arguments, but the simplest use of it is as follows:

<ul>
<?php wp_list_authors(); ?>
</ul>

This should print out a list of the authors on your blog. There is one very important thing I should mention here. This function will only print out an authors name if they have written a post and are not the default admin account. I tried to print out the authors on a test blog and after a few minutes of frustration I looked at the function call itself on line 460 of the file author-template.php in the directory wp-includes. The function specifically excludes authors that haven’t written any posts but you can turn this on if you give it the right parameter.

The wp_list_authors() function takes a number of arguments in the form of a string of arguments. The available arguments are as follows:

  • optioncount: Print out the number of posts for each author. The default is 0 (false).
  • exclude_admin: Print out the blog admin user. The default is 1 (true)
  • show_fullname: Use the full name of the author instead of their nickname. Default is 0 (false).
  • hide_empty: Show users who don’t have any posts. The default is 1 (true). Note that even if a user with no posts is printed they will not be linked.
  • echo: Immediately echo the list of authors or return as a variable. The default is 1 (true).
  • feed: If set then a link to an RSS feed for that author is printed, along with the text given. The default is not to print anything.
  • feed_image: This is a link to a RSS icon, it works in the same way as the feed parameter, but will override it.

To use the arguments you need to pass them to the function in the form of a string or arguments like this.

<?php wp_list_authors('exclude_admin=0&hide_empty=0&feed=Feed'); ?>

Now that you have a link of authors being printed out it is time to look at where the links go to. Each author has an ID associated with them, and this is appended to the URL using the author tag. If you have permalinks turned on then the authors name is used.

When clicked on WordPress will attempt to display the author information using a template file called author.php. If this is not present then it will display a list of the posts using the archive.php template file. Finally, if this is not present then WordPress will default to the index file.

Your author.php file should contain the following lines so that you can access the author informaiton.

<?php
if(isset($_GET['author_name'])) :
 $curauth = get_userdatabylogin($author_name);
else :
 $curauth = get_userdata(intval($author));
endif;
?>

The $curauth variable now contains an author object that can be used to access the various different data fields associated with authors. The following snippet will print out the authors name, profile and homepage, if this information is not filled in then nothing will be printed.

<h2><?php echo $curauth->first_name . ' ' . $curauth->last_name; ?> (<a href="<?php echo $curauth->user_url; ?>"><?php echo $curauth->user_url; ?></a>)</h2>
<p><?php echo $curauth->user_description; ?></p>

You can also include a normal loop on this page to print out every post that the user has written.

For more information on the author templates (and a full list on the attributes available in the author object) take a look at the author templates section on the WordPress site.

Turn Off WordPress Revisions

February 24th, 2009 1 comment

WordPress has a nice little revisions feature that will allow you to revert to a previous version of a post if you don’t like the current edit. However, the drawback of this feature is that it is not always needed and it fills the post table full of stuff you will never need. Fortunately, turning this feature off isn’t too much of a pain. All you need to do is add the following line of code to your wp-config file, just below the DB_COLLATE line.

define('WP_POST_REVISIONS', 0);

You can also set the autosave interval here to something greater than the default of 60 seconds. It is possible to do this in the wp-config file since version 2.5.0.
define('AUTOSAVE_INTERVAL', 123);

If you want to get rid off all of the post revisions from your post table then you can use the following SQL query.

DELETE FROM wp_posts WHERE post_type = 'revision';

Adding A Sortable List To WordPress

February 19th, 2009 No comments

A sortable list is simply a list of items that can be dragged and dropped to alter the order of those elements.

There are two sortable lists available in WordPress, one from the JQuery framework and one from the Scriptaculous framework. For a sortable list you will need a list, so here is a simple one.

<ul id="sortcontainer">
<li class="sortable">Item 1</li>
<li class="sortable">Item 2</li>
<li class="sortable">Item 3</li>
<li class="sortable">Item 4</li>
</ul>

JQuery Sortable

The JQuery sortable control is actually a plugin and not part of the standard JQuery framework. The first thing we need to do is add in the correct JavaScript libraries that we need to use. We do this with the wp_enqueue_script() WordPress function. The are quite a few different things that are needed so you can use an array to pass in all of the options.

wp_enqueue_script( array("jquery", "jquery-ui-core", "interface", "jquery-ui-sortable", "wp-lists", "jquery-ui-sortable") );

Here is the JavaScript code that is needed to create the sortable list.

<script type="text/javascript">
 jQuery(function($) {
 
 var smpSortable;
 var smpSortableInit = function() {
  try { // a hack to make sortables work in jQuery 1.2+ and IE7
   $('#sortcontainer').SortableDestroy();
  } catch(e) {}
  smpSortable = $('#sortcontainer').Sortable( {
  accept: 'sortable',
  onStop: smpSortableInit
  } );
 }
 
 // initialize sortable
 smpSortableInit();
});
</script>

For more information on the sort of options available have a look at the JQuery Sortables documentation.

Scriptaculous Sortable

To use the Scriptaculous sortable list we add in the scriptaculous-dragdrop library using the wp_enqueue_script() WordPress function.

wp_enqueue_script("scriptaculous-dragdrop");

Here is the code you need to create the sortable list, you need to include this on the page below the list.

<script type="text/javascript">
  Position.includeScrollOffsets = true;
  Sortable.create('sortcontainer',{
   tag: 'li',
   scroll: window
  });
</script>

For more information on the sort of options available have a look at the Scriptaculous Sortables documentation.

If you only want to run the wp_enqueue_script() function in the admin section of WordPress you can use the is_admin() function. This returns true if the code is run in the admin section, and false if run on the front end. Using this function you can stop JavaScript libraries being included when you don’t use them.

Adding Code To WordPress Blogs And Comments

February 5th, 2009 No comments

WordPress is a pretty neat blogging platform, but it falls over quite spectacularly when trying to write code in posts. I write a lot of code for Talk In Code and so I have understand what needs to be encoded to make code examples work.

For code example on Talk In Code I use the <code> tag and I encode the following characters.

  • < into &lt;
  • > into &gt;
  • " into &quot;
  • ' into &#39;

Note: You must be in HTML mode in your WordPress editor or everything will be double encoded.

If these characters are left in then WordPress will either keep them "as is" (ie, a <br /> will cause a line break) or it will convert them into non standard characters. For example, typing a ' (single quote) is straightforward, but when your users come to copy and paste the code to try it for themselves they find that the characters WordPress gives them cause the examples to fail. So every time you type a ' you have to encode it using &#39;. The following example shows why typing a single quote will break your code examples.

echo ‘Hello World’;

The same thing applies to double quotes, as in the following example.

echo “Hello World”;

WordPress will also try to guess what you are doing and add in tags where you don’t want them. The effect of this is to break your code tags if you leave any space in them. Take the following example of a 4 line snippet of code, with a blank line between line 2 and line 4.

line 1
line 2

line 4

This is because WordPress will see the blank line and try to add some tags in to make it look like it thought you wanted it. In order to stop this you need to put in a &nbsp; (non breaking space) character on any blank lines that you have. The following example fixes the previous example.

line 1
line 2
 
line 4