Top Ad unit 728 × 90

Useful Code Snippets for WordPress


WordPress plugins are the most popular way of extending the functionality of WordPress. They allow you to change the way that WordPress works.
Plugins are simply functions written in HTML and PHP that are plugged into your website. Another way to change the functionality of WordPress is to use a code snippet.
Code snippets are small pieces of code that you add to your website directly.
Be aware that any additional code you add to theme files will be overwritten if you upgrade your WordPress theme. It is therefore prudent to take a note of any snippets you add to your website in a separate location. You should also check out the free WordPress plugin Code Snippets. It can be used to insert code snippets directly into your theme functions.php file.
In this article, I would like to share with you a collection of useful code snippets for WordPress. I hope you enjoy the list 
Spammers frequently link to web pages with very long URLs. Therefore, if someone publishes a comment and enters a long URL in the website field, there is a high chance that the comment was published by a spammer.
The snippet below allows you to mark a comment as spam that has a website URL over 50 characters. You can increase or decrease this limit to suit your own needs.

Marking comments that have very long URLs in the website field will help you tackle spammers. However, if you find that spam is getting out of control, you may want to consider removing the website URL field altogether.

If you implement the snippet below and configure your WordPress discussion settings so that any comment with a link is held for moderation; you can effectively stop all spam entirely.
File to Edit: Functions.php
A good way to tackle spam and encourage better comments is to apply a minimum length for comments. This helps stop small irrelevant comments such as “Great post” and “Thanks!”.
WordPress automatically turns URLs posted in comments into links. The downside to this feature is that it helps spammers publish links in their comments. You can remove this functionality with the following code snippet.
This useful function will automatically link Twitter usernames to the the person’s Twitter profile. Worth using if you frequently refer to Twitter users in your articles.
By default, WordPress includes pages in search results. This means that pages such as about, contact, privacy, etc, will be included in search results. These types of pages are not necessary if they are linked in your main navigation menu. WordPress will also include private pages in search results too. For example, a newsletter confirmation page or an eBook download page.
To remove all pages from WordPress search, simple add the snippet below to your theme functions.php file.
When you search for something on a WordPress website, a search results page is displayed with all relevant posts, posts, and other post types. The problem with this setup is that WordPress will still display a results page if there is only one blog post result.
This code snippet addresses this issue. If only one blog post is found for a search, the visitor will be taken directly to the blog post in question.
You will find this snippet useful if you allow people to register on your website. It lets you redirect new users to a specific page after they have completed their registration.
This is useful if you permit visitors to sign up for an account to publish comments or join your discussion forum. For example, you can redirect new members to a thank you page that gives details of their membership.
CSS Tricks published some useful snippets that can be used to customize your WordPress login page. The first snippet changes the logo that is displayed at www.yourwebsite.com/wp-login.php and www.yourwebsite.com/wp-admin/.
The second snippet allows you to customize the URL that is attached to your login logo. Rather than link to WordPress.org, the logo will now link to your website home page.
The third snippet can be used to change the title attribute of your login logo to the website title defined in your settings area.
All of the above functions should be added to your theme functions.php file.
The default URL slug for authors is /author/name. For example, on WPThemeDetector my author URL is www.wpthemedetector.com/author/kevin-muldoon/.
You can change the author slug using the code snippet below. Simple change the author slug to your desired name. For example, instead of the word author, you could use profile, member, or account.
The default text widget in WordPress only supports HTML. If you insert the following snippet into your theme functions.php file, you can also use PHP code.
This greatly extends the functionality of the text widget. For example, you could use the text widget to display custom functions, PHP queries, calls to external files, and more.
The WordPress revision system saves a copy of your posts and pages every time you update them. This is handy as it allows you to refer back to previous versions of your article, compare drafts, and correct mistakes.
The problem is that WordPress does not limit how many revisions it saves. Therefore, if you have saved an article 100 times, your website will store 100 revisions of the article. This can greatly increase the size of your database as one article with 100 revisions will take up just as much room as 100 articles with no revisions.
It is therefore practical to limit the number of revisions that are stored. On my own websites, I limit revisions to two. This allows me to refer back to the last few copies of an article, without taking up too much room in the database.
You can limit revisions by adding the code snippet below to wp-config.php.
Revisions can be disabled altogether by using the following code.
Be careful about disabling post revisions. If you lose your internet connection or accidentally close your browser tab, you could lose everything you had written since you last saved your article.
When you delete something in WordPress, such as a comment, post, image, or link; it is not permanently deleted. Instead, it is sent to the trash folder. This fail safe system is used to allow you to retrieve items that were deleted in error. The downside to this is that deleted items will remain in your database until trash is emptied.
Items that are sent to the trash folder are automatically deleted after 30 days. You can reduce the number of days before trash items are deleted by adding the following code to wp-config.php.
The trash system can be completely disabled if you wish. You can do this by using this code.
Be careful of disabling the trash system completely as it means that there is no way of recovering an item once you have deleted it. At the very least, I recommend setting trash to be emptied after one day. This will let you recover any items you deleted by accident.
WordPress introduced automatic background updates in WordPress 3.7. All automatic updates can be completely disabled by adding this code to your wp-config.php.
When it comes to updating WordPress, there are two types of updates: minor and major. Major updates add additional functionality to the WordPress platform. They can be identified quickly due to their WordPress version number e.g. WordPress 3.7, 3.8, 3.9, 4.0 etc.
Minor updates are updates of major versions of WordPress that have been released to address security holes and correct bugs. Their version number increases by 0.01 e.g. 4.01, 4.02, 4.03 etc.
By default, WordPress is configured to apply all minor updates automatically when they are released.
If you want to disable core updates, you can do so using the following code.
Minor and major updates of WordPress can be installed automatically by adding the following code to wp-config.php.
Once the above code has been added to wp-config.php, you can repair your database by going to www.yourwebsite.com/wp-admin/maint/repair.php.
In time, all WordPress websites accumulate overhead due to queries being processed. A little overhead in a database is normal, however if there is too much overhead, the performance of your website may be affected.
To remove overhead from a MySQL database, you need to repair it. WordPress allows you to repair your database automatically. All you need to do is add the following code to your wp-config.php file.
What is your favorite WordPress code snippet? Please share it with other WPThemeDetector readers in the comment area below ðŸ™‚
Useful Code Snippets for WordPress Reviewed by Unknown on 10:08 PM Rating: 5

No comments:

All Rights Reserved by Boca Raton Real Estate © 2014 - 2015
Powered By Blogger, Designed by Sweetheme

Contact Form

Name

Email *

Message *

Powered by Blogger.