• 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.

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

    Source

     
     

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

    Add the following code in your functions.php file

    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;
    }

    Source

     
     

    How to Change the Font in HTML Editor In WordPress 3.3

    Just add this code in your functions.php file

    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;
            }
        </style>
    <?php }

    Source

     
     

    “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 edit_post_link(__('Edit This')); ?>

    Source

     
     

    Add/remove Contact Info Fields

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

    <?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;
    }
    add_filter('user_contactmethods','new_contactmethods',10,1);  
    ?>

    Source

     
     

    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

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

    Source

     
     

    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.

    function login_with_email_address($username) {
            $user = get_user_by('email',$username);
            if(!empty($user->user_login))
                    $username = $user->user_login;
            return $username;
    }
    add_action('wp_authenticate','login_with_email_address');
    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' );

    Source

     
     

    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;

    }

    Source

     
     

    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.

    <?php
    
    /**
     * 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 );
    }
    
    ?>

    Source

     
     

    Add PayPal Donate Button

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

    // paypal donate button
    function cwc_donate_shortcode( $atts ) {
        extract(shortcode_atts(array(
            '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');

    Source

    3 Responses to “10 More Useful WordPress Code Snippets For Your Blog”
    1. derek

      is there a reason why u don’t date your posts? just curious.

    2. Arshad Cini

      No reason Derek.But if you really wanna know the date of the posts you can check the urls

    3. Nice post Arshad, thanks for this info.