Add the_shortlink()

Posted by The Doctor on Jul 22, 2010 with No Comments | Short Link
in Functions
as ,

I believe there are many reasons for themes to use this function or add it to your WordPress web site. I will briefly discuss a couple of the ones that come to mind. Note, if you are using the default permalink structure this may not be for you as the_shortlink() creates a URL that looks just like it.

If you are new to WordPress and are still experimenting with the permalink structure to best fit your needs, you might consider using this function to create internal links for your site. Using the URL generated by the_shortlink() will always resolve correctly, using the verbose URL of the permalink structure may cause issues if you change the structure and do not manually edit all the internal links you have previously posted.

Please note, by “internal links” I am refering to the links used within the content of a post or page on your site pointing to another post or page on your site. A WordPress installation has the “smarts” to correctly resolve the links it generates if you change the permalink structure but it will not go back and “fix” anything you manually typed yourself as part of your page or post entry.

There is great potential for the user, the designer, and the developer using this function. I recommend adding it to all themes. Here is the most basic default usage of the_shortlink():

<?php the_shortlink(); ?>

This is the code I have decided to use on this site:

<?php the_shortlink( __('Short Link'), '', ' &#124; ', '' ); ?>

… which you can see just after the comments or subscribe link in the post meta details.

Of course you can always use the ‘Get Shortlink’ button found on the page and post administration panels to get your shortlink, but making use of this single line of code allows your readers to gain that same benefit, too.

NB: If you have the WordPress.com Stats plugin installed and activated, the_shortlink() function will use the wp.me link shortening service; and, it will retain all of the usefulness I noted above.

Upgrade wp_list_categories() to wp_nav_menu()

Posted by The Doctor on Jul 20, 2010 with No Comments | Short Link
in Functions
as , , ,

In this third installment in the upgrade to wp_nav_menu() series of posts, I will be dealing with moving from wp_list_categories() to wp_nav_menu().

Let’s start with the same basic outline and list the default options for each function. We will start with wp_nav_menu() 1 first:

wp_nav_menu( array(
    'menu'              => '',
    'container'         => 'div',
    'container_class'   => '',
    'container_id'      => '',
    'menu_class'        => 'menu',
    'menu_id'           => '',
    'echo'              => true,
    'fallback_cb'       => 'wp_page_menu',
    'before'            => '',
    'after'             => '',
    'link_before'       => '',
    'link_after'        => '',
    'depth'             => 0,
    'walker'            => '',
    'theme_location'    => ''
    ) );

We follow with wp_list_categories() 2, 3 which is often wrapped in a <ul class=”menu”> tag:

<ul class="menu">
wp_list_categories( array(
    'include'           => '',
    'exclude'           => '',
    'exclude_tree'      => '',
    'child_of'          => 0,
    'hide_empty'        => 1,
    'orderby'           => 'name',
    'order'             => 'ASC',
    'use_desc_for_title'=> 1,
    'number'            => NULL,
    'hierarchical'      => true,
    'show_count'        => 0,
    'pad_counts'        => 0,
    'style'             => 'list',
    /* 'style' set to list "creates list items for an unordered list" */
    'show_option_all'   => '',
    'show_option_none'  => __('No categories'),
    'show_last_update'  => 0,
    'feed'              => '',
    'feed_type'         => '',
    'feed_image'        => '',
    'current_category'  => 0,
    'taxonomy'          => 'category',
    'title_li'          => __( '' ),
    /* 'title_li' set to '' for menus from the default 'Categories' */
    'echo'              => 1,
    'depth'             => 0,
    'walker'            => 'Walker_Category'
    ) );
</ul>

Here is the break-down of the wp_list_categories() default options from above:

  • 'include' – not used
  • 'exclude' – not used
  • 'exclude_tree' – not used
  • 'child_of' – not used
  • 'hide_empty' – not used
  • 'orderby' – not used
  • 'order' – not used
  • 'use_desc_for_title' – not used
  • 'number' – not used
  • 'hierarchical' – not used
  • 'show_count' – not used
  • 'pad_counts' – not used
  • 'style' – not used
  • 'show_option_all' – not available
  • 'show_option_none' – not available
  • 'show_last_update' – not available
  • 'feed' – not available
  • 'feed_type' – not available
  • 'feed_image' – not available
  • 'current_category' – not available
  • 'taxonomy' – not available
  • 'title_li' – not used
  • 'echo' – similar to wp_nav_menu(); true versus 1 as value
  • 'depth' – same as wp_nav_menu()
  • 'walker' – see below for additional notes

Although there are great many options available to be used with wp_list_categoires() most are not used (as noted in the list above) in place of the user interface of wp_nav_menu() found under Appearance | Menu in the dashboard. The options that are marked as “not available” are currently not directly supported by wp_nav_menu() without the application of filters. The last few options remaining match up almost exactly with wp_page_nav() in a similar fashion as was shown with wp_list_pages().

Just like the wp_list_pages() consideration, the wp_nav_menu() fallback_cb option may be set to 'fallback_cb' => 'wp_list_categories'. This backward compatibility may also be addressed with a custom function as was noted, too.

Note: In most cases you would leave the wp_nav_menu() 'walker' option set to its default NULL although wp_list_categories() uses its own default walker class.

Upgrade wp_list_pages() to wp_nav_menu()

Posted by The Doctor on Jul 16, 2010 with 1 Comment | Short Link
in Functions
as , , ,

Following in the series dealing with upgrading to the wp_nav_menu() function in WordPress 3.0 from menus derived from the template tags: wp_page_menu(); wp_list_pages(); and, wp_list_categories(). This post will be covering the options involved in updating to wp_nav_menu() from wp_list_pages().

Like the last post in the series we will look at the default arguments used by each function, starting with wp_nav_menu() 1:

wp_nav_menu( array(
    'menu'              => '',
    'container'         => 'div',
    'container_class'   => '',
    'container_id'      => '',
    'menu_class'        => 'menu',
    'menu_id'           => '',
    'echo'              => true,
    'fallback_cb'       => 'wp_page_menu',
    'before'            => '',
    'after'             => '',
    'link_before'       => '',
    'link_after'        => '',
    'depth'             => 0,
    'walker'            => '',
    'theme_location'    => ''
    ) );

The following is a default “menu” version of wp_list_pages() 2, 3, often wrapped within a <ul class=”menu”> tag:

<ul class="menu">
<?php wp_list_pages( array(
    'sort_column'       => 'menu_order, post_title',
    'include'           => '',
    'exclude'           => '',
    'exclude_tree'      => '',
    'child_of'          => 0,
    'show_date'         => '',
    'date_format'       => get_option('date_format'),
    'title_li'          => __(''),
    /* 'title_li' set to '' for menus from the default 'Pages' */
    'authors'           => '',
    'echo'              => 1,
    'link_before'       => '',
    'link_after'        => '',
    'depth'             => 0,
    'walker'            => ''
    ) ); ?>
</ul>

Here is the break-down of the wp_list_pages() default options from above:

  • 'sort_column' – not used
  • 'include' – not used
  • 'exclude' – not used
  • 'exclude_tree' – not used
  • 'child_of' – not used
  • 'show_date' – not used
  • 'date_format' – not used
  • 'title_li' – not used, see below for additional notes
  • 'authors' – not used
  • 'echo' – similar to wp_nav_menu(); true versus 1 as value
  • 'link_before' – same as wp_nav_menu()
  • 'link_after' – same as wp_nav_menu()
  • 'depth' – same as wp_nav_menu()
  • 'walker' – same as wp_nav_menu()

Similar to wp_page_menu(), the ‘sort_column’, ‘include’, ‘exclude’, ‘exclude_tree’, and ‘child_of’ are replaced via the end-user’s specific choices. The ‘show_date’ and its related ‘date_format’ option, as well as the ‘author’ argument, are not available within the default wp_nav_menu() structure. Once again, the balance of the default options in wp_list_pages() matches the defaults found in wp_nav_menu().

Also consider setting the wp_nav_menu() fallback_cb option to 'fallback_cb' => 'wp_list_pages'. This backward compatibility may also be addressed with a custom function as posted by Nicolas Kuttler.

Note: If the 'title_li' => __('') option was being used it may also require the removal of the wrapping <ul> tag in the existing menu structure created with wp_list_pages().

Upgrade wp_page_menu() to wp_nav_menu()

Posted by The Doctor on Jul 12, 2010 with No Comments | Short Link
Last modified by The Doctor on July 25, 2010
in Functions
as , , ,

WordPress version 3.0 has been live for a while and a lot of people want to make use of the wp_nav_menu() functionality but they are using one of the older template tags: wp_page_menu(); wp_list_pages(); or, wp_list_categories().

This post will be dealing with function arguments in common between wp_nav_menu() and wp_page_menu(). Follow-up posts will be covering wp_list_pages() and wp_list_categories(). I will not be covering the specific use for each function’s arguments; the idea is to help show how these options correlate to one another.

Let’s start with the full default of wp_nav_menu() 1 explicitly displayed:

wp_nav_menu( array(
    'menu'              => '',
    'container'         => 'div',
    'container_class'   => '',
    'container_id'      => '',
    'menu_class'        => 'menu',
    'menu_id'           => '',
    'echo'              => true,
    'fallback_cb'       => 'wp_page_menu',
    'before'            => '',
    'after'             => '',
    'link_before'       => '',
    'link_after'        => '',
    'depth'             => 0,
    'walker'            => '',
    'theme_location'    => ''
    ) );

Followed by the same explicit defaults of wp_page_menu() 2, 3:

wp_page_menu( array(
    'sort_column'       => 'menu_order, post_title',
    'include'           => '',
    'exclude'           => '',
    'show_home'         => false,
    'menu_class'        => 'menu',
    'echo'              => true,
    'link_before'       => '',
    'link_after'        => ''
    ) );

Now onto updating wp_page_menu() to wp_nav_menu(), which turns out to be rather simple.

Here is the break-down of the wp_page_menu() default options from above:

  • 'sort_column' – not used
  • 'include' – not used
  • 'exclude' – not used
  • 'show_home' – not used (see bonus section below)
  • 'menu_class' – same as wp_nav_menu()
  • 'echo' – same as wp_nav_menu()
  • 'link_before' – same as wp_nav_menu()
  • 'link_after' – same as wp_nav_menu()

First off ‘sort_column’, ‘include’, and ‘exclude’ are replaced by the end-user’s choices using the UI of wp_nav_menu() found under Appearance | Menu in the dashboard. The balance of the default options are the same in wp_nav_menu(), with the exception of 'show_home' => false which is the “bonus” content.

Upgrading from wp_page_menu() to wp_nav_menu() may be as easy as a simple find and replace operation.

Bonus – Although the default in wp_page_menu() does not show a home page link you can add a custom menu item in wp_nav_menu() if you want one to appear. You may need to edit your theme to remove the orginal “home” link if one was being generated before upgrading.
NB: You may also need to add this line of code to your ‘functions.php’ template file to add custom menu support to your theme: add_theme_support( 'menus' );

Strike Through Text

Posted by The Doctor on Apr 15, 2010 with No Comments | Short Link
in Tips
as , , , ,

A simple how-to for creating a strike-through text effect with an HTML tag or CSS.

Strike-through text using the <del> tag.

OK, that was really easy. Here is a reference at W3Schools that goes into all the details.

If you are using WordPress you will find the del button on the HTML editor screen. Of course the editor button conveniently drops a date and time stamp attribute into the <del> tag as well, so the above line looks like this under the page:

<del datetime="2010-04-15T13:41:32+00:00">Strike-through text</del>

The ABC button of the visual editor uses inline CSS to create the strike-through effect. Here is an example of what the above line would look like under the page:

<span style="text-decoration: line-through;">Strike-through text</span>

To see an example of how strike-through text can be used feel free to read the latest revision of WordPress 3.0 Navigation Menu Styles.

Modified Post Function

Posted by The Doctor on Mar 18, 2010 with 1 Comment | Short Link
in Functions
as , , ,

Here is a simple bit of code you can drop into your functions.php file then use in any of your templates where you want to display a short statement telling when a post was last modified.

// Modified Post - requires WordPress version 3.0 or greater
function modified_post(){
  global $wp_version;
  if (version_compare($wp_version, "2.999", ">")) {
    /* If the post date and the last modified date are different display modified post details */
    if ( get_the_date() <> get_the_modified_date() ) {
      echo '<div class="modified-post">'; /* CSS wrapper for modified post details */
      echo 'Last modified by ' . get_the_modified_author() . ' on ' . get_the_modified_date();
      echo '</div><!-- .modified-post -->';
    }
  }
}

Of course, you may have noticed the check for WordPress version 3.0 or greater. This is required as the function get_the_date() on line 6 is currently only found in versions of WordPress starting at 3.0

Once the above code has been added to your functions.php file and you are using a version of WordPress greater than or equal to 3.0 then all you need to do is drop the following line of code inside the loop of the template file you want to display the modified post details on.

<?php modified_post(); ?>

Also included is a CSS class element for easy styling. The code can be easily edited to add more elements, too.

Now, what good is this little bit of code? Have a look around and see it in action.

WordPress 3.0 Navigation Menu Styles

Posted by The Doctor on Mar 17, 2010 with No Comments | Short Link
Last modified by The Doctor on April 15, 2010
in Functions, Tips
as , , , , ,

Let’s jump right in …

Using the default definition from the register_sidebar() function found in ../wp-includes/widgets.php of WordPress version 3.0-alpha 3.0-beta12 (see below):

	$defaults = array(
		'name' => sprintf(__('Sidebar %d'), $i ),
		'id' => "sidebar-$i",
		'description' => '',
		'before_widget' => '<li id="%1$s" class="widget %2$s">',
		'after_widget' => "</li>\n",
		'before_title' => '<h2 class="widgettitle">',
		'after_title' => "</h2>\n",
	);

… and the default Navigation Menu widget, the following CSS style elements are generated:

  • #nav-menu-<widget instance>
  • .widget
  • .widget_nav_menu
  • .widgettitle

… with these elements generated specifically by the wp_nav_menu() function found in ../wp-includes/default-widgets.php of WordPress version 3.0-alpha 3.0-beta12:

  • .menu-<menu name>-container2
  • #menu-<menu name>
  • .menu
  • #menu-item-<unique identifier*>
  • .menu-item-type-<types: page, category, Custom>1
  • .menu-item2
  • .menu-item-type-<post_type, custom, or taxonomy>2
  • .menu-item-object-<page, or category>2
  • .current_page_item
  • .sub-menu1

This is just an initial listing of my observations as of the March 16, 2010 3.0-alpha version of WordPress. Look for future updates; and, please feel free to make note of your own observations below.

Bonus – A suggested addition to style.css for theme developers:

/* WordPress 3.0 Navigation Menu default widget */
.widget_nav_menu .menu {margin: 0;}
.widget_nav_menu .menu li {
  display: block;
  float: none;
  /* text-align: left; */
}
*N.B. – each new menu item generates its own post ID in the WordPress database ‘posts’ table. This appears to be in a similar fashion to post revisions.
    Notes:

  1. Mar 21, 2010
    • menu item type class further defined
    • sub-menu class noted
  2. Apr 15, 2010 (3.0-beta1 updates)
    • additional menu classes defined and/or modified
    • new menu container class noted

Dynamic Copyright

Posted by The Doctor on Feb 16, 2010 with 6 Comments | Short Link
Last modified by The Doctor on June 20, 2010
in Functions, Tips
as , , ,

At the bottom of most blogs you will find the commonplace copyright notice that may look something like this:

Copyright © 2010 WP First Aid All rights reserved.

Which begs the question, what about those posts made before the year 2010? This is where a dynamic copyright function would come in very handy. It would be more correct to have this display in the footer:

Copyright © 2009-2010 WP First Aid All rights reserved.

Most WordPress themes will include, at a minimum, code that will generate a current year copyright statement, perhaps something along these lines:

<?php _e( 'Copyright &copy; ' ); ?>
<?php echo date( 'Y' ); ?>
<strong> <?php bloginfo( 'name' ); ?> </strong>
<?php _e( 'All rights reserved.' ); ?>

To make this code into a function and add some dynamics lets start with a simple PHP construct:

function dynamic_copyright() {
  // code goes here
}

To get the first year a post was written on the blog we need to find the first post. This can be done with this code snippet:

  $all_posts = get_posts( 'order=ASC' );
  $first_post = $all_posts[0];
  $first_date = $first_post->post_date_gmt;

We started by getting all of the posts with the get_posts() function. We assigned the first post, which is found at index 0 (zero) to its own variable. Then we assign that post’s date (in GMT) to its own variable.

We now have isolated the date of the first post on the blog and can perform further operations on it. The basic dynamic part of the function is complete. If the first post is removed and the “new” first post has a different date, then the above snippet will simply use the new post’s date.

The balance of the function is only a slight modification of the original “minimum” sample code from above:

  _e( 'Copyright &copy; ' );
  echo substr( $first_date, 0 ,4 ) . "-" . date( 'Y' );
  echo ' <strong>' . get_bloginfo( 'name' ) . '</strong> ';
  _e( 'All rights reserved.' );

The key is highlighted at line 6. We echo, or display, the first four characters of the $first_date string which is the year; add a hyphen; and, then call a standard PHP function to add the current year as a four digit representation.

Here is the entire dynamic_copyright() function, including comments:

function bns_dynamic_copyright() {
  /* Get all posts */
  $all_posts = get_posts( 'post_status=publish&order=ASC' );
  /* Get first post */
  $first_post = $all_posts[0];
  /* Get date of first post */
  $first_date = $first_post->post_date_gmt;

  /* Display common footer copyright notice */
  _e( 'Copyright &copy; ' );
  /* Display first post year and current year */
  if ( substr( $first_date, 0, 4 ) == date( 'Y' ) ) {
  /* Only display current year if no posts in previous years */
    echo date( 'Y' );
  } else {
    echo substr( $first_date, 0, 4 ) . "-" . date( 'Y' );
  }
  /* Display blog name from 'General Settings' page */
  echo ' <strong>' . get_bloginfo( 'name' ) . '</strong> ';
  _e( 'All rights reserved.' );
}

Of course, this function is compliments of BuyNowShop.com and has their customary bns prefix as it will be appearing in all of their themes.

Just like I did, you can copy and paste the entire sample bns_dynamic_copyright() code above into your theme’s functions.php file; then, replace the code in your theme’s footer.php file that generates your old static copyright notice with this line of code:

<?php bns_dynamic_copyright(); ?>

Your theme will now display a dynamic copyright notice as you continue your writings for many years to come. Congratulations!

Calendar Default Widget Broken

Posted by The Doctor on Jan 22, 2010 with No Comments | Short Link
Last modified by The Doctor on March 17, 2010
in News
as , , ,

I just happened to notice this today. The calendar default widget that comes with WordPress is having issues. It appears to work fine on the initial visit to a blog’s home page, but if you start reading the archives, especially using the calendar as a navigation tool you may find the calendar using some very oddly structured months.

Here is the trac ticket: http://core.trac.wordpress.org/ticket/11414, so it is being addressed.

In the meantime, I have decided to install the Ajax Calendar plugin as a substitute. There are many other calendar plugins available but this one suits my current needs.