Here I’m going to share some very useful things that we can do in wordpress. We will do all these things into functions file.
Whtat is Functions File ?
The functions.php file which is known as functions file, is a WordPress theme file. This file allow developers to define theme features and functions. This file acts just like a WordPress plugin and can be used to add your own custom code in WordPress.
So here are some very useful things that we can do in WordPress functions file:
Remove WordPress Version Number
To remove wordpress version number from your site, add below code to functions file (functions.php):
function wpb_remove_version() { return ''; } add_filter('the_generator', 'wpb_remove_version');
Disable Search Feature in WordPress
function fb_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;" ) );
Disable Login by Email in WordPress
WordPress allows users to login with username or email address both. But you can disable login by email. Add below code into functions file to disable login by email:
remove_filter( 'authenticate', 'wp_authenticate_email_password', 20 );
Add extra fields to profile page
Let see how we can add ‘facebook’ and ‘twitter’ fields into user profile page:
Add below code to do this:
function wpb_new_contactmethods( $contactmethods ) { // Add Twitter $contactmethods['twitter'] = 'Twitter'; //add Facebook $contactmethods['facebook'] = 'Facebook'; return $contactmethods; } add_filter('user_contactmethods','wpb_new_contactmethods',10,1);
Add Additional Image Sizes in WordPress
WordPress automatically creates several image sizes when we upload an images. We can also create more image sizes when we need. Add below code in functions file to create image size:
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
Here is how we can use these new created image size:
<?php the_post_thumbnail( 'homepage-thumb' ); ?>
Add a Custom Dashboard Logo
First upload custom logo to 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');