Top WordPress hacks of early 2010
The first months of 2010 have been extremely prolific in terms of WordPress hacks. In this article, I have compiled 10 new WordPress hacks that you should definitely add to your library.
Display an incrementing number on each post
I always loved how A List Apart numbers its posts. The following hack will let you do the same with your own blog, using a custom field.
Implementing this hack is quite simple. First, paste the following function into your functions.php file:
function updateNumbers() {
global $wpdb;
$querystr = "SELECT $wpdb->posts.* FROM $wpdb->posts WHERE $wpdb->posts.post_status = 'publish' AND $wpdb->posts.post_type = 'post' ";
$pageposts = $wpdb->get_results($querystr, OBJECT);
$counts = 0 ;
if ($pageposts):
foreach ($pageposts as $post):
setup_postdata($post);
$counts++;
add_post_meta($post->ID, 'incr_number', $counts, true);
update_post_meta($post->ID, 'incr_number', $counts);
endforeach;
endif;
}
add_action ( 'publish_post', 'updateNumbers' );
add_action ( 'deleted_post', 'updateNumbers' );
add_action ( 'edit_post', 'updateNumbers' );
Once done, you can display the post number with the following code. Note that it have to be used within the loop.
<?php echo get_post_meta($post->ID,'incr_number',true); ?>
Source: http://www.wprecipes.com/how-to-display-an-incrementing-number-next-to-each-published-post
Allow your contributors to upload files
If you’re like me, you have guest contributing articles on your blog and you might be annoyed that the contributor role doesn’t allow file uploads. Most blog posts need images to stand out of the crowd so
this hack is extremely handy: Just paste it on your function.php file and your contributors will be allowed to upload files in the WordPress dashboard. How cool is that?
if ( current_user_can('contributor') && !current_user_can('upload_files') )
add_action('admin_init', 'allow_contributor_uploads');
function allow_contributor_uploads() {
$contributor = get_role('contributor');
$contributor->add_cap('upload_files');
}
Source: http://www.wprecipes.com/wordpress-tip-allow-contributors-to-upload-files
Display “time ago” dates
Twitter has a very cool function which displays the elapsed time since a tweet has been published. What about doing the same with WordPress? Of course it’s possible!
This code just needs to be pasted in your functions.php file. Once you saved the file, posts that were published less than 24 hours ago will display “Published XX ago” instead of regular dates.
add_filter('the_time', 'timeago');
function timeago() {
global $post;
$date = $post->post_date;
$time = get_post_time('G', true, $post);
$time_diff = time() - $time;
if ( $time_diff > 0 && $time_diff < 24*60*60 )
$display = sprintf( __('%s ago'), human_time_diff( $time ) );
else
$display = date(get_option('date_format'), strtotime($date) );
return $display;
}
By the way, if you’re on Twitter do not hesitate to follow me!
Source: http://aext.net/2010/04/display-timeago-for-wordpress-if-less-than-24-hours/
WordPress navigation outside the loop
WordPress provides some functions which allow you to link to the next and previous posts. However, those functions have to be used within the loop. Jeff Starr, who wrote the Digging into WordPress book, has the solution to this problem.
Simply paste the code below on your single.php file, where you’d like to link to the next and previous posts. Or even better, put the code in a php file and then include it in your theme file.
<?php if(is_single()) { // single-view navigation ?>
<?php $posts = query_posts($query_string); if (have_posts()) : while (have_posts()) : the_post(); ?>
<?php previous_post_link(); ?> | <?php next_post_link(); ?>
<?php endwhile; endif; ?>
<?php } else { // archive view navigation ?>
<?php posts_nav_link(); ?>
<?php } ?>
Source: http://digwp.com/2010/04/post-navigation-outside-loop/
Disallow theme switching
If you’re like me, you’ve created WordPress themes for your clients and already face a problem: The client “explored” the WordPress dashboard and “accidentally” switched the theme.
Using WordPress actions, we can easily remove the “themes” menu and consequently prevent the risk of having a client switching the theme. The code below just has to be pasted in your functions.php. The “themes” menu will be removed once the file is saved.
add_action('admin_init', 'remove_theme_menus');
function remove_theme_menus() {
global $submenu;
unset($submenu['themes.php'][5]);
unset($submenu['themes.php'][15]);
}
Source: http://soulsizzle.com/quick-tips/stopping-clients-from-switching-their-wordpress-theme/
Get rid of unused shortcodes in your posts
WordPress shortcodes are extremely useful, but they have a weak point: If you use a shortcode in your posts and then stop to use it for some reason, the shortcode code (Like [shortcode] for example) will stay in your posts.
To get rid of unused shortcodes, you just have to execute this line of SQL code. This can be done using PhpMyAdmin or the SQL command line interpreter. Don’t forget to replace [tweet] by the unused shortcode you’d like to delete from your posts.
UPDATE wp_post SET post_content = replace(post_content, '[tweet]', '' );
Source: http://www.wprecipes.com/wordpress-tip-get-rid-of-unused-shortcodes
Switch WordPress theme programmatically
Recently, I worked on an interesting project where I had to switch the blog theme automatically. As the current WordPress theme name is saved in the wp_options table of your WordPress database, we can easily change it.
The cleanest way to do it is definitely to use the update_option() function, as shown in the function below. Paste it in your functions.php file.
function updateTheme($theme){
update_option('template', $theme);
update_option('stylesheet', $theme);
update_option('current_theme', $theme);
}
Once you’ve added the function to your functions.php file, you can call it wherever you need it:
<php updateTheme('default'); ?>
Modify WordPress dashboard footer text
Another good tip for those who create WordPress themes for clients is to modify the WordPress dashboard footer text, and add (for example) a link to your support forum. The only thing you have to do is to copy this code and paste it in functions.php:
function remove_footer_admin () {
echo "Your own text";
}
add_filter('admin_footer_text', 'remove_footer_admin');
Source: http://www.wprecipes.com/wordpress-tip-how-to-change-the-dashboard-footer-text
Programmatically Creating Posts in WordPress
If for some reason you need to programmatically insert posts in WordPress database, you’ll be amazed to see how easy is it. The wp_insert_post() takes an array of data as a parameter, and then return the post ID.
global $user_ID;
$new_post = array(
'post_title' => 'My New Post',
'post_content' => 'Lorem ipsum dolor sit amet...',
'post_status' => 'publish',
'post_date' => date('Y-m-d H:i:s'),
'post_author' => $user_ID,
'post_type' => 'post',
'post_category' => array(0)
);
$post_id = wp_insert_post($new_post);
Source: http://www.webmaster-source.com/2010/02/09/programmatically-creating-posts-in-wordpress
WordPress 3.0: Query custom post types
WordPress 3.0 should be released soon. And I don’t know about you, but personally, I can’t wait. Lots of exiting features are scheduled. One of them is particularly interesting in my opinion: the custom post types, which allow you to define a custom type for a post.
In order to be able to retrieve posts of a specific type from a WordPress database, you can use the following loop, which will get the albums post type:
<ul>
<?php global $wp_query;
$wp_query = new WP_Query("post_type=albums&post_status=publish");
while ($wp_query->have_posts()) : $wp_query->the_post(); ?>
<li><a href="<?php the_permalink() ?>" rel="bookmark"><?php the_title(); ?></a></li>
<?php endwhile; ?>
</ul>
Source: http://www.catswhocode.com/blog/8-useful-code-snippets-to-get-started-with-wordpress-3-0
Please note that I’m currently accepting freelance work; so if you need any kind of WordPress help, I’ll be happy to help you. Simply send me an email and I’ll get back to you.
For the Allow your contributors to upload files im using a Plugin. It allows me to create/edit in so many ways. It’s called Capability Manager.
Regards.
Social comments and analytics for this post…
This post was mentioned on Twitter by tejasluvs: Top WordPress hacks of early 2010 http://bit.ly/9GYOaz @catswhocode…
Nice tips
Disallowing theme switching really helps.
I really like the footer text modification hook and the custom post types.
Thanks for mentioning my hack to disable theme-switching.
Also, the code to allow contributor uploads originally came from me as well. So regarding Capability Manger, it’s a great plugin. I actually dug through it’s code a little bit when I was trying to figure out how to make this hack work. However, using a plugin to complete such a simple task adds unnecessary additional overhead. If you aren’t using any of the other features of Capability Manager, I would recommend sticking with these few lines of code.
=== popurls.com === popular today…
yeah! this story has entered the popular today section on popurls.com…
> Switch WordPress theme programmatically
This is unsafe as the input isn’t sanitized and is used to directly manipulate the database. Add a layer or two of abstraction for safety.
[...] am trying to code an incremental number to my post. As stated here, here and here. But does it work. Well see for yourself, nope. Still not. Dont know why. But like [...]
[...] Jung who runs CatsWhoCode.com has compiled a list of what he considers to be the top WordPress hacks so far in 2010. Among the list are code snippets [...]
[...] NorthSouthMedia had a nice article on blank comment problems and CatswhoCode had a piece on the Top WordPress hacks of early 2010 which I have to be honest, led to me wasting half my morning messing about with the [...]
Disallow theme switching: Instead of hiding the menu item, a more foolproof solution would be to remove the ‘switch_themes’ capability from every role. The Capability Manager plugin mentioned above allows you to do this.
Great list of WP hacks!
How can I programmatically create a post with image attachments, such as a thumbnail?
[...] Top WordPress hacks of early 2010 (tags: wordpress hacks) [...]
Neat tricks, especially showing “time ago”, numbered posts, and getting rid of unused shortcodes. Good job.
[...] tips and tricks to help you spring clean your Twitter account and ensure … 2 Tweets Top WordPress hacks of early 2010 The first months of 2010 have been extremely prolific in terms of WordPress hacks. In this [...]
Again you did magic, thanks for the hacks, i like that navigation outside the loop.
I really like the footer text modification hook and the custom post types. thanx
great contribution..this really help me out!
Awesome, this is exactly what I needed for a theme I’m working on!
Excellent tips! I especially like the one that disallows theme switching. I have a site that someone built for me on WordPress but, being an internet newbie, I accidentally messed up the theme when i was exploring all the different options in the administration panel. Apparently my webdesigner didn’t know this hack or he would have saved himself quite a headache =P
Thanks for this great article.
Getting rid of unused shortcodes can be extremely useful
[...] Top WordPress hacks of early 2010 — 9:05am via [...]
Very good collection of wordpress hacks. I have applied the time ago hack.
“Nice tips
Disallowing theme switching really helps.”
I agree. This is something I have been wanting to do since a long time. I just did not know HOW to do it, LOL
Great tips and pointers, WP is an easy to use and impressive programme for someone who is a novice like me.
[...] Top WordPress hacks of early 2010 Sehr interessante WordPress Hacks, z.B. nummerieren von Posts, Footer im Dashboard ändern, etc. [...]
[...] A couple nice WP hacks. [...]
[...] Top WordPress Hacks Of Early 2010 [...]
I didn’t know these things were possible with WordPress — I’m still at the “if I touch it I’ll break it” stage, but I desperately need to learn more. You’ve made my top ten reading list!
I’m amazed once again how well and how mysterious the results of squirrel surfing can be. I was looking for something entirely different, not even wp-related, and somehow, 3, 4, or 19 degrees later, I end up here and find great contributions for my tips and tricks cache.
Thanks for this right-on-time post.
As to the nifty addition for allowing those with “Contributor” status…
Here’s a follow-up question for you– Is there a shortcode, or addition, or function-addition, or heck, even a plug-in, that would allow for uploading of files from the user side, and not necessarily from the dashboard side?
I’d like to set up a theme/site where visitors can upload photos of their favorite park but without them needing to be familiar, or know anything about wordpress. Much like folk contribute photos to “…whyyourfat”, “notaparkingspace” and or “people of walmart” type sites.
Thanks in advance for any further illumination.
Yes, there are quite a few plugins that will allow this, CFormsII , TDOMiniForms and GravityForms all allow for uploading files from the user side.
WAW!! great hacks man..
Wow, best compliation of hacks I’ve seen in a while!:D
Really great list!
Disallow theme switching – will help me to solve some problems
Bookmarked, could come in handy soon.
Nice article and tips. Programmatically Creating Posts in WordPress is pretty simple and helpful.
Great collection of hacks! Especially twitter like “time ago” and navigation outside loop. Keep it up!
Awesome tips Jean! but i am still at the beginning of learning wordpress code, so it might take a week for me to understand this post LOL. Good job, thumbs up!
This is one hell of a good post !
My favorite is “Programmatically Creating Posts in WordPress”,I never knew it is going to be so easy.
[...] Source: Cats Who Code – Top WordPress Hacks of Early 2010 [...]
Really nice hacks. Thanks a lot Jean! =)
This is very very helpful. Thanks so much Jean for incorporating this list!
thanks for this nice post