Being an aspiring WordPress developer, it’s essential for you to understand the nuts and bolts of functions.php file. It’s an integral part of WordPress core and plays a huge role when it comes to developing or modifying a theme. Functions.php file works just like a plugin within your theme and gives you enough control over the look and functionality of a specific theme.
With just a little code or modifications, you can speed up the development and execute a great deal of functions in the manner most effective.
When used correctly, this file can do wonders within your website. Plus, it also accelerates the performance of your WordPress website development because you have all the codes at a common place. In this post, I am going to show you 10 most practical ways and tricks to make the most out of your functions.php file.
In this post, I am going to show you 10 most practical ways and tricks to make the most out of your functions.php file.
1. Adding Post Thumbnails
Although majority of the themes come packed with support for adding post thumbnails, or we can say featured images, you can integrate the same functionality within your functions.php file with this way:
add_theme_support( ‘post-thumbnails’ );
Below we have mentioned a loop which must be included in the loop where you want the thumbnail to be displayed on your website.
<?php the_post_thumbnail(); ?>
2. Google Analytics
Adding Google Analytics within your functions.php file will call you to add a code to your page where you have the footer. The code will look this like:
<?php
add_action(‘wp_footer’, ‘add_googleanalytics’);
function add_googleanalytics() { ?>
// add Google Analytics code here
<?php } ?>
In the first line, we have used the action (‘wp_footer’, ‘add_googleanalytics), which depicts that it will add Google Analytics in the footer. This is called as “hook”. To utilize it, grab your Google analytics code first and replace it with the line “// add Google Analytics code here ”. Also, not to forget about eliminating the comment marks.
3. Making the Copyright Date
There is no need to change the copyright year every time the new year arrives. Just simply add the below mentioned code within your functions.php file, which helps you logically organizes copyright year and gives your website an updated look.
<strong”>function copyright($start_year, $site_name) {</strong”>
$year = date(‘Y’);
echo “© Copyright “;
echo $start_year;
if($start_year != $year) echo “-$year”;
echo “, $site_name, All Rights Reserved.”;
}
One you add the code, you need to locate the footer file and place the same code there. Also, remember that the year add should be the year in which the website started and not the current year.
<div class=’copyright’>
<?php copyright(2014, “Add the name of your company”); ?>
</div>
4. Creating an Excerpt Box on Pages
WordPress users use excerpt boxes to provide a summary or a small description of their post. Apart from this, an excerpt box serves two purpose: 1) it replaces the full content when the option to display summary is selected in the Dashboard; 2) it can be easily displayed in places where instant post summaries have loading priority than the full content. Some of these places include: search results, Tag archives, Category Archives, and Author archives.
So, to create an excerpt box, add the following snippet within your functions.php file.
add_post_type_support( ‘page’, ‘excerpt’ );
After adding the code, you can see an excerpt box on the editing page. If it’s not there, you can check the Screen options and see if the right checkbox is selected.
5. Updating the Default Gravatar
Update the look of your gravatar by replacing the mystery man with a custom branded gravatar to freshen up the look of your theme. Just place the following code and you are ready to go.
add_filter( ‘avatar_defaults’, ‘newgravatar’ );
function newgravatar ($avatar_defaults) {
$myavatar = get_bloginfo(‘template_directory’) . ‘/images/gravatar.gif’;
$avatar_defaults[$myavatar] = “your website or company name”;
return $avatar_defaults;
}
After this, make sure you upload the custom image to your theme’s folder. Also change the name ane everything to make the gravatar compatible with your brand name.
6. Adding a Custom Dashboard Logo
Custom dashboard logo can truly breathe a new life to your theme. It’s very simple to achieve with the help of the following theme.
//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>
‘; }
7. Providing Support to Custom Post Formats
WordPress users often don’t understand the difference between post formats and post types. There are 9 post types included into WordPress, you can even see all of them on your post edit page. But before activating them, you need to provide them support in the functions.php file. This is how you can add support to custom post formats.
add_theme_support(‘post-formats’, array( ‘aside’, ‘chat’, ‘gallery’, ‘image’, ‘link’, ‘quote’, ‘status’, ‘video’, ‘audio’));
8. Customizing Footer
You can easily customize your footer to give it a unique personality of your brand. You can add the following code in the functions.php file and customize it easily.
function remove_footer_admin () {
echo “add the text you want”;
}
add_filter(‘admin_footer_text’, ‘remove_footer_admin’);
9. Creating a Maintenance Page
There are times when your website goes into some kind of maintenance or internal improvements for upgrades. At that time, you don’t want your website to be available for the general audience. For this you need to create a perfect maintenance mode page so that your visitors don’t feel annoyed. This is extremely simple with the help of the following code in your functions.php file.
function maintenance_mode() {
if ( !current_user_can( ‘edit_themes’ ) || !is_user_logged_in() ) {wp_die(‘Maintenance.’);}
}
For removing the maintenance page, the below mentioned lines can be added:
add_action(‘get_header’, ‘maintenance_mode’);
10. Specifying The Character Limit
If you want to specify the number of characters to be displayed on your comments, you can either provide an error message or add the following code below the functions.php file.
new GW_Minimum_Characters( array(
‘form_id’ => 530,
‘field_id’ => 1,
‘min_chars’ => 15,
‘max_chars’ => 545,
‘min_validation_message’ => __( ‘Sorry! You need more than %s characters.’ ),
‘max_validation_message’ => __( ‘Sorry! You need less than %s characters.’ )
) );
All said and done, this is how you can levarage the power of functions.php file for planning and developing the potential of your WordPress site.
This is a guest post shared by Sophia Phillips.