Useful code snippets
Below is a list of few useful code snippets which can be used in WordPress and come quite handy sometimes. For most of them there are different plugins.
Table of Contents
But as they say lot of plugins do more harm than good.
So better to use these useful code snippets whenever and wherever necessary.
Here you go:
1. Temporary Maintenance
The following code needs to be added to your theme’s functions.php to temporarily close access to your site by visitors. Only admin who can log in and then check the website but not others.
This way you can work on the site and do changes without your site being accessed or without worrying about people seeing a messed up website:
// Temp Maintenance - with http response 503 (Service Temporarily Unavailable)
// This will only block users who are NOT an administrator from viewing the website.
function wp_maintenance_mode(){
if(!current_user_can('edit_themes') || !is_user_logged_in()){
wp_die('Maintenance, please come back soon.', 'Maintenance - please come back soon.', array('response' => '503'));
}
}
add_action('get_header', 'wp_maintenance_mode');
2. Add featured images to WordPress feeds
Another code snippets for WordPress
Featured images aren’t by default added to a WordPress rss feed and hence this code snippet which can be pasted in functions.php comes handy:
//Function to add featured image in RSS feeds function featured_image_in_rss($content) { // Global $post variable global $post; // Check if the post has a featured image if (has_post_thumbnail($post->ID)) { $content = get_the_post_thumbnail($post->ID, 'full', array('style' => 'margin-bottom:10px;')) . $content; } return $content; } //Add the filter for RSS feeds Excerpt add_filter('the_excerpt_rss', 'featured_image_in_rss'); //Add the filter for RSS feed content add_filter('the_content_feed', 'featured_image_in_rss');
3. Minimal Comment Limit in WordPress
Most people have comments in their blogs to increase interactivity and healthy discussion on the site. But there are some scammers who want to just get a link out of the site and hence they will reply with minimal words like “good read”, “nice post”, “thanks” etc.
Hence to stop these users from posting such less content which makes no sense here is a code which gives control to admin to limit comment limit:
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;
}
4. Increase WordPress memory limit
Some hosts by default limit the memory limit to just 2mb and hence it becomes difficult to upload large media files in media section of WordPress.
For this we need to add the following code in wp-config.php
define('WP_MEMORY_LIMIT', '96M');
5. Allow PHP in WordPress text widgets
Sometimes it is difficult to just add another template in WordPress and hence a widget can come handy. But wait text widgets as normal don’t allow for php code. Hence the following code snippets for WordPress can be used for this purpose:
add_filter('widget_text', 'enable_php_code', 99);
function enable_php_code ($text) {
if (strpos($text, '' . $text);
$text = ob_get_contents();
ob_end_clean();
}
return $text;
}
6. Remove URL field from Comment
There are 3 ways in which this can be done.
One is if you want to completely remove website field from WordPress comments on your theme paste the following code into your theme’s or child theme’s function.php
function ra_remove_comment_url_field( $field ) {
return '';
}
add_filter( 'comment_form_field_url', 'ra_remove_comment_url_field' );
If you want users to add url but not display them use the following code instead:
function ra_hide_comment_url( $url ) {
if ( !is_admin() )
return '';
else
return $url;
}
add_filter( 'get_comment_author_url', 'ra_hide_comment_url' );
And if you want to remove website links on comments but not pingbacks and trackbacks then paste following code in function.php
function ra_hide_comment_url( $url ) {
if ( !is_admin() && get_comment_type() == 'comment' )
return '';
else
return $url;
}
add_filter( 'get_comment_author_url', 'ra_hide_comment_url' );
7. Empty trash of WordPress
Its real simple just paste the following code into wp-config and it works like a charm:
define('EMPTY_TRASH_DAYS', 1 ); //Integer is the amount of days
8. Reduce post revisions or disable post revisions
This is a great way to reduce the size of MYSQL specially if you have a big site or have the tendency to revise a post many times before finally publishing it. Add the following code snippet in wp-config.php:
define( 'WP_POST_REVISIONS', 2 );
You can turn off the post revisions by writing false instead of 2 above. Given below:
define( 'WP_POST_REVISIONS', false );
9. Move WordPress admin bar to bottom
The following code pasted in functions.php does the work:
function fb_move_admin_bar() {
echo '
body {
margin-top: -28px;
padding-bottom: 28px;
}
body.admin-bar #wphead {
padding-top: 0;
}
body.admin-bar #footer {
padding-bottom: 28px;
}
#wpadminbar {
top: auto !important;
bottom: 0;
}
#wpadminbar .quicklinks .menupop ul {
bottom: 28px;
}
';
}
// on backend area
add_action( 'admin_head', 'fb_move_admin_bar' );
// on frontend area
add_action( 'wp_head', 'fb_move_admin_bar' );