Sélectionner une page

Eight Useful Code Snippets for WordPress

Eight Useful Code Snippets for WordPress


There are few WordPress websites online that have not been modified in some way or another. The most common way of extending or modifying the functionality of a website is to install a WordPress plugin, however functions can also be added to theme files. In fact, the majority of WordPress themes contain functions that modify WordPress in some way.

Code snippets are little pieces of code that can be inserted directly into your theme files. Sometimes they contain full functions, other times they simply modify an existing function.

In this article, I would like to show you eight useful code snippets that will enhance WordPress. I have tested that all snippets are working using the current default theme Twenty Fourteen, however some functions may not work correctly if your theme has been modified a lot (particularly if it is a framework).

1. Empty Your Trash

As a failsafe, WordPress will keep a copy of all posts, pages and comments you delete; unless you specifically go into your trash folder and delete items permanently. The trash works in the same way as the Recycle Bin on the Windows operating system.

WordPress will automatically clear out your trash every thirty days, however this can be reduced by adding the following line of code to your wp-config.php file (this file is located in the root of your WordPress installation):

define ('EMPTY_TRASH_DAYS', 7);

If you want to optimize your database further so that no unnecessary items are stored in your database, you can disable the trash system altogether by adding this line of code to your wp-config.php file:

define ('EMPTY_TRASH_DAYS', 0);

Source

2. Reduce Post Revisions

The WordPress revision system saves a draft of your posts and pages each time you save an article. This feature is important to bloggers as it allows them to refer to earlier drafts and stops any work being lost in the event of a lost connection.

Unfortunately, these drafts take up a lot of room in your database as the default version of WordPress defines no limit on the number of drafts which are saved. This means that a large post that has been saved one hundred times would take up one hundred rows in your database table.

To address this issue, you can reduce the number of post revisions to a more sensible number by adding the following code to your wp-config.php file:

define( 'WP_POST_REVISIONS', 3 );

If you would prefer to disable the post revision system altogether, simply add this code to your wp-config.php file:

define( 'WP_POST_REVISIONS', false );

Source

WordPress also autosaves your posts and pages every sixty seconds. The interval in which posts are saved can be modified by adding the following code to your wp-config.php file:

define( 'AUTOSAVE_INTERVAL', 160 ); // Seconds

3. Move Your WP-Content Folders

The wp-content folder contains your themes, plugins and uploads. Certain plugins, such as caching plugins, also use the wp-content folder to store data.

Due to this, the wp-content folder is frequently a target for hackers, particularly those that insert malware into your theme files. You can make it difficult for people to find your wp-content directory by moving it to another area of your website.

If you want to simply move the wp-content folder to another location, you can add this code to your wp-config.php file:

* Notice the wp-content folder does not have a trailing slash

define( 'WP_CONTENT_DIR', dirname(__FILE__) . '/newlocation/wp-content' );

If you prefer, you can define the new location using the URL:

define( 'WP_CONTENT_URL', 'http://www.yourwebsite.com/newlocation/wp-content' );

WordPress also allows you to rename your wp-content folder using:

define ('WP_CONTENT_FOLDERNAME', 'newfoldername'); 

Renakming your wp-content folder can make WordPress website even safer, however it is unfortunately not always practical to do so because many WordPress plugin developers continue to hard code “wp-content” into their plugin code. It may still be worth doing if security is a top priority, though be aware that it may require you to manually update the code of many plugins you use (and these would have to be manually updated every time you updated the plugin).

Source

4. Redirect Author Archive Link to Your About Page

The author archive link that is listed in the meta information area of a blog post links to a page that displays all previous posts by the author. Sometimes a bio is displayed at the top this page too.

If you run a single author blog, there is no need for you to link to author archives as the same posts are linked in your category and monthly archives. A better solution is to link your author archive link directly to your about page.

You can do this by adding the following code to your theme’s functions.php file:

add_filter( 'author_link', 'my_author_link' );
 
function my_author_link() {
	return home_url( 'about' );
}

Source

5. Redirect to Post If Search Results Return One Post

Whenever a search is performed, WordPress displays a list of all posts and pages that are related to the particular keyword or key phrase. The user can then click on the article they want to read.

If there is only one result, the search results page is not necessary. It makes more sense to simply redirect the visitor directly to the article in question.

You can do this by adding the following function to your theme’s functions.php file:

add_action('template_redirect', 'redirect_single_post');
function redirect_single_post() {
    if (is_search()) {
        global $wp_query;
        if ($wp_query->post_count == 1 && $wp_query->max_num_pages == 1) {
            wp_redirect( get_permalink( $wp_query->posts['0']->ID ) );
            exit;
        }
    }
}

Source

6. Exclude Pages from WordPress Search Results

The typical blog has hundreds or thousands of posts but just a few pages. These pages tend to be important areas of your website such as About and Contact; and are always clearly displayed in the header or sidebar. There is therefore less of a priority to include pages in search results as it is unlikely that visitors will be looking for them.

Removing pages from search results is also a quick way of hiding private pages from visitors. For example, your newsletter subscription page or your eBook download page.

Adding the code below to your themes’s functions.php will ensure that only posts are shown in search results:

function filter_search($query) {
	if ($query->is_search) {
		$query->set('post_type', 'post');
	}
	return $query;
}
add_filter('pre_get_posts', 'filter_search');

7. Remove the URL Field From Your Comment Form

I recently spoke about how you can reduce WordPress comment spam. Here is another trick you can use to reduce manual comment spam: Remove the URL field. Doing this will greatly reduce the number of poor quality comments your blog receives.

To remove the URL field, all you have to do is add the following function to your theme’s functions.php file:

function remove_comment_fields($fields) {
    unset($fields['url']);
    return $fields;
}
add_filter('comment_form_default_fields','remove_comment_fields');

Source

8. Enforce a Minimum Content Length

Another annoying thing that some commenters do is leave short, pointless replies. Comments such as “Thanks”, “Great post” and “Cool”, add nothing to the discussion. If you do not want to remove the URL field from your comment form, you may want to enforce a minimum length for your comments so that short two word replies are not accepted.

You can do this by adding the following code to your theme’s functions.php file. Be sure to change the $minimalCommentLength string to the minimum number of character you desire.

add_filter( 'preprocess_comment', 'minimal_comment_length' );
function minimal_comment_length( $commentdata ) {
    $minimalCommentLength = 20;
    if ( strlen( trim( $commentdata['comment_content'] ) ) < $minimalCommentLength ){
    wp_die( 'All comments must be at least ' . $minimalCommentLength . ' characters long.' );
    }
    return $commentdata;
}

Source

I hope you have enjoyed this short list of useful code snippets for WordPress. If so, I encourage you to subscribe to Elegant Themes for updates of our latest blog posts.



Source link

Poster le commentaire

Votre adresse e-mail ne sera pas publiée.