Add the_shortlink()
in Functions
as the_shortlink, WordPress
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'), '', ' | ', '' ); ?>
… 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.
Strike Through Text
in Tips
as CSS, del, how-to, HTML, strikethrough
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
in Functions
as get_the_date, modified_post, version_compare, WordPress
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.
Dynamic Copyright
as bns_dynamic_copyright, dynamic_copyright, get_posts, WordPress
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 © ' ); ?> <?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 © ' ); 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 © ' );
/* 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!
Your Username Can Be Changed
as change, how-to, login, username, WordPress
Changing a username in WordPress cannot be done within the WordPress interfaces, but it is still easy to do … providing you have access to the WordPress database.
NB: This tip is offered under the premise you are using a self-hosted installation of WordPress and your web host provider has installed the cPanel application to allow you to manage your web site.
Let’s start …
- Access the cPanel for your web site; and, locate the phpMyAdmin applet, it is generally found in the Databases section.
- Start phpMyAdmin.
- Locate and access your WordPress database.
- Find the wp_users table
Next, click on the left most icon to browse the users in the wp_users table.
Then, click on the “pencil” icon beside the user you wish to edit. This will display the screen we will be using to change the username.
Now, edit the user_login (and the user_nicename) to your new login username. Click on the “GO” button in the bottom right corner (not shown in the image) of the edit screen. Voila!
You can now change the username in a WordPress self-hosted installation. Well Done!
PS: Applying this technique using other methods to access the database is possible, this one only deals with using the cPanel / phpMyAdmin combination.
PPS: Remember to make back-ups! Always!









