10wpsnip

10 WordPress Snippets to improve your blog Functionality, Flexibility and Performance

Posted in Web Design/Development, Wordpress | 4 Comments

WordPress snippets have proved to give quick solutions to improving your blog functionality, flexibility and boosting its performance.  The snippets below are some of the common ones you will come across, so make it useful in your projects cos’ if you don’t, I will :-P .

How to Remove Menus in WordPress Dashboard

I wanted to build a static site for a client recently and was worried about certain menus in the admin panel distracting the client as they will not be using them on their static website.  Thankfully I found this snippet that allows you to remove these menus from the WordPress admin, in short, solved my issue:

function remove_menus() {
global $menu;
	$restricted = array(__('Dashboard'), __('Posts'), __('Media'), __('Links'), __('Pages'), __('Appearance'), __('Tools'), __('Users'), __('Settings'), __('Comments'), __('Plugins'));
	end ($menu);
	while (prev($menu)){
		$value = explode(' ',$menu[key($menu)][0]);
		if(in_array($value[0] != NULL?$value[0]:"" , $restricted)){unset($menu[key($menu)]);}
	}
}
add_action('admin_menu', 'remove_menus');

Source: Hungred, WP Recipes

How To Disable Image Caption in WordPress Post Editor

It has always been annoying to see poups when you hover over images in your WordPress blog. I wish they would just have an option in the Settings screen to disable them nasty popups that reveal the image filename. Anyway thanks to this snippet, we can still remove this totally:

add_filter('disable_captions', create_function('$a','return true;'));

Source: SEOdenver

Detect User From Google

Have you ever wondered how some websites know straight away that you found them through Google? Paste this snippet into your template files, coupled with some CSS and you could get something like this:

detect-user-google

if (strpos($_SERVER[HTTP_REFERER], "google") == true) {
echo "Hello Google User!";
}

Source: Filip Stefansson | WPSnippets

Detect Browsers

WordPress has also made it simple for us to detect which browser people access your website on.  This snippet will add the relevant browser name as a class to your <body> tag for relevant styling:

add_filter('body_class','browser_body_class');
function browser_body_class($classes) {
	global $is_lynx, $is_gecko, $is_IE, $is_opera, $is_NS4, $is_safari, $is_chrome, $is_iphone;

	if($is_lynx) $classes[] = 'lynx';
	elseif($is_gecko) $classes[] = 'gecko';
	elseif($is_opera) $classes[] = 'opera';
	elseif($is_NS4) $classes[] = 'ns4';
	elseif($is_safari) $classes[] = 'safari';
	elseif($is_chrome) $classes[] = 'chrome';
	elseif($is_IE) $classes[] = 'ie';
	else $classes[] = 'unknown';

	if($is_iphone) $classes[] = 'iphone';
	return $classes;
}

Result:

<body class="home blog logged-in safari">

Source: Nathan Rice

How To Disable, Limit and Delete Remaining Post Revisions in WordPress and Reduce Database Size

Disabling WordPress’ Post Revision is as simple as pasting the below snippet into your wp-config.php file at the root of your WordPress Install. To limit the amount of revisions is as simple as replacing ‘false’ with a number of how many revisions you want created:

define('WP_POST_REVISIONS', false);

To delete remaining revisions in your database, simple run this command at the SQL tab of your phpMyAdmin page:

DELETE a,b,c
FROM wp_posts a
LEFT JOIN wp_term_relationships b ON (a.ID = b.object_id)
LEFT JOIN wp_postmeta c ON (a.ID = c.post_id)
WHERE a.post_type = 'revision';

Source: WPBeginner, Ashfame

Edit WordPress’ ‘Help’ Dropdown Section

At the top right of your WordPress admin screen is a hanging tab with the option dropdown ‘Screen Options’ or ‘Help’ or both.  You can use this to insert custom information, where its for your client or donations or whoever you feel you wanna shout out to.

add_action('load-page-new.php','add_custom_help_page');
add_action('load-page.php','add_custom_help_page');

function add_custom_help_page() {
   //the contextual help filter
   add_filter('contextual_help','custom_page_help');
}

function custom_page_help($help) {
   //keep the existing help copy
   echo $help;
   //add some new copy
   echo "<h5>Custom Features</h5>";
   echo "<p>Content placed above the more divider will appear in column 1. Content placed below the divider will appear in column 2.</p>";
}

Source: Smashing Magazine

Add A Dashboard Widget

Display some information to whoever logs into the main screen.

<?php
function your_dashboard_widget() {
?>
<h3>Hello WordPress user!</h3>
<p>Fill this with HTML or PHP.</p>
<?php
};
function add_your_dashboard_widget() {
 wp_add_dashboard_widget( 'your_dashboard_widget', __( 'Widget Title!' ), 'your_dashboard_widget' );
}
add_action('wp_dashboard_setup', 'add_your_dashboard_widget' );
?>

Source: Filip Stefansson | WPSnippets

Disable Dashboard Widgets

Do the opposite of the above:


function remove_dashboard_widgets() {
 global $wp_meta_boxes;
 unset($wp_meta_boxes['dashboard']['normal']['core']['dashboard_plugins']);
 unset($wp_meta_boxes['dashboard']['normal']['core']['dashboard_recent_comments']);
 unset($wp_meta_boxes['dashboard']['side']['core']['dashboard_primary']);
 unset($wp_meta_boxes['dashboard']['normal']['core']['dashboard_incoming_links']);
 unset($wp_meta_boxes['dashboard']['normal']['core']['dashboard_right_now']);
 unset($wp_meta_boxes['dashboard']['side']['core']['dashboard_secondary']);
}
add_action('wp_dashboard_setup', 'remove_dashboard_widgets' );

Source: Filip Stefansson | WPSnippets

How to Add Custom Post Types to your Main WordPress RSS Feed

By default the WordPress RSS Feed works only for the ‘post‘ post type. Using this, you can add more:


function myfeed_request($qv) {
 if (isset($qv['feed']))
 $qv['post_type'] = get_post_types();
 return $qv;
}
add_filter('request', 'myfeed_request');

By default, this selects all the post types you have on your website. To be selective with this, use the code below and edit the array:

function myfeed_request($qv) {
 if (isset($qv['feed']) && !isset($qv['post_type']))
 $qv['post_type'] = array('post', 'story', 'books', 'movies');
 return $qv;
}
add_filter('request', 'myfeed_request');

Source: WPBeginner

Add a jQuery prettyPhoto functionality to WordPress’  gallery shortcode

The WordPress gallery shortcode does a good job in its ouput, except it does not allow you to specify any lighbox functionality except you have a plugin.  This simple technique by Utkarsh Kukreti adds a ‘rel’ attribute to the image link, allowing you to specify the lightbox effect you want, in this example the prettyPhoto jQuery lightbox clone is used:


add_filter( 'wp_get_attachment_link', 'gallery_prettyPhoto');

function gallery_prettyPhoto ($content) {

	// add checks if you want to add prettyPhoto on certain places (archives etc).

	return str_replace("<a", "<a rel='prettyPhoto'", $content);

}

In effect, a HTML attribute rel=”prettyPhoto” has been added here to activate the prettyPhoto jQuery lightbox clone.

Source: WPQuestions



4 Comments to “10 WordPress Snippets to improve your blog Functionality, Flexibility and Performance”





Recommended

Expert Help with WordPress
Learn how you can market your business on Twitter
Get the Book now! Use 25% discount code 'november25'
Hostgator - Cheap and Reliable Web Hosting - WordPress friendly

Incoming Tweets