WordPress

5 life saving code snippets to make your WordPress website maintaining delightful

Wordpress

I have been searching for a good topic to write on, since past few days and today I finally found it while working on WordPress. While working with WP I realized that there are a lot of things that we can do. I hope these
following snippets will help you guys out there.

  • Disable theme change property
    While creating websites based on WordPress for clients, the main thing that you need to ensure is that clients do not switch themes. For this you just need to add this piece of code to your functions.php file.

    add_action('admin_init', 'remove_theme_menus');
    function remove_theme_menus() {
    global $submenu; unset($submenu['themes.php'][5]);
    unset($submenu['themes.php'][15]);
    }
    Once saved, this will disable/remove the “Theme” submenu from WP Dashboard. And client won’t be able to switch themes anymore.
  • Reduce “SPAM” on your WP blog
    If you are sick of spammers then you can use .htaccess file to reduce spams too. Eventhough “Akismet” helps a lot but using .htaccess too can be an option. Paste the following lines of code into your .htaccess file.

    <IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteCond %{REQUEST_METHOD} POST
    RewriteCond %{REQUEST_URI} .wp-comments-post\.php*
    RewriteCond %{HTTP_REFERER} !.*yourdomainname.* [OR]
    RewriteCond %{HTTP_USER_AGENT} ^$
    RewriteRule (.*) ^http://%{REMOTE_ADDR}/$ [R=301,L]
    </IfModule>
    Simply save it and we are done. The main aim of the above code is to prevent spam bots to directly access wp-comments-post.php file directly, which is used to post the comments on blog. 

    P.S.
    * Don’t forget to replace ‘yourdomainname’ by your real domain name.
    * If you wonder where you will find .htaccess file, it is located at the root of your WordPress install. (Its good to take back up of your .htaccess file before editing.)

  • Simplify WordPress login URL
    How about making your WordPress Dashboard URL into something simple like http://mysite.com/login. Worying how to do this? Simply copy and paste the following line of code into .htaccess. And you have it.

    RewriteRule ^login$ http://website.com/wp-login.php [NC,L].
  • Display your latest Google+ updates on WordPress
    Google+, new project launched by Google, has been growing rapidly since it’s launch. If you are using Google+ then at present or in future you may want to display latest feeds from Google on you WP site/blog. To do that all you have to do is put the below code wherever you would like to display the feeds.

    <?php
    include_once(ABSPATH.WPINC.'/rss.php');
    $googleplus = fetch_feed("http://plusfeed.appspot.com/114670794619799411640"); // Replace 114670794619799411640 by your own Google+ ID
    echo '<a href="'; echo $googleplus->items[0]['link']; echo '">';
    echo $googleplus->items[0]['summary'];
    echo $googleplus->items[0]['updated']; //Optional echo '';
    ?>
    P.S
    * items[0] : Update number to show. Ex. [0] to display the last update, [1] to display the second last update, and so on.
    * [‘summary’] : You can display either ‘summary’ or ‘title’ of your update.
    * [‘updated’] : This shows the post published date. It is optional.
  • Replace words easily in your WP post
    Lets say, you renamed your blog from “blogmine” to “myblog”. Then in this case you need to change every single occurence of word “blogmine” to “myblog”. Before editing your each word wherever it is located, stop and have a look on following function. Following extremely useful function will do it for you, in just a minute. Simply paste the following line in your function.php

    function replace_text_wps($text){
    $replace = array(
    // 'WORD TO REPLACE' => 'REPLACE WORD WITH THIS'
    'blogmine' => 'myblog', // changes every single occurence of "blogmine" to "myblog"
    'excerpt' => '<a href="#">excerpt</a>', //changes every single occurence of "excerpt" with links
    'function' => '<a href="#">function</a>' ); $text = str_replace(array_keys($replace), $replace, $text); return $text;}add_filter('the_content', 'replace_text_wps');
    add_filter('the_excerpt', 'replace_text_wps');

    Save the file and you’re done.

So this is it. I hope these will help you at some point of time. And do not forget to subscribe, follow and like us ;)