WordPress Tips, Tricks, and Hacks in 2021

Even if plugins are sometimes needed and help us to customize our WordPress site, from my experience, it is advisable to have a few modules installed as possible. A minimum number of modules will keep your site clean, safe and guarantee a good browsing speed.

In this article, I will present useful things to customize your WordPress site without plugins.

If you want to see my list of favorite plugins I use I invite you to read about Best WordPress plugins that I use in my sites.

How to switch back to the old classic editor on WordPress

The latest version of WordPress 5.0, have Gutenberg enabled. Personally, I did not use the new Gutenberg editor, so I don’t have much to say about it. Because I got used to the old editor, the classic one, in this post I will show solutions to revert back to the previous WordPress editor.
Support for the Classic Editor plugin will remain in WordPress through 2021.

There are 2 simple ways to do this: with plugins or without plugins

Use Classic Editor plugin

The Classic Editor plugin restores the previous WordPress editor and the Edit Post screen. It lets you keep using plugins that extend it, add old-style meta boxes. To install, visit plugins page and click the “Install Now” button next to “Classic Editor”. After the plugin finishes installing, click “Activate”.

Without plugins

Because I want to have fewer plugins installed, I prefer to disable the new editor, editing functions.php file.

  1. Browse your theme and locate functions.php
  2. For wordpress versions > 5.0 add this line of code
    add_filter('use_block_editor_for_post_type', '__return_false', 10);
    

Enable Debugging in WordPress

Before we start it is indicated to have the debug enabled. To be sure that debug mode is enabled WordPress, go to the wp-config file and add next lines

//enable-debug
define('WP_DEBUG', true);

//log debug info
//log file will be located in the wp-content directory.
//Make sure that the debug file have to write permissions on the server
define('WP_DEBUG_LOG', true);

//do not display debug info on page
define('WP_DEBUG_DISPLAY', false);

For more information about enabling debug on wordpress, check out the developer docs at WordPress

Random post order with correct pagination

You can find many resources on the internet to randomize the wordpress posts, simply add ‘orderby’ => ‘rand’ in wp query :

query_posts("orderby=rand");

The issue with this method is that it breaks WordPress pagination. If you navigate to the second page of the results, the random posts which are returned may contain posts that you have already viewed.

Here is an easy way to randomize the posts order with correct pagination

Simply add the next piece of code to Your wordpress theme, at the beginning of functions.php file:

<?php
session_start();
add_filter( 'posts_orderby', 'randomise_with_pagination' );
function randomise_with_pagination( $orderby ) {
	if( is_front_page() ) {
	  	// Reset seed on load of initial archive page
		if( ! get_query_var( 'paged' ) || get_query_var( 'paged' ) == 0 || get_query_var( 'paged' ) == 1 ) {
			if( isset( $_SESSION['seed'] ) ) {
				unset( $_SESSION['seed'] );
			}
		}
	
		// Get seed from session variable if it exists
		$seed = false;
		if( isset( $_SESSION['seed'] ) ) {
			$seed = $_SESSION['seed'];
		}
	
	    	// Set new seed if none exists
	    	if ( ! $seed ) {
	      		$seed = rand();
	      		$_SESSION['seed'] = $seed;
	    	}
	
	    	// Update ORDER BY clause to use seed
	    	$orderby = 'RAND(' . $seed . ')';
	}
	return $orderby;
}
?>

Note

This code randomize the order to the front page only (because it has is_front_page() condition), but you can change the conditional to target whatever page or post type archive that you wish.

How to nofollow all external links in WordPress

By default, WordPress does not let you to automatically add nofollow to external links. You will have to manually add it or use a plugin to do this in an easier way.

Tested plugin

Nofollow for external link

Use this plugin https://wordpress.org/plugins/nofollow-for-external-link/, and rel=nofollow and target=_blank will be automatically inserted, for all external links of the site posts or page.

You can also exclude domains by not to adding rel=nofollow for the selected external links.

  • The plugin will not add rel=nofollow or target=_blank to any internal link on your website posts/pages.
  • If you previously added manually, rel=dofollow or rel=nofollow to any post , the plugin will not add rel=nofollow for that post.
  • If you previously added manually, target=_blank to any post, the plugin will not add target=_blank for that post.
Pro

– very easy to setup

Posting source code on your posts

WordPress does not allow you to use potentially dangerous code on your blog, there is a way to post source code for viewing.
You can easily post syntax-highlighted code on your blog.

1. Install https://wordpress.org/plugins/syntaxhighlighter/ plugin

2. In your WordPress text editor just wrap your code in these tags:

[  code lang=”js”]
your code here
[/code]

Code in between the source code tags will automatically be encoded for display, you don’t need to worry about HTML entities or anything.

Setting parameters

lang – The language (or lang) parameter controls how the code is syntax highlighted. The following languages are supported: css, html, java, javascript, perl, php

If the language parameter is not set, it will default to “text” (no syntax highlighting).

autolinks (true/false) — Makes all URLs in your posted code clickable. Defaults to true.

collapse (true/false) — If true, the code box will be collapsed when the page loads, requiring the visitor to click to expand it. Defaults to false.

gutter (true/false) — If false, the line numbering on the left side will be hidden. Defaults to true.

highlight (comma-separated list of numbers) — You can list the line numbers you want to be highlighted. For example “1,5”.

title (string) — Set a label for your code block. Can be useful when combined with the collapse parameter.

More settings here


Leave a Comment

WebPedia.net