Top 15 most Useful Tricks for your WordPress Functions File.

The functions file behaves like a WordPress Plugin, adding features and functionality to a WordPress site. You can use it to call functions, both PHP and built-in WordPress, and to define your own functions.

Importance of Functions File in WordPress

With this file, you can customize different sections of your website such as post excerpt, login page or other areas of your website. Functions.php file is stored with each theme in the theme’s subdirectory in wp-content/themes. It executes only when in the currently activated theme’s directory and applies only to that theme. If the theme is changed, the functionality is lost. Each theme has its own function file but only the functions.php file in the active affects how your site publicly displays. If your theme already has a function’s file, you can add a code to it. If not, you can create a plain text file named functions.php to add to your theme’s directory. A child theme can have its own functions.php. This child functions file can be used to augment or replace the parent theme’s function.
Let’s take a look at some extremely useful tricks for the WordPress functions file.

1. Uploading Additional Image Sizes in WordPress

WordPress automatically creates several image sizes when you upload an image. You can also create additional image sizes to use in your theme. Add this code your theme’s functions file.

add_image_size( 'sidebar-thumb', 120, 120, true ); // Hard Crop Mode
add_image_size( 'homepage-thumb', 220, 180 ); // Soft Crop Mode
add_image_size( 'singlepost-thumb', 590, 9999 ); // Unlimited Height Mode

This code creates three new image sizes with different sizes. You can tweak the code to meet your own requirements. You can display an image size in anywhere in your theme using this code.

<?php the_post_thumbnail( 'homepage-thumb' ); ?>

Related: How to increase the default file upload size limit in WordPress

2. Update WordPress URLs

If your WordPress login page keeps refreshing or you are unable to access admin area, then you need to update WordPress URLs.

One way to do this is by using the wp-config.php file. However, if you do that you will not be able to set the correct address on the settings page. The WordPress URL and Site URL fields will be locked and uneditable. If you want to fix this, then you should add this code to your functions file.

update_option( 'siteurl', 'http://onlineshouter.com' );
update_option( 'home', 'http://onlineshouter.com' );

Once you are logged in, you can go to Settings and set the URLs there. After that you should remove the code you added to the functions file, otherwise, it will keep updating those URLs any time your site is accessed.

3.Delay Posts in RSS Feed

Sometimes you may end up with a grammar or spelling mistake in your post. The error goes live and is distributed to your RSS feed subscribers. If you have email subscriptions on your WordPress blog, then those subscribers will get it as well. Simply add this code in your theme’s functions file.

function publish_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/en/date-and-time-functions.html#function_timestampdiff
        $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. Change Read More Text for Excerpts in WordPress

Do you want to change the text that appears after the excerpt? Simply add this code to your theme’s functions file.

function modify_read_more_link() {
    return '<a class="more-link" href="' . get_permalink() . '">Your Read More Link Text</a>';
}
add_filter( 'the_content_more_link', 'modify_read_more_link' );

5. Disable Login by Email in WordPress

WordPress allows users to login with username or email address. You can easily disable login by email in WordPress by adding this code to your functions file.

remove_filter( 'authenticate', 'wp_authenticate_email_password', 20 );

6.Dynamic Copyright Date in WordPress Footer

You can simply add copyright date by editing the footer template in your theme. However, it will not show when your site started and it will not automatically change next year.

You can use this code to add a dynamic copyright date in WordPress footer.

function wpb_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;
}
 After adding this function, you’ll need to open your footer.php file and add the following code wherever you like to display the dynamic copyright date:
<?php echo wpb_copyright(); ?>
 This function looks for the date of your first post, and the date of your last post. It then echoes the years wherever you call the function.

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

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 '<p>Welcome to New Blog Theme! Need help? Contact the developer <a href="mailto:yourusername@gmail.com">here</a>. For WordPress Tutorials visit: <a href="http://www.onlineshouter.com" target="_blank">onlineshouter</a></p>';
}

8. Change the Footer in WordPress Admin Panel

The footer in WordPress admin area shows the message ‘Thank you for creating with WordPress’. You can change it to anything you want by adding this code.

function remove_footer_admin () {

echo 'Fueled by <a href="http://www.wordpress.org" target="_blank">WordPress</a> | WordPress Tutorials: <a href="http://www.onlineshouter.com" target="_blank">onlineshouter</a></p>';

}

add_filter('admin_footer_text', 'remove_footer_admin');

9. Hide Login Errors in WordPress

Login errors in WordPress can be used by hackers to guess whether they entered wrong username or password. By hiding login errors in WordPress you can make your login area a bit more secure.

function no_wordpress_errors(){
  return 'Something is wrong!';
}
add_filter( 'login_errors', 'no_wordpress_errors' );
Now users see a generic message when they enter incorrect username or password.

 

10. Add Featured Images to RSS Feeds

The post thumbnail or featured images are usually only displayed within your site design. You can easily extend that functionality to your RSS feed with a simple function in your RSS feed.

function rss_post_thumbnail($content) {
global $post;
if(has_post_thumbnail($post->ID)) {
$content = '<p>' . get_the_post_thumbnail($post->ID) .
'</p>' . get_the_content();
}
return $content;
}
add_filter('the_excerpt_rss', 'rss_post_thumbnail');
add_filter('the_content_feed', 'rss_post_thumbnail');

11. Disable XML-RPC in WordPress

XML-RPC is a method that allows third-party apps to communicate with your WordPress site remotely. This could cause security issues and can be exploited by hackers.

Simply add this code to your functions file to turn off XML-RPC in WordPress:

add_filter('xmlrpc_enabled', '__return_false');

12. Remove WordPress Version Number

You should always use the latest version of WordPress. However, you may still want to remove the WordPress version number from your site. Simply add this code snippet to your functions file.

function wpb_remove_version() {
return '';
}
add_filter('the_generator', 'wpb_remove_version');

Want to white label your WordPress admin area? Adding a custom dashboard logo is the first step in the process.

First you’ll need to upload your custom logo to your theme’s images folder as custom-logo.png. Make sure your custom logo is 16×16 pixels in size.

After that you can add this code to your theme’s functions file.

function wpb_custom_logo() {
echo '
<style type="text/css">
#wpadminbar #wp-admin-bar-wp-logo > .ab-item .ab-icon:before {
background-image: url(' . get_bloginfo('stylesheet_directory') . '/images/custom-logo.png) !important;
background-position: 0 0;
color:rgba(0, 0, 0, 0);
}
#wpadminbar #wp-admin-bar-wp-logo.hover > .ab-item .ab-icon {
background-position: 0 0;
}
</style>
';
}
//hook into the administrative header output
add_action('wp_before_admin_bar_render', 'wpb_custom_logo');

14. Change the Footer in WordPress Admin Panel

The footer in WordPress admin area shows the message ‘Thank you for creating with WordPress’. You can change it to anything you want by adding this code.

function remove_footer_admin () {

echo 'Fueled by <a href="http://www.wordpress.org" target="_blank">WordPress</a> | WordPress Tutorials: <a href="http://www.onlineshouter.com" target="_blank">onlineshouter</a></p>';

}

add_filter('admin_footer_text', 'remove_footer_admin');

15.Show Total Number of Registered Users in WordPress

Do you want to show a total number of registered users on your WordPress site? Simply add this code to your theme’s functions file.

// Function to return user count
function wpb_user_count() { 
$usercount = count_users();
$result = $usercount['total_users']; 
return $result; 
} 
// Creating a shortcode to display user count
add_shortcode('user_count', 'wpb_user_count');
This code creates a shortcode that allows you to display a total number of registered users on your site. Now you just need to add this shortcode to [user_count] your post or page where you want to show the total number of users.

sarah ali

sarah ali

Sarah is a passionate writer and blogger. As an early adopter, she enjoys trying out new social media and Internet tools along with WordPress plugins and Web apps.
sarah ali

Leave a Reply

Your email address will not be published.