5 Useful Functions for Your WordPress Website

5 Useful Functions for Your WordPress Website

WordPress is the favorite CMS (Content Management System) for bloggers and companies because it is easy to use. Also, customizing it is a big part of the platform’s appeal. That’s why more than 35% of websites use WordPress.

To properly customize your website, though, learning the most important WordPress functions is essential. We share five of these functions with you. Two of these five functions are for speed optimization, another for security, and the other two are the most commonly used functions.

You can also use plugins for these simple functions. However, each plugin has many unnecessary files and the plugins you install on your website slow down the loading speed of your website. So it is necessary to install as few plugins as possible.

Activating Classic Editor

If you have been using WordPress for a long time, you may have gained some habits; the classic editor. When you install WordPress, Gutenberg Editor (default page builder) is installed as the default editor. However, a lot of users find it hard to adjust to and would prefer to keep the classic editor.

There are many plugins available to disable the Gutenberg Editor and reactivate the classic editor. If you don’t want to write code or are not very familiar with WordPress files, you can use a plugin to reactivate the Classic Editor. Thankfully, there’s a plugin by the WordPress core team which allows you to use the classic editor even on WordPress 5.0 or later. But the more plugins you install on WordPress, the slower the loading speed of your website will be. Therefore, using fewer plugins will increase the performance of your website and will be scored better by search engines.

Activating the Classic Editor (without using plugins)

After logging into the WordPress Admin Panel, go to Appearance > Theme Editor. Make sure your theme is selected and open the functions.php file. Paste the following code at the bottom of the functions.php file and click the update file button. You can now use the classic editor while editing your posts and pages.

// Activating Classic Editor
add_filter('use_block_editor_for_post', '__return_false');

Remove Query Strings from Static Sources in WordPress

You will notice an instruction to remove query strings when you test the performance of your website with GTMetrix or Google PageSpeed. So what exactly is a query string? Simple: it is a version of a file. For example:

domain.com/style.css?ver=4.6 

?ver=4.6 is a query string. Query string is only important for dynamic resources. That’s why we need to remove query strings created for static resources. Thus, the loading speed of our website will increase. Remember that a slow website can undermine your SEO performance and the overall user experience. Therefore, it is highly recommended that you remove query strings from static resources in WordPress.

After logging into the WordPress Admin Panel, go to Appearance > Theme Editor. Make sure your theme is selected and open the functions.php file. Paste the following code at the bottom of the functions.php file and click the update file button. You will no longer notice an instruction to remove query strings.

// Remove query string from static files
function remove_cssjs_ver( $src ) {
    if( strpos( $src, '?ver=' ) )
    $src = remove_query_arg( 'ver', $src );
    return $src;
}
add_filter( 'style_loader_src', 'remove_cssjs_ver', 10, 2 );
add_filter( 'script_loader_src', 'remove_cssjs_ver', 10, 2 );

Hiding WordPress Version For Security

One of the most popular WordPress website hacking methods used by hackers: Exploiting a known vulnerability on an old version of WordPress, plugin or theme. To keep your WordPress website safe, you need to hide the WordPress version.

So there are two things you have to do:

1) Keep your WordPress website and plugins up to date.

2) Hide your version of WordPress. 

After logging into the WordPress Admin Panel, go to Appearance > Theme Editor. Make sure your theme is selected and open the functions.php file. Paste the following code at the bottom of the functions.php file and click the update file button. 

// Hide WordPress Version
function complete_version_removal() {
return '';
}
add_filter('the_generator', 'complete_version_removal');

Change Excerpt Length in WordPress

If you are listing your blog posts on your website on a page such as /blog/ and you want those posts to appear organized, you can limit the WordPress excerpt length. WordPress’ default excerpt length is 55 words. You can design a special structure and want to decrease or increase the number of words in the excerpt length. You can also use a plugin to change the excerpt length, but you know that every plugin you install on your website will cause your website to load slower. That’s why you should add simple functions to your website without installing plugins.

After logging into the WordPress Admin Panel, go to Appearance > Theme Editor. Make sure your theme is selected and open the functions.php file. Paste the following code at the bottom of the functions.php file and click the update file button. 

//custom excerpt length
function custom_excerpt_length( $length ) {
    return 60;
}
add_filter( 'excerpt_length', 'custom_excerpt_length', 999 );

Set the first post image as featured image on WordPress

When posting on WordPress, we can add featured images for each post. On the post edit page, there is a Featured Image meta box, usually in the right column and at the bottom. You can upload the image you want. However, if you don’t want to upload the Featured Image every time and waste time, you can automatically set the first image in the post as the featured image with a function.

After logging into the WordPress Admin Panel, go to Appearance > Theme Editor. Make sure your theme is selected and open the functions.php file. Paste the following code at the bottom of the functions.php file and click the update file button. 

// Set the first post image as featured image on WordPress
function auto_featured_image() {
    global $post;
    if (!has_post_thumbnail($post->ID)) {
        $attached_image = get_children( "post_parent=$post->ID&post_type=attachment&post_mime_type=image&numberposts=1" );
        
      if ($attached_image) {
              foreach ($attached_image as $attachment_id => $attachment) {
                   set_post_thumbnail($post->ID, $attachment_id);
              }
         }
    }
}
// Use it temporary to generate all featured images
add_action('the_post', 'auto_featured_image');
// Used for new posts
add_action('save_post', 'auto_featured_image');
add_action('draft_to_publish', 'auto_featured_image');
add_action('new_to_publish', 'auto_featured_image');
add_action('pending_to_publish', 'auto_featured_image');
add_action('future_to_publish', 'auto_featured_image');

Stay tuned to our blog posts for security, speed optimization and important tips.


We're part of the Asquared WordPress Agency. All rights reserved.