Web Development

9 Tweaks to Functions.php that can Tailor Your WordPress Site

Being an aspiring WordPress developer, it’s essential for you to figure out the nuts and bolts of functions.php file. It’s an integral part of the WordPress core and plays a huge role when it comes to developing or modifying a theme. Functions.php file is a core file 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.

[gads]
Tweaks to WordPress Function.php file

In this post, I’ll throw some light on the 9 most practical ways and tricks to make the most out of your functions.php file.

Adding Post Thumbnails

Although the majority of the themes comes 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:

[cc lang=”php”]
add_theme_support( ‘post-thumbnails’ );
[/cc]

Below we have mentioned a loop which must be included in the loop where you want the thumbnail to be displayed on your website.

[cc lang=”php”]
the_post_thumbnail();
[/cc]

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 like this:

[cc lang=”php”]

// add Google Analytics code here

[/cc]

In the first line, we have used the action [code](‘wp_footer’, ‘add_googleanalytics)[/code], 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.

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 organize copyright year and gives your website an updated look.

[cc lang=”php”]
function copyright($start_year, $site_name) {
$year = date(‘Y’);
echo “© Copyright “;
echo $start_year;
if($start_year != $year) echo “-$year”;
echo “, $site_name, All Rights Reserved.”;
}
[/cc]

[gads]

The one you add the code, you need to locate the footer file and place the same code there. Also, remember that the year adds should be the year in which the website started and not the current year.

[cc lang=”php”]

[/cc]

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 purposes:
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.

[cc lang=”php”]
add_post_type_support( ‘page’, ‘excerpt’ );
[/cc]

After adding the code, you can see an excerpt box on the editorial page. If it’s not there, you can check the Screen options and see if the right checkbox is selected.

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.

[cc lang=”php”]
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;
}
[/cc]

After this, make sure you upload the custom image to your theme’s folder. Also change the name on everything to make the gravatar compatible with your brand name.

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 themes.

[cc lang=”php”]
//hook the administrative header output
add_action(‘admin_head’, ‘my_custom_logo’);

function my_custom_logo() {
echo ‘

‘;
}
[/cc]

Providing Support for Custom Post Formats

WordPress users often don’t understand the difference between post formats and post types. There are 9 post types included in 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.

[cc lang=”php”]
add_theme_support(‘post-formats’, array( ‘aside’, ‘chat’, ‘gallery’, ‘image’, ‘link’, ‘quote’, ‘status’, ‘video’, ‘audio’));
[/cc]

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.

[cc lang=”php”]
function remove_footer_admin () {
echo “add the text you want”;
}
add_filter(‘admin_footer_text’, ‘remove_footer_admin’);
[/cc]

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.

[cc lang=”php”]
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’);
[/cc]

All said and done, this is how you can leverage the power of the functions.php file for planning and developing the potential of your WordPress site.

Shares:

Leave a Reply

Your email address will not be published. Required fields are marked *