10 Bespoke WordPress Code Snippets for WP Developers
The convenience attached with customizing the look and feel of a WordPress powered website has made this CMS a leading choice among website developers across the globe. Combating the hassles of installing plugins, WordPress allows you to use simple code snippets for modifying your website in your own unique way. Today, in this post, I’ve listed out ten WordPress code snippets which are custom-made to meet the requirements of every WordPress web developer. Let’s hop onto these WordPress code snippets!
- Hide the working version for your WordPress website
Keeping an outdated WordPress version installed in your site can expose your portal to a wide array of security infringements, making it easy for the hackers to gain access to your website. Therefore, it is better to remove any such outdated WordPress version from your website. Use the following lines of code for hiding the current WordPress version:
<?php // This will remove the WP version for extra WordPress Security
function remove_wp_version(){
return ”;
}add_filter(‘the_generator’, ‘remove_wp_version’);
?>
- Hide WordPress Core Update Messages
If you are a WordPress developer working for a client, then you probably wouldn’t want the client to update anything for the website outside your knowledge. The below mentioned code snippet would simply hide the yellow bar that appears at the top of the admin dashboard each time a new version is being released:
<?php // Hide WordPress Update Message
function wp_hide_update() {
remove_action(‘admin_notices’, ‘update_nag’, 4);
}add_action(‘admin_menu’,’wp_hide_update’);
?>
- Shift WordPress Admin Bar towards the bottom
By default, the WordPress admin bar is displayed in the header once you’re logged in to your admin dashboard. You can choose to move this admin bar towards the bottom for all admin pages by adding the below code snippet to your theme’s functions.php file:
function fb_move_admin_bar() {
echo ‘
<style type=”text/css”>
body {
margin-top: -18px;
padding-bottom: 15px;
}
body.admin-bar #wphead {
padding-top: 0;
}
body.admin-bar #footer {
padding-bottom: 15px;
}
#wpadminbar {
top: auto !important;
bottom: 0;
}
#wpadminbar .quicklinks .menupop ul {
bottom: 15px;
}
</style>’;
}
// on backend area
add_action( ‘admin_head’, ‘fb_move_admin_bar’ );
// on frontend area
add_action( ‘wp_head’, ‘fb_move_admin_bar’ );
- Sharpen the resized uploaded images in WordPresss(applicable only for images with format .jpg)
Contributed by Unsal Korkmaz, displayed below is a code snippet which will allow you to sharpen resized images which have been uploaded to the WordPress website:
function sharp_resized_image( $resized_file ) {
$image = wp_load_image( $resized_file );
if ( !is_resource( $image ) )
return new WP_Error( ‘error_loading_image’, $image, $file );
$size = @getimagesize( $resized_file );
if ( !$size )
return new WP_Error(‘invalid_image’, __(‘Could not read image size’), $file);
list($orig_w, $orig_h, $orig_type) = $size;
switch ( $orig_type ) {
case IMAGETYPE_JPEG:
$matrix = array(
array(-1, -1, -1),
array(-1, 16, -1),
array(-1, -1, -1),
);
$divisor = array_sum(array_map(‘array_sum’, $matrix));
$offset = 0;
imageconvolution($image, $matrix, $divisor, $offset);
imagejpeg($image, $resized_file,apply_filters( ‘jpeg_quality’, 90, ‘edit_image’ ));
break;
case IMAGETYPE_PNG:
return $resized_file;
case IMAGETYPE_GIF:
return $resized_file;
}
return $resized_file;
}add_filter(‘image_make_intermediate_size’, ‘sharp_resized_image’,900);
- Changing the Image Path Only
If you’re one of those WordPress users who have recently hosted their images on an external CDN(Content Delivery Network) the it is important for you to change the image location for all the previous uploaded images. For this, all you need to do is simply perform the below SQL queries on your database:
UPDATE wp_posts SET post_content = REPLACE (post_content, ‘src=”//www.oldsiteurl.com’, ‘src=”//yourcdn.newsiteurl.com’);
And:
UPDATE wp_posts SET guid = REPLACE (guid, ‘http://www.oldsiteurl.com’, ‘http://yourcdn.newsiteurl.com’) WHERE post_type = ‘attachment’;
- List all the available WordPress User Roles
If you want to show a list of all available user roles, all you need to do is simply use the below code snippet:
$roles_obj = new WP_Roles();
$roles_names_array = $roles_obj->get_names();
echo ‘<select name=”role”>’;
foreach ($roles_names_array as $role_name) {
echo ‘<option>’.$role_name.'</option>’;
}
echo ‘</select>’;
- Taking a snapshot of any website
If you are keen on taking the snapshot of a website, then you can use the below function which uses a shortcode for outputting a real-time thumbnail of the website:
<?php // Output a snapshot of any website!
function wp_snap($atts, $content = NULL) {
extract(shortcode_atts(array(
“snap” => ‘http://www.testweb.com’,
“url” => ‘http://mywebsite.com/’,
“alt” => ‘Snaps’,
“w” => ‘550’, // width
“h” => ‘250’ // height
), $atts));
$img = ‘<img alt=”’ . $alt . ‘” src=”’ . $snap . ” . urlencode($url) . ‘?w=’ . $w . ‘&h=’ . $h . ‘” />’;
return $img;
}add_shortcode(“snap”, “wp_snap”);
// Use [snap url=”http://mywebsite.com/” alt=”Snaptaken” w=”550″ h=”250″]
?>
Now, to use the above shortcode, just add the following code snippet to your post:
[code][snap url=”http://torquemag.io/” alt=”WPDaily Website” w=”300″ h=”200″][/code]
- Remove image sizes from media upload gallery
Use the below code snippet for removing image sizes from media upload gallery:
function image_size_removal($sizes) {
unset( $sizes[‘small’] );
unset( $sizes[‘medium’] );
unset( $sizes[‘large’] );
return $sizes;
}
add_filter(‘image_size_names_choose’, ‘image_size_removal’);
- Style the first paragraph of your posts and pages
Contributed by Jake Rocheleau, below is the code snippet(to be added to the functions.php file) that allows you to style the first paragraph of your posts and pages; making them look different from rest of the content:
function first_paragraph($content){
global $post;
if ($post->post_type == “post”){
return preg_replace(‘/<p([^>]+)?>/’, ‘<p$1 class=”first”>’, $content, 1); }
else {
return $content;
}
}add_filter(‘the_content’, ‘first_paragraph’);
- Assign all articles written by Author A to Author B
If you want to transfer all posts belonging to one author say A to a different author, say B, then all you need to do is simply use the below SQL query:
UPDATE wp_posts SET post_author = ‘new-author-id’ WHERE post_author = ‘old-author-id’;
That’s it!
Summing Up
The above mentioned code snippets can aid you in performing the required tweaks for your WordPress site in the most flexible manner. Don’t wait any further in using them for your WP portal.
Author Bio:
As a certified top-notch developer at OSSMedia Ltd, Edward Jones has an impressive expertise in WordPress & he provide concrete information on WordPress related tips & trends. If you need to hire WordPress developer then simply get in touch with him via his Twitter and Google+ handle. Having gathered a total of 5 years of experience in WordPress Development, Edward has delivered numerous projects within the allotted time-frame.
Please let us know your comments and suggestions: