With the way WordPress has made its place in our lives is awesome. Almost every second or third website you found on internet is a WordPress website, then whether it would be a blog, a corporate or something else. In short, WordPress speaks the language of making big your brand.
But still with all this, have you ever tried to think that all these sites have something in common – yes I know they all are WordPress websites – but what else? If you noticed correctly, all these websites from time to time require some short snippets (code) – just like your car requires fuel – which helps you in creating and customizing your website better.
The list that I am going to share through this article is a pretty comprehensive list of useful snippets for WordPress. These all snippets should work across all versions of WordPress. So, here we go – the ten very useful WordPress Snippets:
Checks If The Visitor Is From Mobile Device
The purpose of this snippet is to check whether if the user is visiting using a mobile device or not and also in return if it would be returning results in either true or false.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
<?php // Add to functions.php // version proof, checks if the visitor is from a mobile device function muneeb_wp_is_mobile() { if ( function_exists( 'wp_is_mobile' ) ) return wp_is_mobile(); //code from wp_is_mobile function, wp_is_mobile() is located in wp-includes/vars.php version 3.4 static $is_mobile; if ( isset($is_mobile) ) return $is_mobile; if ( empty($_SERVER['HTTP_USER_AGENT']) ) { $is_mobile = false; } elseif ( strpos($_SERVER['HTTP_USER_AGENT'], 'Mobile') !== false // many mobile devices (all iPhone, iPad, etc.) || strpos($_SERVER['HTTP_USER_AGENT'], 'Android') !== false || strpos($_SERVER['HTTP_USER_AGENT'], 'Silk/') !== false || strpos($_SERVER['HTTP_USER_AGENT'], 'Kindle') !== false || strpos($_SERVER['HTTP_USER_AGENT'], 'BlackBerry') !== false || strpos($_SERVER['HTTP_USER_AGENT'], 'Opera Mini') !== false ) { $is_mobile = true; } else { $is_mobile = false; } return $is_mobile; } // Use anywhere if ( muneeb_wp_is_mobile() ){ //do mobile stuff here } ?> |
Remove Login Shake
The following snippet has to be added to functions.php and it will remove the “wrong password” screen shake.
1 2 3 4 5 6 |
<?php function my_login_head() { remove_action('login_head', 'wp_shake_js', 12); } add_action('login_head', 'my_login_head'); ?> |
Only Show Admin Bar To Administrators
To hide the admin back to logged in users except admins, add this snippet in your theme’s function.php file. This snippet is basically useful for sites having public registration turned on.
1 2 3 4 5 6 |
<?php // Snippet code goes here! if (!current_user_can('administrator')) : show_admin_bar(false); endif; ?> |
QR Code With Article Link
To have the QR-code rendered for every article, just insert the following snippet inside the WordPress loop.
1 2 3 4 5 |
<img src="http://api.qrserver.com/v1/create-qr-code/?size=500x500&data=" <?php the_permalink() ?> " alt="QR Code for <?php the_title_attribute(); ?> "/> |
Disable All Comments
Although you do have an option to disable all the comments in a single go via your WordPress dashboard area but you can also do the same via using the snippet. You can do this by using this MYSQL snippet in your database.
1 2 3 4 |
// Disable all comments UPDATE wp_posts SET comment_status = 'closed', ping_status = 'closed'; // Enable all comments UPDATE wp_posts SET comment_status = 'open', ping_status = 'open'; |
Detect iPhone Users
With the way mobile has entered in our day to day lives, the use of mobile web is also on increase. It’s a good idea to customize your website for mobile browsers. The following snippet will helps you detect visitors which are using an iPhone, allowing you to create a better experience for them.
1 2 3 4 5 6 |
<?php global $is_iphone; if($is_iphone) { // User in on an iPhone. } ?> |
Check PHP Version
Since WordPress 3.2 version made its debut, PHP 5.2.4 is required for it to run. The time ever since development of plugins for this version come into existence you might require to check that whether the PHP version is up to date or not.
1 2 3 4 5 |
<?php if ( version_compare(PHP_VERSION, '5.2.4', '<') ) { wp_die('PHP 5.2.4 is required.'); }; ?> |
Custom Admin CSS
One in Five times you would be interested in changing the default style of WordPress area. In order to do this, you have to create a new css file in your theme folder and then have to include this in WP-Admin using the admin_head hook. As soon as you are done, add this to functions.php template, change paths and then add the stylesheet in the place where it is require and you are then free to add whatever styles you want to override with.
1 2 3 4 5 6 7 8 9 10 |
<?php function customAdmin() { $url = get_settings('siteurl'); $url = $url . '/wp-content/themes/my-theme/styles/wp-admin.css'; echo '<!-- custom admin css --> <link rel="stylesheet" type="text/css" href="' . $url . '" /> <!-- /end custom adming css -->'; } add_action('admin_head', 'customAdmin'); ?> |
Define Widget Area In Post Or Page
Often you feel the sense that you would be requiring more separate widget areas for your particular page as well as post you are on. At that time, instead of building a new template, it would be better to add a custom filed to each of this page or post you want to have a different sidebar on.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
<?php // Check if custom field for sidebar is set if(get_post_meta($post->ID, "sidebar", true)){ // If set, save it $sidebar = get_post_meta($post->ID, "sidebar", true); } else { // If not set, default to your standard sidebar $sidebar = 'default-sidebar'; } // Now echo your sidebar in your template using the $sidebar variable <?php dynamic_sidebar($sidebar); ?> |
Hide ‘Screen Options’ Tab
If you hated the way the Screen Options tab appearing in front of you in your admin area, then adding this snippet to your functions.php template will do the trick for you. Simply add it.
1 2 3 4 5 6 7 8 |
<?php // Hide admin 'Screen Options' tab function remove_screen_options_tab() { return false; } add_filter('screen_options_show_screen', 'remove_screen_options_tab'); ?> |