How To’s & Tutorials10 WordPress Tricks For The Functions.php File

Among other themes in your WordPress folder, there is the most powerful of them all: the functions.php file. As you can already guess from the name, this file contributes to some of the unique functions of your WordPress installation, even if added as a plugin to your already existing theme. What it does is to allow you to override or modify critical WordPress features without having to drill deep and tackle core files. If used appropriately, functions.php can make your WordPress amazing and fortress-firm. At the same time, it will accelerate development by simply collecting all codes under one roof.


functions.php file

As expected, there is a multitude of incredible tricks and best practices to help you make the most of your WordPress functions.php file, and this article reveals the most wanted among them.


1. Customizing your dashboard logo

In case you’re designing a client’s theme, you can use this file to be the perk. What it takes is to paste this code inside the file:


//hook the administrative header output
add_action('admin_head', 'my_custom_logo');
functionmy_custom_logo() {
echo '

';
}
2. Disabling WordPress search

When working with WordPress as your CMS, you don’t really need the search function, and you can use functions.php to remove it from your design. Searching will still be possible, with the difference that there will be no sidebar for the function. This is the code you should use for the purpose:


functionfb_filter_query( $query, $error = true ) {

if ( is_search() ) {
$query->is_search = false;
$query->query_vars[s] = false;
$query->query[s] = false;

// to error
if ( $error == true )
$query->is_404 = true;
}
}

add_action( 'parse_query', 'fb_filter_query' );
add_filter( 'get_search_form', create_function( '$a', "return null;" ) );
3. Using RSS to control the availability of your posts

The most common error of all bloggers is that publish their posts right after completing them, and they start catching errors just after a minute or two. That’s why posts should be delayed at least 5 or 10 minutes, and be published on the RSS only after it is confirmed that there are no errors. In order to do it, add the following function:


functionpublish_later_on_feed($where) {
global $wpdb;

if ( is_feed() ) {
// timestamp in WP-format
$now = gmdate(‘Y-m-d H:i:s’);

// value for wait; + device
$wait = ‘10′; // integer

// http://dev.mysql.com/doc/refman/5.0/e...
$device = ‘MINUTE’; //MINUTE, HOUR, DAY, WEEK, MONTH, YEAR

// add SQL-sytax to default $where
$where .= ” AND TIMESTAMPDIFF($device, $wpdb->posts.post_date_gmt, ‘$now’) > $wait “;
}
return $where;
}
add_filter(‘posts_where’, ‘publish_later_on_feed’);
4. Customizing excerpt length

Excerpt length is intuitively capped at exactly 55 words, which puts an obstacle to the flexibility designers want and expect from WordPress. In order to customize excerpt length you should use the following function:


functionnew_excerpt_length($length) {
return 100;
}
add_filter('excerpt_length', 'new_excerpt_length');
5. Disabling RSS feeds

The RSS feed is not absolutely necessary for static WordPress websites, so in case you’re designing one you can consider disabling it with the following function:


functionfb_disable_feed() {
wp_die( __('No feed available,please visit our homepage!') );
}

add_action('do_feed', 'fb_disable_feed', 1);
add_action('do_feed_rdf', 'fb_disable_feed', 1);
add_action('do_feed_rss', 'fb_disable_feed', 1);
add_action('do_feed_rss2', 'fb_disable_feed', 1);
add_action('do_feed_atom', 'fb_disable_feed', 1);
6. Enabling a featured image function

According to the codex, a WordPress author can use the featured image function to pick representative images for his pages, posts, or post categories.


In order to enable the functionality, copy the following code and include it in the functions.php file:


add_theme_support( 'post-thumbnails' );

Most of the time, it is WordPress that defines thumbnail sizes, but we could also take matters in our hands and define sizes ourselves. No doubt most designers would be impressed by this option!


Take a magazine website as an example, where all featured images appear at least in 3 different sizes. The largest one, for instance, can be used for most recent posts, medium ones for less important posts, and a small one that will appear in other necessary locations.


You should use the add_image_size() function to instruct WordPress to make few copies of the featured image, and adjust them to the predefined sizes.


In order to do it, add the following code to the functions.php file:


// For regular size
add_image_size( 'regular', 400, 350, true );

// For medium size
add_image_size( 'medium', 650, 500, true );

// For the largest thumbnail
add_image_size( 'large', 960, '' );

Loading jQuery from Google CDN
// Smart jquery inclusion
add_action( 'init', 'jquery_register' );

// register from google and for footer
functionjquery_register() {

if ( !is_admin() ) {

wp_deregister_script( 'jquery' );
wp_register_script( 'jquery', ( 'https://ajax.googleapis.com/ajax/libs...' ), false, null, true );
wp_enqueue_script( 'jquery' );
}
}
7. Removing the security information of your WordPress version
// remove version info from head and feeds
functioncomplete_version_removal() {
return '';
}
add_filter('the_generator', 'complete_version_removal');

Restricting backend access to menu functions depending on usernames and role permissions


You can use this function to deactivate individual access to the admin menu, based on the permissions assigned to that particular username:


{
$restricted = array(__('Posts'),
__('Media'),
__('Links'),
__('Pages'),
__('Comments'),
__('Appearance'),
__('Plugins'),
__('Users'),
__('Tools'),
__('Settings')
);
end ($menu);
while (prev($menu)){
$value = explode(' ',$menu[key($menu)][0]);
if(in_array($value[0] != NULL?$value[0]:"" , $restricted)){unset($menu[key($menu)]);}
}// end while

}// end if
}
add_action('admin_menu', 'ah_remove_menus');
8. Adjusting the excerpt word length

As we already said, the usual excerpt length is 55 words, but you can always overwrite it with the following snippet:


functionnew_excerpt_length($length) {
return 150;
}

add_filter('excerpt_length', 'new_excerpt_length');
9. Showcasing dynamic copyright data in the footer space

Many websites neglect the importance of updating their copyright data, or include simply the current year because that’s the only thing the WordPress feature allows them to do. As often as it happens, this doesn’t qualify as a best practice, as patterns similar to © 2012 to 2016 look much prettier and more professional for websites that want to maintain their reputation. You’re supposed to do nothing else, but to add a simple code snippet to the functions.php file:


/* - */
functionah_dynamic_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;
}
10. Upgrading your author profile with author fields

The best thing about WordPress is that adding is just as simple as removing. We’re going to share one truly important code you could use to add input boxes to the author profile:


functionah_new_contactmethods( $contactmethods ) {
// Add Twitter
$contactmethods['twitter'] = 'Twitter';
//add Facebook
$contactmethods['facebook'] = 'Facebook';
return $contactmethods;
}
add_filter('user_contactmethods','ah_new_contactmethods',10,1);
 •  0 comments  •  flag
Share on Twitter
Published on August 11, 2016 02:03
No comments have been added yet.