Monday, 31 October 2011

How to keep a wordpress site in Quick maintenance mode?

When we are doing some maintenance to our blog at that time we should keep the users not to visit our site. To achieve this we have to keep the blog in maintenance mode.

Some cms (content management systems) will have an inbuilt option at admin panel to keep the website(blog) in maintenance mode or offline mode. In wordpress we don’t have the inbuilt option at admin panel to keep the blog in maintenance mode.

We can achieve this by adding a plugin or by adding the hook to your theme functions.php.

If you want to add the hook copy & paste the following code to your theme functions.php file.

function wpr_maintenance_mode() {  
    if ( !current_user_can( 'edit_themes' ) || !is_user_logged_in() ) {
        wp_die('We are into Maintenance, please come back soon.');
add_action('get_header', 'wpr_maintenance_mode');

The above code will not allow any user(except admin) to visit the site .

When you are done with the maintenance remove the code from the functions.php .otherwise the users will not able to enter into the site.

Enjoy the maintenance.

Monday, 24 October 2011

How to add custom content to RSS Feed posts in wordpress?

In the previous posts I shared how to add custom content to wordpress posts. Now I will share you how to add custom content to RSS feeds.
RSS (Rich Site Summary) is a format for delivering regularly changing web content. Now a day the sites using RSS feeds are increasing rapidly. Any end user may subscribe to the site RSS feeds .The users who subscribed will get all the latest updates, the user will directly go to the RSS feed our website and see the posts. Like this we may increase the number of users but the subscribed user even won’t come back to our website.

To make our subscribers to visit our home page frequently here is the solution. Add the site home link to all your RSS feeds. To achieve this add the following code to your theme functions.php file.

function addToRSS($content) { return $content.'Remember to stop by at Indian Recipes '; }
add_filter('the_excerpt_rss', 'addToRSS');
add_filter('the_content_rss', 'addToRSS');

Wednesday, 19 October 2011

How to display parent page title in sub pages?

WordPress will have an option to create sub page for a page. We may have any number of sub pages for a parent page .Suppose if we created pages in this way like parent and child relation and if we want to display the parent page in all the sub pages just follow these steps.

    wp_title('');  function is used to display the title of current page.
echo $pagetitle;

Place the above code where you want to display the title of the parent page. In the above code first it will check that the page has a parent page or not. If it is true then it will display the title of the page.

The above code will be very easy for the WordPress advanced developers. I am working on WordPress from so many days but I did observe this $post->post_parent. There may be someone like me that’s why I am sharing this.

Hope it is useful.

Wednesday, 12 October 2011

How to set post thumbnail as image for facebook share?

When we write or read any article and if we found that it as useful then we will share it to our friends or any known persons. We will make this is to spread that useful information to all. For this we have social bookmarking sites like facebook, twitter, and digg and delicious etc.

Facebook is the popular social bookmarking site; it will have two options for Share the data. One is facebook like button to like any post and another one is facebook share button. Facebook share button works same like facebook like button.

Facebook has some set of open graph Meta tags, which are to decide which image to show.

<meta property="og:image" content=""/> is the open graph  meta tag to find the image for facebook share. This should be present in the header before </head>.When we want share any article it will check for above tag , if it is not present then it will check for the older method of specifying an image  

<link rel="image_src" href=" "/>.

If neither is present then it will read the whole page and gives the all available types of images like jpeg, png, gif on that page and it will ask us to select any thumbnail image before posting the link to profile. 

To avoid the selection of images from the content and to define a particular image as thumbnail, place the following code in the header file.

<link rel="image_src" href=" "/>

Replace the image url with your logo url and it will display the same default image for all the posts you share.

For Wordpress Sites

The above code will work if you want to use same custom image for all the posts, but if you want to use related image for individual post or page use the following code.

Copy and paste the following code in the functions.php of your theme to define the post thumbnail as thumbnail for facebook share.

add_action( 'wp_head', 'fb_thumbnails' );
Function fb _thumbnails ()
global $posts;
$ thumb = '';
$content = $posts[0]->post_content; // $posts is an array, fetch the first element
$output = preg_match_all( '/<img.+src=[\'"]([^\'"]+)[\'"].*>/i', $content, $matches);
if ( $output > 0 )
$thumb = $matches[1][0];
echo " <link rel=\"image_src\" href=\"$thumb\" /> ";

Replace the $ thumb variable url with the one which you want to specify as default image if there is no image in the post content.

Sometimes after placing the code also the results will not display properly, because Facebook crawler  will access your page and cache the meta information .Facebook will scrape the page for every 24 hours. To force facebook server to clear the cache immediately use Facebook Url Debugger /Facebook  Linter Tool .

Friday, 7 October 2011

How to disable wordpress theme change?

If we are developing any website, we will try different layouts to check which one is suitable for the website. Like this I tried many themes for one of my client website and finalized one layout among all of them and customized it according to my client requirements.

At the time of theme customization we will place the widgets at different locations where have the chance to place the widgets like sidebar and footer and header.

If we switch from the current theme to any another theme and come back, then the widgets which we placed in our current theme will be lost and again we have to place the widgets.

I faced this problem for one of my client website and then I decided to restrict switch the theme.

For that I added the following code and it works well for me and solved my problem

add_action('admin_init', lock_theme');
function  lock_theme() {
            global $submenu, $userdata;
            if ($userdata->ID != 1) {

Hope this will be useful.

Monday, 3 October 2011

How to add custom content to each post automatically?

Many bloggers will have a habit to write custom content after each post. Like “We welcome your suggestions on this blog “ Or something like ask readers to subscribe their RSS feed “If you like this post subscribe to our rss feed”.

We can hard code this content after the post content or we can add it every time when we write a new post. Adding this content every time, when we write a new article is not the right choice.

To automate this one add the following code to your theme functions.php i.e

function addFootNote($content) {
        if(is_feed() || is_single()) {
                $content.= "<div class='subscribes'>";
                $content.= "<h4>like this article?</h4>";
                $content.= "<p>Subscribe to our  <a href='>RSS feed</a> </p>";
                $content.= "</div>";

        return $content;