• 17 Great WordPress Hacks You Might Need Some Day

    Advertisement

    You can find hundreds of WordPress hacks,tips and tricks and some of them might be useful but some of them might not.Wordpress is being improved by the bloggers’ help and the coders find solutions almost everything about wordpress.
    When you do a Google search for your needs in wordpress i’m sure you can find whatever you want but today i’ve collected some interesting wordpress hacks which some of them will be useful for you in the future.The below hacks are all about inserting codes to your blog’s functions.php file so i highly recommend to backup your original functions.php file.



    1.How to Automate The Year In Copyright Notice
    You know mostly in the footer part of wordpress blogs there is a copyright notice with year.The year is not automated so you have to enter one a year.If you want to automate this(year),here is the code.
    Note:2008 is the first year you launced your blog.

    © 2008- 

    Source

    2.How to Change ‘mystery man’ Default Gravatar
    If a user who comments on your blog didn’t sign up to Gravatar.com then the default gravatar shows in coment box as a thumbnail. You can change the default gravatar from the settings page but if you don’t like those gravatars then you can customize a custom gravatar for your blog.
    Upload a gravatar to your images folder and paste the code in your functions.php file.(Write the image file name as you see in the code and you can change Designbeep to whatever you want.That will be the name of the gravatar)

    //Make a new default gravatar available on the dashboard
    add_filter( 'avatar_defaults', 'newgravatar' );
    function newgravatar ($avatar_defaults) {
    $myavatar = get_bloginfo('template_directory') . '/images/YOUR GRAVATAR.jpg';
    $avatar_defaults[$myavatar] = "Designbeep";
    return $avatar_defaults;
    }

    Source
    3.How to Allow Contributers to Upload Files
    Some blogs have contributers but they cannot upload files.If you want to give a new role to your contributer with all the capabilities of a ‘contributor’ as well as the ability to upload files and add the below code to your functions.php file

    add_role('special_contributor', 'Special Contributor', array(
    'read' => true,
    'edit_posts' => true,
    'delete_posts' => true,
    'upload_files' => true,
    ));

    Source

    4.How to Show the Posts with Dates In Archives
    When you click on the archives in any blog you just see the months but not the exact date.If you want to add the exact date then do the following.Add the below code to your functions.php file.

    //Linking date published to date archives
    add_shortcode( 'entry-link-published', 'my_entry_published_link' );
    function my_entry_published_link() {
    /* Get the year, month, and day of the current post. */
    $year = get_the_time( 'Y' );
    $month = get_the_time( 'm' );
    $day = get_the_time( 'j' );
    $out = '';
    /* Add a link to the daily archive. */
    $out .= '<a title="Archive for ' . esc_attr( get_the_time( 'j F Y' ) ) . '" href="' . get_day_link( $year, $month, $day ) . '">' . $day . '</a>';
    /* Add a link to the monthly archive. */
    $out .= ' <a title="Archive for ' . esc_attr( get_the_time( 'F Y' ) ) . '" href="' . get_month_link( $year, $month ) . '">' . get_the_time( 'F' ) . '</a>';
    /* Add a link to the yearly archive. */
    $out .= ' <a title="Archive for ' . esc_attr( $year ) . '" href="' . get_year_link( $year ) . '">' . $year . '</a>';
    return $out;
    }

    Then;

    add the following code to your template

    
    

    Source

    5.How to Detect Google Visitors and Give a Special Greeting
    Put this snippet where you want the message to be.

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

    Source

    6.How to Use Shortcodes in Widget
    If you want to add shortodes in a widget then just add a filter in your functions.php file

    add_filter('widget_text', 'do_shortcode')

    Source

    7.How to Share via E-mail Link
    If you want to share your articles via e-mail add the following code into single.php file.It will appear as a text.You can change the ”Share via Email to whatever you want.

    ?php echo "<a title="Email to a friend/colleague" href="mailto:type%20email%20address%20here?subject=I%20wanted%20to%20share%20this%20post%20with%20you%20from%20<?php bloginfo('name'); ?>&body=<?php the_title(); ?> - <?php the_permalink(); ?>" target="_blank">Share via Email</a>"; ?>

    If you want to share as an excerpt then add following code to

    &body=
    %20%20%3A%20%20
    %20%20%2D%20%20%28%20
    %20%29" title="Email to a friend/colleague" target="_blank">Share via Email"; ?>

    Source

    8.How To Display Total Number Of Blogroll Bookmarks
    A quick tip from Jeff Starr.(digwp.com)
    If you want to display the total number of blogroll bookmarks add the follwing code to your theme file.

    get_var("SELECT COUNT(*) FROM $wpdb->links WHERE link_visible = 'Y'");
    if (0 < $numlinks) $numlinks = number_format($numlinks); ?>

    Then,

    Replace the below code whereever you want to show it.

    
    

    Source

    9.How to Remove Autolinks In Comments
    When a user puts a link in his/her comment,wordpress automaticaly converts that link to a clickable link.If you want to disable this then add the below code in your functions.php file

    remove_filter('comment_text', 'make_clickable', 9);

    Source

    10.How to set HTML editor as the default in WordPress
    You know the default editor is the visual editor in wordpress dashboard.If you want to make th HTML editor as default then add the following code in functions.php file

    add_filter('wp_default_editor', create_function('', 'return "html";'));

    Source

    11.How to Modify User Contact Info
    If you want to add more services to the author contact info then add the below code to the function.php file.Lines 2, 3 and 4 are for removing unnecesseray items, and lines 5, 6 and 7 add news items

    function extra_contact_info($contactmethods) {
    unset($contactmethods['aim']);
    unset($contactmethods['yim']);
    unset($contactmethods['jabber']);
    $contactmethods['facebook'] = 'Facebook';
    $contactmethods['twitter'] = 'Twitter';
    $contactmethods['linkedin'] = 'LinkedIn';
    
    return $contactmethods;
    }
    add_filter('user_contactmethods', 'extra_contact_info');

    Source

    12.How to Exclude Pages from WordPress Search Results

    WordPress search results includes the page results as you know.If you want to exclude your blog’s pages from search results then add the following code to the functions.php file

    /* BEGIN Excluding Pages from Search Results */
    function filter_search($query) {
    if ($query->is_search) {
    $query->set('post_type', 'post');
    };
    return $query;
    };
    
    add_filter('pre_get_posts', 'filter_search');
    /* END Excluding Pages from Search Results */

    Source

    13.How to Leverage Browser Caching in WordPress via .htaccess
    If you want to lower the loading times of for example ”images, CSS, PDF’s, JS” then add the following code to .htacces file

    ## EXPIRES CACHING ##
    
    ExpiresActive On
    ExpiresByType image/jpg "access 1 year"
    ExpiresByType image/jpeg "access 1 year"
    ExpiresByType image/gif "access 1 year"
    ExpiresByType image/png "access 1 year"
    ExpiresByType text/css "access 1 month"
    ExpiresByType application/pdf "access 1 month"
    ExpiresByType text/x-javascript "access 1 month"
    ExpiresByType application/x-shockwave-flash "access 1 month"
    ExpiresByType image/x-icon "access 1 year"
    ExpiresDefault "access 2 days"
    
    ## EXPIRES CACHING ##

    Source

    14.How to Replace a Text or Link in All Posts At Once
    It would be hard to add text or link to all your posts one by one.I don’t know if it is useful for you but you can do this by adding the following code to your functions.php file

    function replace_text($text) {
    $text = str_replace('WordPress', '<strong>WordPress</strong>', $text);
    $text = str_replace('WordPress hacks', '<a href="#">WordPress hacks</a>', $text);
    return $text;
    }
    add_filter('the_content', 'replace_text');

    Source

    15.How to Disable Default WordPress Widgets
    You know there are a few default wordpress widgets and if you want to remove them just add the following code to your functions.php file

    // unregister all default WP Widgets
    function unregister_default_wp_widgets() {
    unregister_widget('WP_Widget_Pages');
    unregister_widget('WP_Widget_Calendar');
    unregister_widget('WP_Widget_Archives');
    unregister_widget('WP_Widget_Links');
    unregister_widget('WP_Widget_Meta');
    unregister_widget('WP_Widget_Search');
    unregister_widget('WP_Widget_Text');
    unregister_widget('WP_Widget_Categories');
    unregister_widget('WP_Widget_Recent_Posts');
    unregister_widget('WP_Widget_Recent_Comments');
    unregister_widget('WP_Widget_RSS');
    unregister_widget('WP_Widget_Tag_Cloud');
    }
    
    add_action('widgets_init', 'unregister_default_wp_widgets', 1);

    Source

    16.How to Eliminate Comment Spam
    You can block spammers instead of marking them as spam.The code rejects any comment request posting from a browser that has no reffer. Usually it is a bot. The code checks the for ‘HTTP_REFERRE’. If it’s not defined or incorrect, the execution is stopped with the appropriate message.

    Paste the following code to the functions.php file

    function check_referrer() {
    if (!isset($_SERVER['HTTP_REFERER']) || $_SERVER['HTTP_REFERER'] == "") {
    wp_die( __('Please enable referrers in your browser, or, if you\'re a spammer, get lost!') );
    }
    }
    add_action('check_comment_flood', 'check_referrer');

    Source

    17.How to Remove Symbol […] From Post Excerpt
    If you want to get rid of this symbol ”[…]” , you would only need to insert next code in the functions.php of your theme.

    function trim_excerpt($text) {
    return rtrim($text,'[...]');
    }
    add_filter('get_the_excerpt', 'trim_excerpt');

    Source

    Hidden Options Panel
    If you want to see the hidden options panel of your blog which is not shown in default dashboard then try http://your-blog/wp-admin/options.php

    You will see all the settings.

    4 Responses to “17 Great WordPress Hacks You Might Need Some Day”
    1. Victor

      1 YR CACHING FOR IMAGES?
      YOU ARE INSANE…
      nice article otherwise

    2. Hey nice post, I’m going to use the “Automate The Year In Copyright Notice”.. I’ve always just done it manually each year!!

      Simple things eh!

    3. Frankly speaking many of these things were commonly unknown to me and the title justify’s that we gonna need these one day or another for sure. Thank you for posing these nice tips. I wonder if we can define Etags in .htaccess properly as well?

    4. This was really great! I will be saving these snippets.