After reading a post on bavotasan.com about the new wp_trim_words() function in WordPress, I took a bit of time this morning to update my get_better_excerpt() plugin for WordPress to version 2.0, which now makes use of the new function.
Oops, I lost thisismyurl.com
This morning I had a problem running WordPress Multisite, it turns out that it was a simple error with file permissions but somehow in the process of updating the network settings and moving files around, I managed to lose the theme for thisismyurl.com
Not to worry, the content is all OK but the site design … not so much.
As a result, the new and improved thisismyurl.com is really just the default TwentyEleven theme from WordPress while I try to decide what to do with the old theme, if you see anything funky on the site please let me know via Twitter!
Fetch Twitter Count for WordPress
Looking for an easy way to include your current Twitter count in WordPress? Here’s a simple piece of code I’m using to fetch and display the current Twitter count, it includes transient caching of one hour for your most recent Twitter count.
/**
* Summary returns the current follower count of a specific Twitter account, or FALSE if not found
* @param String $twitter_username the username you wish to get a Twitter count for
* @param Number $transient_cache the time in seconds to cache the transient (default is one hour)
* @return String an unformatted number of followers from twitter
*
* @author Christopher Ross (@thisismyurl)
* @version 1.0.0
*/
function thisismyurl_twitter_count( $twitter_username = 'thisismyurl', $transient_cache = 3600 ) {
$current_twitter_count = get_transient( 'thisismyurl_twitter_count' );
if ( empty( $current_twitter_count ) ) {
$xml = file_get_contents ( 'http://twitter.com/users/show/' . $twitter_username );
if ( $xml ) {
$twitter_profile = new SimpleXMLElement ( $xml );
$current_twitter_count = $twitter_profile->followers_count;
if ( !empty( $current_twitter_count ) )
set_transient( 'thisismyurl_twitter_count' , $current_twitter_count, $transient_cache );
} else {
return FALSE;
}
}
return $current_twitter_count;
}
To display your current Twitter count in your WordPress theme, include the code:
if ( function_exists( 'thisismyurl_twitter_count' ) ) echo thisismyurl_twitter_count( 'thisismyurl', 3600 );
If the count can not be returned, the function will return FALSE which allows error checking such as:
if ( function_exists( 'thisismyurl_twitter_count' ) ) {
$twitter_count = thisismyurl_twitter_count( 'thisismyurl', 3600 );
if ( $twitter_count )
echo 'Twitter Count:' . $twitter_count;
}
What do you think? Is there a way to improve this function?
Five simple rules to writing a better blog
Ernest Hemingway might have been onto something when he wrote that there is nothing to writing. All you do is sit down at a typewriter and bleed.
The same, not surprisingly holds true for writing on a blog.
First off, be honest. The Internet is a huge place but it’s easily searchable. If you don’t take the time to be honest, people will figure it out pretty quickly. The same goes for social media, but it’s particularly true with writing your blog. Follow some simple advice, it’s easier to tell the truth than to try to cover it up later.
Whenever possible, be assertive but not aggressive and learn the difference.
Be accurate. This isn’t the same as being honest, it’s important to realize that bloggers aren’t journalists but that we’re still responsible to our readership. If you’re going to claim the new iPhone 5′s battery life is only one hour, have the data to back it up or you’ll look like a fool (I have no idea how long the iPhone 5′s battery life will be by the way, it’s just an example). Likewise if you’re going to quote another blogger, make sure they’re accurate first.
Remember your manners and be courteous to your fellow bloggers. If you’re going to quote them, source them, reference their work, or even duplicate their ideas take the time to link back to them and give them and give them some credit. The web is a surprisingly small place, be polite about it.
Most importantly, if you want to write a successful blog then you have to be willing to give more than others. Put a little bit of yourself into every post you write, admit your own mistakes, and always shed a little blood on your MacBook Pro.
Responsive Photography Website Design
This week I decided to update my personal photography website and took advantage of a new responsive design template from c.Bavota, it wasn’t specifically developed to be a WordPress theme but was fairly easy to modify into a powerful WordPress theme. Continue reading
WordPress 3.4 allows for better theme file structures
I was pretty impressed reading Andrew Nacin’s most recent post on the changes to how WordPress processes theme files, as of WordPress 3.4 the theme allows page files to be located in sub directories, without the need to specify the directory.
So, instead of polluting the root directory with dozens of special cases you can now place those files in a sub-directory (but not two levels deep!).
Here’s an example structure:
/theme-directory
—-/pages
——–/page-about.php
——–/page-contact.php
There’s no need to worry about telling WordPress to find the files, as WordPress with automatically parse the directories for the page files (but only one level deep. Thanks for point it out Andrew, that’s a big improvement for structuring the theme!
How to change a WordPress theme with PHP
There are times when a programmer needs to change a theme using PHP rather than using the WordPress administration tool, this little piece of code will allow you to do that by placing it in your functions.php file.
add_action( 'template_redirect' , 'thisismyurl_change_theme_manually' );
function thisismyurl_change_theme_manually() {
if ( 'twentyeleven' != get_current_theme() )
switch_theme( 'twentyeleven', 'style.css' );
}
Update: Nacin (@nacin) pointed out a couple fixes for this piece of code. First, the get_current_theme() is depreciated, so get_stylesheet() is a better function to use and secondly, running this on the front end of the site isn’t the best idea.
With that in mind, here’s the revised code.
add_action( 'template_redirect' , 'thisismyurl_change_theme_manually' );
function thisismyurl_change_theme_manually() {
if ( 'twentyeleven' != get_stylesheet() && is_admin() )
switch_theme( 'twentyeleven', 'style.css' );
}
You can read more from Andrew Nacin at nacin.com.
Assign a Template to a Page and all Child Pages
An interesting question popped up the other day while I was developing a theme for a WordPress website. The client wanted to load a specific template for a specific page, as well as all of the child pages. For example, the About Us section but also the Contact Us (assuming it was a child of About Us).
If they’d just wanted to load a single page template, I would refer to the default hierarchal structure and simply create a theme file named page-[slug].php but how to do it for each of the subsection pages as well? On Friday when I wrote the a piece on redirecting WordPress based on the URL, it occurred to me that there are a couple really easy ways to do it.
Here’s my solution:
add_action( 'template_redirect','thisismyurl_about_template_override' );
function thisismyurl_about_template_override() {
if ( strpos( $_SERVER['REQUEST_URI'], '/about-us' ) > 0 ) {
include ( TEMPLATEPATH . '/page-about-us.php');
exit;
}
}
So what’s it do? Simply put, it waits until after WordPress has loaded everything it needs to load to run smoothly, then just before WordPress tries to load the theme file we intercept the logic and force it to load page-about-us.php instead.