Friday, 2 December 2011

How to retrieve remote page data in wordpress?

Wordpress will have an inbuilt function to get data from a remote page which is located in wp-includes/http.php.
Here is the syntax of the function:

$url is the url of the page from which we want to get data.

It returns the array of information which includes headers and content.
Use the following code where you want to get the page data.

$response = wp_remote_get( '' );
if( is_wp_error( $response ) ) {
echo 'Something went wrong!';
} else {
echo 'Response:';
print_r( $response );
echo '';

The above code will print the array like the below format

[headers] => Array
[date] => Thu, 30 Sep 2010 15:16:36 GMT
[server] => Apache
[x-powered-by] => PHP/5.3.3
[x-server] =>
[expires] => Thu, 30 Sep 2010 03:16:36 GMT
[cache-control] => Array
[0] => no-store, no-cache, must-revalidate
[1] => post-check=0, pre-check=0

[vary] => Accept-Encoding
[content-length] => 1641
[connection] => close
[content-type] => application/php
[body] => This is a website!
[response] => Array
[code] => 200
[message] => OK

[cookies] => Array


Wednesday, 2 November 2011

How to add featured image to wordpress theme?

In wordpress we can display the images in two ways.

1. in the post content.
2. as a featured image.

Featured image is introduces since worpress 2.9, also known as post thumbnails. There is no rule that every wordpress theme should be with featured images. Using featured image in a theme will give good look and clear presentation of the posts with the image.

Now I will explain adding featured image to wordpress posts and to the wordpress theme.

First we will check whether the theme supports adding a featured image. For this edit the posts which are published or add a new post. If the theme supports featured images, the new opened window (post or edit screen) will have an option in the sidebar like the below image. 

If the option is not available in the sidebar, it means that your theme does not support featured image.
Now it’s the time to add featured image support to wordpress theme. We can add featured image support to whole wordpress theme or we can add to only posts or to only pages. For this open your theme functions.php and add the following code.

To add thumbnail support for all post types, add the following code
if(function_exists(‘add_theme_support’)) {

To add thumbnail support for only posts add the following code
if(function_exists(‘add_theme_support’))  {
add_theme_support(‘post_thumbnails’, array('post'));

For pages add the following code
if(function_exists(‘add_theme_support’)) {
add_theme_support(‘post_thumbnails’, array(page));

Adding the above code will enable the featured image support. Now open the post or edit screen you should now see your featured image added to the sidebar to indicate you were successful.
I hope all you know how to add featured image to wordpress posts.

Following the above process will add the featured image support to wordpress theme now we will see how to display the featured image on wordpress theme.

To display the post thumbnails add the following code in the query post loop where you want to display the post thumbnail.
    if (has_post_thumbnail()) {
      We have three predefined functions used to check and display the post thumbnail.

      has_post_thumbnail() returns ‘true’ if a thumbnail has been set.
      the_post_thumbnail() echos a string containing the thumbnail <img> tag.
      get_the_post_thumbnail() returns a string containing the thumbnail <img> tag.

      the_post_thumbnail(),get_the_post_thumbnail()  will have two optional arguments.
      The first option is the size of the thumbnail.The values for this option are

         1.Default thumbnail sizes like a string containing the text ‘thumbnail’, ‘medium’ or ‘large’. The values of this ‘thumbnail’, ‘medium’ or ‘large’ will be defined in wordpress media screen under settings in wordpress dashboard(Settings > Media screen).
 array with new width and height dimensions, e.g. array(120, 90).
      The second one is an associative array containing the src, class, alt and title.
      the_post_thumbnail(array(120,100),array('src'=>anjali.jpg','class'=>'thumbnail','alt=>'post thumbnail','title'=>'Title'));

      Enjoy using the featured image.

      How to add shortcodes in wordpress sidebar text widget?

      In wordpress we have a nice concept called shortcodes. A shortcode is WordPress-specific codes that will do the things in an easy way. We have several default shortcodes and a shortcode will be inserted in a post or page file by embedding square braces.

      Once I got a requirement to add the shortcode in the sidebar. So I just edited the sidebar.php file and added shortcode in the sidebar. As a developer I know how to add the code in the file so i can do it in that way. This notes is for the one who does not know how to embed the code.  For this add the following line of code to your theme functions.php file.

      add_filter('widget_text', 'do_shortcode');

      Enjoy using the shortcodes

      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;

      Friday, 30 September 2011

      How to prevent update checks for installed plug-ins in wordpress?

      Plug-in is the one which is used to add some additional functionality to the blog. We have many free plug-in in the repository to extend the functionality.

      If we get any plug-in, for our requirement we can use it directly or else we will customize the plug-in according to our requirements.

      Each time the wordpress will check for the updates in the repository by using the function wp_update_plugins( ) in wp_includes/update.php. If there is any new version available we will get a notification at wordpress plugin dashboard.

      If anyone clicks on update the plug-in will be updated to latest version, then the changes what we done will be lost and again we have to update the customizations. There is no hook to avoid this problem.

      I faced this problem for one of my website. Each time, when the notification comes for the plug-in my client will update it.

      To avoid this problem I added the following code to the plug-in for which I want to exclude from update check.

      add_filter( 'http_request_args', 'prevent_update_check', 10, 2 );
      function  prevent_update_check( $r, $url ) {
          if ( 0 === strpos( $url, '' ) ) {
              $my_plugin = plugin_basename( __FILE__ );
              $plugins = unserialize( $r['body']['plugins'] );
              unset( $plugins->plugins[$my_plugin] );
              unset( $plugins->active[array_search( $my_plugin, $plugins->active )] );
              $r['body']['plugins'] = serialize( $plugins );
          return $r;

      Tuesday, 27 September 2011

      How to avoid the “XML Parsing Error: XML or text declaration not at start of entity” in wordpress feed?

      Web feed or news feed is a format which is to represent the updated data for the users or subscribers. Wordpress has in built functionality for the generation of rss feeds for website.

      Sometimes while accessing the rss feed it will throw a parsing error i.e

      XML Parsing Error: XML or text declaration not at start of entity.

      This error will cause if there is a blank space in the theme files or in the core files of wordpress.
      To avoid this error remove the blank spaces before or after the <?php ?> tags  or add the following code.

      We have different plug-ins to check the spaces in the files and to remove ,but the best solution is to add the following code. I resolved the problem is like this only. Adding a piece of code is better than using one more plugin for a website.Its better to use very less plug-ins for a website.

      $out = ob_get_contents();  
      $out = str_replace(array("\n", "\r", "\t", " "), "", $input);

      Add this code in the following files ,located root directory of your  wordpress site.


      Thursday, 15 September 2011

      How to get post ancestors of a post?

      In wordpress we have an option to assign a post as a child to another post. In order to get the ancestors of a post wordpress have an inbuilt function i.e.  get_post_ancestors( ).

      This function will have one argument that can be either post id or post object and it returns the ancestor’s ids as an array. 


      Tuesday, 13 September 2011

      How to Activate or Deactivate wordpress plug-in programmatically?

      Recently I had a problem with wordpress plug-in display at the dashboard. I searched on the web but did not find a proper solution. At that time I got a requirement to install one plug-in, I uploaded the plug-in from ftp and the uploaded plug-in is not displayed in the dashboard to activate.

      For this I tried the following hook and done the activation of the plug-in.

      function activate_plugin() {    
               // Absolute path to your specific plugin
      $my_plugin = WP_PLUGIN_DIR.'/wp-email/wp-email.php';
       // Here wp-email is the plug-in which i want to activate. Replace it with the plug-in which you want to activate.
          // Check to see if plugin is already active
          if(is_plugin_active($my_plugin)) {
              // Deactivate plugin
              // Note that deactivate_plugins() will also take an
              // array of plugin paths as a parameter instead of
              // just a single string.
          else {
              // Activate plugin

      Add the above code to the functions.php of your current theme .Once the activation is done comment the code, because we are activating the plug-in if It is in deactivated state and we are deactivating the plug-in if it is active state. So if we did not remove the code the plug-in will be deactivated again.
      If you don’t need to deactivation functionality just comment the following line and no need of commenting the whole function
      This code made my work easy that’s why I am sharing it , so it may be useful for someone.

      Thursday, 28 July 2011

      How to add shortcode to wordpress widget?

      Wordpress has one great feature called shortcode for adding some special content to the posts or pages. Shortcodes are the keywords with the square brackets.

      Ex: [gallery] is the shortcode to insert a gallery into a post or page.Place this shortcode in the page where you want to display the gallery.

       Like this adding a shortcode to the page or post is very simple then we will get a question “how to add shortcode to widgets?”

      If we add a shortcode to widget in the same process of adding to a post, then the shortcode ([gallery] in the above example) only be displayed instead of special content.

      To add shortcode to the widget follow these steps.
      1. Login into wordpress admin page and go to the Theme Editor — i.e. select Apperance > Editor from admin menu.

      2. Select Functions or functions.php from the list of files in the right panel,and click on it to load it into the editor.

      3. Then add the following line of php code.
             add_filter('widget_text', 'do_shortcode');          
      1. Click on the Update File button to save the file.
      That’s it.Now if you add shortcode to the sidebar it will process the code and will display the special content.

      Wednesday, 27 July 2011

      How to add google +1 button to wordpress post page?

       All you know that google recently launched google+, and it has the high features for sharing the website content( Promotion).Google +1  button is one of the feature of google+ for social recommendation. This feature is like facebook like button.

      Here are the simple steps to add +1 to wordpress post page or archive page.

      Get the code from Google+

      The code will be like this.

      <! -- Place this tag in your head or just before your close body tag -->
      <script type="text/javascript" src=""></script>
      <! -- Place this tag where you want the +1 button to render -->
      <g:plusone href=""></g:plusone>

      Now open your theme header file and place the following code in the header file before </head> tag.

      <! -- Place this tag in your head or just before your close body tag -->
      <script type="text/javascript" src=""></script>

      Then we need to place the +1 button. Open your sinle.php file and place the following code in the file where you want to render the button.

      <! -- Place this tag where you want the +1 button to render -->
      <g:plusone href="<?php the_permalink(); ?>"  size="small" ></g:plusone>

      Here we have the option to change button sizes.

      Size="small"  - To display small size +1 button of 15px
      Size="medium" - To display medium size +1 button of 20px
      Size="large" - To display large size +1 button of 60px

      That’s it .This will display +1 button on wordpress post page.

      Enjoy Sharing.

      Thursday, 21 July 2011

      How to avoid the image alignment problem in Wordpress?

      Wordpress has an excellent feature of adding images into a post while at the time of adding a new post or at the time of editing post. After adding images into post we can align those to different positions like left, right, center and none along with the text.

      Some times we will get a problem like the image is not aligned properly as per the selection. To avoid this problem add the following lines of code into your selected theme stylesheet.

      /* =WordPress Core
      -------------------------------------------------------------- */
      .alignnone {
          margin: 5px 20px 20px 0;

      .aligncenter, div.aligncenter {
          margin: 5px auto 5px auto;

      .alignright {
          margin: 5px 0 20px 20px;

      .alignleft {
          margin: 5px 20px 20px 0;

      .aligncenter {
          display: block;
          margin: 5px auto 5px auto;

      a img.alignright {
          margin: 5px 0 20px 20px;

      a img.alignnone {
          margin: 5px 20px 20px 0;

      a img.alignleft {
          margin: 5px 20px 20px 0;

      a img.aligncenter {
          display: block;
          margin-left: auto;
          margin-right: auto

      .wp-caption {
          background: #fff;
          border: 1px solid #f0f0f0;
          max-width: 96%; /* Image does not overflow the content area */
          padding: 5px 3px 10px;
          text-align: center;

      .wp-caption.alignnone {
          margin: 5px 20px 20px 0;

      .wp-caption.alignleft {
          margin: 5px 20px 20px 0;

      .wp-caption.alignright {
          margin: 5px 0 20px 20px;

      .wp-caption img {
          border: 0 none;
          height: auto;
          max-width: 98.5%;
          width: auto;

      .wp-caption p.wp-caption-text {
          padding:0 4px 5px;

      If you need more information about this issue just refer the following link

      Wednesday, 13 July 2011

      Wordpress login url change with .htaccess

      Thought to change Wordpress default login url "" to something like "" , tried this code and it works fine for me and i am sharing this to all who want to change like me.
       To do this you have to add the following code to your .htaccess file which is located at your root directory of your wordpress install.

      RewriteRule  ^wlognin$ [NC,L]