10 More Useful WordPress Code Snippets For Your Blog


WordPress is the best open source sofware which is completely free to use.The advantage of being open source for a software is that developers can see its codes an write plugins to make it more functional.And also developers add or change small piece of codes especially in functions.php file to unleash the power of your favorite blogging engine.

In today’s post we are again sharing wordpress code snippets.I think you will find a useful wordpress code snippet for your wordpress blog.

You may also take a look at our past wordpress theme collections;

htaccess Gzip Compression

Add the following code in your .htaccess file.Gzip will drastically reduce HTTP response time.

[php] # BEGIN GZIP
<ifmodule mod_deflate.c>
AddOutputFilterByType DEFLATE text/text text/html text/plain text/xml text/css application/x-javascript application/javascript
# END GZIP[/php]



How to Remove the Width and Height Attributes From WP Image Uploader

Add the following code in your functions.php file

[php]add_filter( ‘post_thumbnail_html’, ‘remove_width_attribute’, 10 );
add_filter( ‘image_send_to_editor’, ‘remove_width_attribute’, 10 );

function remove_width_attribute( $ html ) {
$ html = preg_replace( ‘/(width|height)=\"\d*\"\s/’, "", $ html );
return $ html;



How to Change the Font in HTML Editor In WordPress 3.3

Just add this code in your functions.php file

[php]add_action( ‘admin_head-post.php’, ‘wpdb_fix_html_editor_font’ );
add_action( ‘admin_head-post-new.php’, ‘wpdb_fix_html_editor_font’ );

function wpdb_fix_html_editor_font() { ?>
<style type="text/css">
#editorcontainer #content, #wp_mce_fullscreen, .wp-editor-area {
font-family: Verdana,Arial,sans-serif!important;
<?php }[/php]



“Edit This” Button on Posts and Pages

Put this code in your single.php or page.php file to make a link that the admin can be redirected to edit the post.

[php]<?php edit_post_link(__(‘Edit This’)); ?>[/php]



Add/remove Contact Info Fields

To add or remove fields from this section, just add the following to your functions.php.

function new_contactmethods( $ contactmethods ) {
$ contactmethods[‘twitter’] = ‘Twitter’; // Add Twitter
$ contactmethods[‘facebook’] = ‘Facebook’; // Add Facebook
unset($ contactmethods[‘yim’]); // Remove YIM
unset($ contactmethods[‘aim’]); // Remove AIM
unset($ contactmethods[‘jabber’]); // Remove Jabber

return $ contactmethods;



Generate QR Code for Posts and Pages

Add this small snippet to the single.php of your wordpress theme in the location you wish to display the QR. To adjust the size of the QR post / page image just change the following within the src size=100×100

[php]<img src="http://api.qrserver.com/v1/create-qr-code/?size=100×100&data=<?php the_permalink(); ?>" alt="QR: <?php the_title(); ?>"/>[/php]



Login with Username or Email Address

Adding this snippet to the functions.php of your wordpress theme will let users login using an email address or a username. The second snippet will change the text on the login page from “username” to “username / email” but feel free to change the login text to anything you would like.

[php]function login_with_email_address($ username) {
$ user = get_user_by(’email’,$ username);
if(!empty($ user->user_login))
$ username = $ user->user_login;
return $ username;
function change_username_wps_text($ text){
if(in_array($ GLOBALS[‘pagenow’], array(‘wp-login.php’))){
if ($ text == ‘Username’){$ text = ‘Username / Email’;}
return $ text;
add_filter( ‘gettext’, ‘change_username_wps_text’ );[/php]



Display Time Since Published in WordPress

Just add the below code in your functions.php file

* Display time since post was published
* @uses human_time_diff() Return time difference in easy to read format
* @uses get_the_time() Get the time the post was published
* @uses current_time() Get the current time
* @return string Timestamp since post was published
* @author c.bavota
function get_time_since_posted() {

$ time_since_posted = human_time_diff( get_the_time( ‘U’ ), current_time( ‘timestamp’ ) ) . ‘ ago’;

return $ time_since_posted;[/php]




Easily Embed and Share Github Gists on Your WordPress Blog

Paste the following code into your functions.php file. Once done, simply paste the URL of a Github gist into a post or page. The gist will be automatically embedded in your blog.


* Usage:
* Paste a gist link into a blog post or page and it will be embedded eg:
* https://gist.github.com/2926827
* If a gist has multiple files you can select one using a url in the following format:
* https://gist.github.com/2926827?file=embed-gist.php
wp_embed_register_handler( ‘gist’, ‘/https:\/\/gist\.github\.com\/(\d+)(\?file=.*)?/i’, ‘wp_embed_handler_gist’ );

function wp_embed_handler_gist( $ matches, $ attr, $ url, $ rawattr ) {

$ embed = sprintf(
‘<script src="https://gist.github.com/%1$ s.js%2$ s"></script>’,
esc_attr($ matches[1]),
esc_attr($ matches[2])

return apply_filters( ’embed_gist’, $ embed, $ matches, $ attr, $ url, $ rawattr );




Add PayPal Donate Button

Add this snippet to the functions.php to add ‘Donate’ Button on your WordPress Website

[php]// paypal donate button
function cwc_donate_shortcode( $ atts ) {
‘text’ => ‘Make a donation’,
‘account’ => ‘REPLACE ME’,
‘for’ => ”,
), $ atts));

global $ post;

if (!$ for) $ for = str_replace(" ","+",$ post->post_title);

return ‘<a class="donateLink" href="https://www.paypal.com/cgi-bin/webscr?cmd=_xclick&business=’.$ account.’&item_name=Donation+for+’.$ for.’">’.$ text.'</a>’;

add_shortcode(‘donate’, ‘cwc_donate_shortcode’);[/php]


Leave a Comment