All WordPress themes come with a “Very Powerful” function.php file.

It is like a plugin, and allows you to do a lot of great things on WordPress.

In this article I will explain it to you File Function what?

And the tips are very nice file function.php this.

tips file functions in WordPress

What is a file function in WordPress?

The function file, commonly called the functions.php file, is a file available in the WordPress theme.

All free and premium themes must have this file.

The purpose of the file is to allow developers to build features and functionality for the theme.

So this file is like a WordPress plugin and you can theme that code into the theme itself.

Now you must be wondering between the plugin and the fucntion.php file. Which one is better?

While the functions.php file is more convenient, pluin is usually better and has more features.

Simply because the plugin can work on any theme (regardless of theme)

On the other hand, the functions.php file only works on that theme, when changing the other interface.

You will have to copy / paste that code again into the new theme.

But if you want to add some small functionality to the theme.

This is still a very good and useful way to use.

32+ Useful tricks when using File Function

1. Delete the version number of WordPress you are using

You should always update to the latest version for WordPress.

However, if you still want to delete the version of WP you are using.

Simply add the following code:

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

2. Replace your own logo in the Dashboard

You want to customize the look of your WordPress dashboard.

And customizing your own logo for your dashboard is one of the first steps.

First you will need to upload it to WordPress with a size of 16 × 16 pixels.

Then add the following code to the function.php file

3. Change the Footer section in WordPress Admin

You can change the end of your WordPress interface by pasting the code below into the Functions file.

function remove_footer_admin () {
 
echo 'Fueled by WordPress | Thu Thuat WordPress Dieuhau

';
 
}
 
add_filter('admin_footer_text', 'remove_footer_admin');

You may also want to remove Footer Link Powered By WordPress, please refer to it.

READ  3 Best Related Post Widget For Blogger

4. Add custom widget dashboard in WordPress

You've probably seen a variety of widgets in Dashboard when you first log in, right?

You can completely create such widgets by the following:

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 '

Welcome to Custom Blog Theme! Need help? Contact the developer here. Thu Thuat WordPress: DieuHau

';
}

Here are the 25 most useful widgets for Website, that you should know.

5. Change the default Gravatar on WordPress

Do you want to replace the default avatar image on WordPress.

You can replace the default image with a personalized branded gravatar (globally regconised avatar).

You only need to paste the code below into the Functions file:

add_filter( 'avatar_defaults', 'dh_new_gravatar' );
function dh_new_gravatar ($avatar_defaults) {
$myavatar = 'http://example.com/wp-content/uploads/2019/01/dh-default-gravatar.png';
$avatar_defaults($myavatar) = "Default Gravatar";
return $avatar_defaults;
}

Also change the name of gravatar to their brand name.

After you've uploaded your photo and this feature, go to WP-Admin »Settings» Discussion Your gravatar will be displayed.

6. Change “Copyright Date” under Footer

For users to know some basic information about the website.

You can display the copyright terms, for example, © 2006 – 2010.

We can do this by pasting the code below:

function dh_copyright() {
global $dh;
$copyright_dates = $dh->get_results("
SELECT
YEAR(min(post_date_gmt)) AS firstdate,
YEAR(max(post_date_gmt)) AS lastdate
FROM
$dh->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, open the footer.php file

And add the code below where you want to display this license time:

This code simply finds the first and last day you posted.

And show it where you want.

7. Randomly change the Background Color in WordPress

Do you want to randomly change the background color on your WordPress.

After each visit or reload the page?

Just add this code to the functions file

function dh_bg() { 
$rand = array('0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f');
$color ="https://dieuhau.com/#".$rand(rand(0,15)).$rand(rand(0,15)).$rand(rand(0,15)).
$rand(rand(0,15)).$rand(rand(0,15)).$rand(rand(0,15));
echo $color;
}

Next, you will need to edit the header.php file in the theme. Find tags

and add the following code:
 style="background-color:">>

8. Update WordPress URL

If your WordPress login page is constantly refreshed, and cannot log in to the admin.

You may need to update your WordPress URL.

Another way to do this is to use the wp-config.php file.

However, if you do, you will not be able to place the correct address on the settings page.

The WordPress URL and website URL fields will be locked and not editable.

If you want to fix this, add the following code to the function.php file.

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

Replace the example with your domain name, then login okay.

Go to settings and adjust the URL there. Then delete the above code. (No, it will continue to update the URL).

9. Add own image size in WordPress

WordPress automatically generates dimensions when uploading images to media.

And you can also create separate sizes for your theme.

By using the following code:

add_image_size( 'sidebar-thumb', 240, 240, 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 snippet creates 3 new image sizes with different sizes.

Feel free to tweak the code above to suit you

You can display image sizes anywhere in the theme with this code.

If you want to prevent automatic resizing of images in WordPress, learn to read through this article.

10. Add new Navigation Menu to the interface

WordPress allows theme developers to locate navigation menus and then display them.

Add this code to the theme's function file, to create a new menu in your theme.

function dh_custom_new_menu() {
  register_nav_menu('my-custom-menu',__( 'My Custom Menu' ));
}
add_action( 'init', 'dh_custom_new_menu' );

Now you can go to Appearance »Menu and you will see My Custom Menu

Add this code to your theme, where you want to display the navigation menu.

 'my-custom-menu', 
    'container_class' => 'custom-menu-class' ) ); 
?>

Read more instructions on adding icons to the Menu in WordPress.

11. Add author author personal information field

If you want to create a more versatile author page, you can add some boxes for author information.

The code below will help you show more boxes of information about facebook and twitter.

But you can use it to add any other cells you want.

function my_new_contactmethods( $contactmethods ) {
// Add Twitter
$contactmethods('twitter') = 'Twitter';
//add Facebook
$contactmethods('facebook') = 'Facebook';

return $contactmethods;
}
add_filter('user_contactmethods','my_new_contactmethods',10,1);

The above feature will add some information like this

You can place the box into the author.php template by adding the code below:

twitter; ?>

** This guide only works with versions 2.9 and above.

12. Add a new widget or sider to the theme

This is still one of the classic and most commonly used distortions.

Paste the following code into the functions.php file:

// Register Sidebars
function custom_sidebars() {
 
    $args = array(
        'id'            => 'custom_sidebar',
        'name'          => __( 'Custom Widget Area', 'text_domain' ),
        'description'   => __( 'A custom widget area', 'text_domain' ),
        'before_title'  => '




', 'after_title' => '

', 'before_widget' => ' ', ); register_sidebar( $args ); } add_action( 'widgets_init', 'custom_sidebars' );

Now you need to go to Appearance »Widget. You will see a new widget bar.

To display this sidebar hat widget on your theme. Add the following code:



13. Add RSS feed under footer of page

Have you ever seen blogs adding ads to their RSS Feed under each post?

You can do it with an extremely simple function. Paste the following code into it:

function dieuhau_postrss($content) {
if(is_feed()){
$content = 'This post was written by dieuhau '.$content.'Check out Dieuhau';
}
return $content;
}
add_filter('the_excerpt_rss', 'dieuhau_postrss');
add_filter('the_content', 'dieuhau_postrss');

14. Add Thumbnails articles to RSS Feeds

Post thumbnail or featured image feature, usually only displayed in the website interface

But you can extend this feature to an RSS feed with a simple piece of code.

function rss_post_thumbnail($content) {
global $post;
if(has_post_thumbnail($post->ID)) {
$content = '

' . get_the_post_thumbnail($post->ID) .
'

' . get_the_content();
}
return $content;
}
add_filter('the_excerpt_rss', 'rss_post_thumbnail');
add_filter('the_content_feed', 'rss_post_thumbnail');
add_filter('the_content_feed', 'rss_post_thumbnail');

15. Delete the error message on the login page

When you enter the wrong password or username, you will receive an error message on the login page.

If this message indicates to the user that they have entered the wrong password or username.

Then it will make it easier for hackers to attack your website.

To restrict this you can add the following to the file function.php

function no_wordpress_errors(){
  return 'Something is wrong!';
}
add_filter( 'login_errors', 'no_wordpress_errors' );

Now, when an error appears, you will only see blank cells.

16. Disable login by email on WordPress

WordPress allows you to login with both user name and email.

You can easily disable login by email by adding the following code to the funcion file

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

17. Turn off the search feature on WordPress.

If you want to disable the search function on WordPress

Please theme the following code into the function.php file.

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;" ) );

18. More timeout for posting RSS Feed

Sometimes you miss out on some grammar or spelling mistakes in your posts.

And you cloned that post to your RSS Feed.

If you allow email subscriptions on your WordPress blog, those subscribers will also receive it.

To minimize this error, add timeout after each publish

function publish_later_on_feed($where) {
 
    global $dh;
 
    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, $dh->posts.post_date_gmt, '$now') > $wait ";
    }
    return $where;
}
 
add_filter('posts_where', 'publish_later_on_feed');

In this code I have set the time to 10 minutes. Change it according to your preference.

19. Add the word “Read More” after the quote in WordPress

Do you want to read more read, after the excerpts on WordPress.

Simply add the following code to some function files in the theme.

function modify_read_more_link() {
    return 'Your Read More Link Text';
}
add_filter( 'the_content_more_link', 'modify_read_more_link' );

20. Turn off the RSS Feed function on WordPress

Not all websites need RSS Feed, if not you need to delete that function with the following code:

function fb_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);

21. Change the snippet length in WordPress

WordPress defaults to a maximum of 55 words. If you want to change this number, add the following code:

function new_excerpt_length($length) {
return 100;
}
add_filter('excerpt_length', 'new_excerpt_length');

Replace 100 with the desired snippet length.

22. Add an Admin account to WordPress

If you forgot both the Admin account email and the password, then you can add another Admin account.

By inserting the following code into the function.php file

function dh_admin_account(){
$user = 'Username';
$pass = 'Password';
$email = 'email@domain.com';
if ( !username_exists( $user )  && !email_exists( $email ) ) {
$user_id = wp_create_user( $user, $pass, $email );
$user = new WP_User( $user_id );
$user->set_role( 'administrator' );
} }
add_action('init','dh_admin_account');

Remember to replace the username and password with your information, after successful login.

Please delete the above code. Or you can refer to the Admin account in WordPress with MySQL.

23. Delete the Welcome screen from the WordPress Dashboard

The welcome panel is a screen that appears on the WordPress Dashboard.

It provides some useful information for new users.

You can easily delete it, just by the following code:

remove_action('welcome_panel', 'wp_welcome_panel');

24. Show the total number of registered users in WordPress

If you want to display the total number of registered users on your website.

Simply add the following code to the function.php file

// Function to return user count
function dh_user_count() { 
$usercount = count_users();
$result = $usercount('total_users'); 
return $result; 
} 
// Creating a shortcode to display user count
add_shortcode('user_count', 'dh_user_count');

This code snippet allows you to display the total number of registered users on your site.

Now just add the short_code (user_count), go to your post or page where you want to display.

25. Exclude specific categories from RSS Feed

Do you have a specific category removed from the RSS Feed?

Just add the following code to the function.php file

function exclude_category($query) {
    if ( $query->is_feed ) {
        $query->set('cat', '-5, -2, -3');
    }
return $query;
}
add_filter('pre_get_posts', 'exclude_category');

26. Activate shortcode in Widget Text

By default, WordPress will not be able to run shortcodes in text widgets.

To do that, just add the following code:

// Enable shortcodes in text widgets
add_filter('widget_text','do_shortcode');

Continue…..

Read more :

Leave a Reply

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