10+ Extremely Useful Tricks for the WordPress Functions File


In your WordPress theme folder, there lays the most powerful theme file known as functions.php. As the name suggests, this file adds unique functions to your WordPress installation. This file works just like a plugin within your theme. So you can modify/override WordPress core features without editing any core file. If used correctly, this file can do wonders for your WordPress site. It also speeds up development because you can have all the codes at one place. In this article, we will share some of the most incredible and most wanted tricks for the WordPress functions.php file.

Note: This article is targeted for WordPress theme developers. You may need have some PHP knowledge for some hacks.

All codes in this article will be added to the functions.php theme file.

1. Add Google Analytics

Simply paste the code below and insert your Google Analytics where it says paste your Google Analytics. You can paste the code once in your functions.php file and never have to worry about it again. We are adding an action to the wp_footer, so it will automatically insert adsense codes wherever on all pages you have the wp_footer string.

<?php
add_action(‘wp_footer’, ‘add_googleanalytics’);
function add_googleanalytics() { ?>
// Paste your Google Analytics code from Step 6 here
<?php } ?>

2. Add a Favicon to your Blog

Every blog deserves to have its own identity. You can add this identity by adding the favicon code in your header.php file, or you can make it easier for yourself by simply adding the following code in your functions.php file.

// add a favicon to your
function blog_favicon() {
echo ‘wpurl’).’/favicon.ico” />’;
}
add_action(‘wp_head’, ‘blog_favicon’);

Now whenever you are developing the theme, just upload the .ico file in the root folder where the blog is uploaded. You can also modify the file destination if you like. Often theme developers forget to change the URL of the favicon. This can eliminate another worry.

3. Remove WordPress Version Number

You should always encourage your clients to upgrade to the latest version, so you don’t have this problem. But if you are working with a client that does not want to upgrade, then it is essential that you remove your WordPress version number from your WordPress header, RSS feeds, and all other locations. To do this, add the following code:

function yourwp_remove_version() {
return ”;
}
add_filter(‘the_generator’, ‘yourwp_remove_version’);

4. Add a Custom Dashboard Logo

When creating themes for a client, you can use this as one of the perks to the theme. All you have to do is paste the following code below:

//hook the administrative header output
add_action(‘admin_head’, ‘my_custom_logo’);

function my_custom_logo() {
echo ‘
<style type=”text/css”>
#header-logo { background-image: url(‘.get_bloginfo(‘template_directory’).’/images/custom-logo.gif) !important; }
</style>
‘;
}

6. Add Custom Dashboard Widgets in WordPress

You probably have seen widgets that numerous plugins and themes add in the WordPress dashboard. As a theme developer, you can add one yourself by pasting the following codes:

add_action(‘wp_dashboard_setup’, ‘my_custom_dashboard_widgets’);

function my_custom_dashboard_widgets() {
global $wp_meta_boxes;

wp_add_dashboard_widget(‘custom_help_widget’, ‘Theme Support’, ‘custom_dashboard_help’);
}

function custom_dashboard_help() {
echo ‘

Welcome to Custom Blog Theme! Need help? Contact the developer here. For WordPress Tutorials visit: anangpratika

.wordpress.com” target=”_blank”>Anang Pratika

‘;
}

Remember to change the email and add any other information you want.

7. Change the Default Gravatar in WordPress

Have you seen the default Mystery man avatar on blogs? Why waste the branding opportunity for your clients? You can replace the mystery man with a custom branded gravatar for your clients to give their site a unique touch. All you have to do is paste the following codes:

add_filter( ‘avatar_defaults’, ‘newgravatar’ );

function newgravatar ($avatar_defaults) {
$myavatar = get_bloginfo(‘template_directory’) . ‘/images/gravatar.gif’;
$avatar_defaults[$myavatar] = “anangpratika”;
return $avatar_defaults;
}

Don’t forget to upload a custom image to your theme’s image folder. Also change the name of the gravatar to their brand name. Once you upload the image and the functions, then visit: WP-Admin » Settings » Discussion

Your gravatar will show up as one of the options.

8. Dynamic Copyright Date in WordPress Footer

Often you will come across sites with outdated copyright dates. Some sites show the current year as their copyright date. Both of these are annoying, and it shows that the site designer was lazy. In order to give your users a little background info about your site, you should display the copyright date as such: © 2006 – 2010. We can do this by simply pasting the following code:

function comicpress_copyright() {
global $wpdb;
$copyright_dates = $wpdb->get_results(”
SELECT
YEAR(min(post_date_gmt)) AS firstdate,
YEAR(max(post_date_gmt)) AS lastdate
FROM
$wpdb->posts
WHERE
post_status = ‘publish’
“);
$output = ”;
if($copyright_dates) {
$copyright = “© ” . $copyright_dates[0]->firstdate;
if($copyright_dates[0]->firstdate != $copyright_dates[0]->lastdate) {
$copyright .= ‘-‘ . $copyright_dates[0]->lastdate;
}
$output = $copyright;
}
return $output;
}

Once you add this function, then open your footer.php file and add the following code wherever you like to display the dynamic copyright date:

<?php echo comicpress_copyright(); ?>

This function looks for the date of your first post, and the date of your last post. It then echos the years wherever you call the function.

9. Rewrite Guest Author’s Name with Custom Fields

Now many blogs are publishing other authors also known as guest authors. A lot of these guest authors are one time writers. In this scenario, it is not feasible to create user accounts for them. This trick will let you replace the author name to guest author with the use of a custom field. Simply paste the code below:

add_filter( ‘the_author’, ‘guest_author_name’ );
add_filter( ‘get_the_author_display_name’, ‘guest_author_name’ );

function guest_author_name( $name ) {
global $post;

$author = get_post_meta( $post->ID, ‘guest-author’, true );

if ( $author )
$name = $author;

return $name;
}

Now every time there is a guest post, simply add the custom field guest-author.

10. Enable Post Thumbnails in WordPress

Starting from WordPress 2.9, you can use the default built-in Thumbnails feature in your themes. To do this, you must enable the post thumbnails inside your functions.php file. Paste the code below:

add_theme_support( ‘post-thumbnails’ );

Then simply place the following code inside your loop where you want to display the thumbnail:

<?php the_post_thumbnail(); ?>

Advertisements

4 thoughts on “10+ Extremely Useful Tricks for the WordPress Functions File

  1. It is perfect time to make a few plans for the long run and
    it is time to be happy. I’ve learn this submit and if I could I wish to suggest you few attention-grabbing things or suggestions. Perhaps you can write subsequent articles relating to this article. I want to learn more things about it!

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s