How to get WordPress posts in Facebook App

In this simple tutorial we are going to show you how to get wordpress posts in facebook app. Application is going to displays recent posts from your wordpress blog as a list of clickable links with short description. This task consists of those 3 simple steps.

1. Write code

At first You should create a directory let’s say facebookapp-get-recent-posts. Then create an index.php file in thisdirectory. Fill index.php file with the following code. Take a closer look at the code comments.


   // Load WordPress Environment
   require_once( '[WORDPRESS_PATH]/wp-load.php' );

   $posts = get_posts('numberposts=15');
   // Iterate throught the results
   foreach ($posts as $post) {
      // Get post title
      $post_title = $post->post_title;
      // Get post url
      $post_url = get_permalink($post->post_ID);

      // Get post content
      $post_data = get_post($post->ID);
      // remove html tags from content
      $post_short_description = strip_tags($post_data->post_content);
      // Get first 150 signs as a description - we assume that post content has more than 150 signs
      $post_short_description = substr($post_short_description,0,150);    // 0 is the string start index and 150 is cut length
      // Add some dots to get some fancy look
      $post_short_description = $post_short_description . '...';

      echo '<div>';
      // Construct post URL
      echo '<a target="_blank" href="'.$post_url.'">'.$post_title.'</a>';
      echo '<div>';
      echo $post_short_description;
      echo '</div>';
      echo '<br/>';
      echo '</div>';

We use WordPress API in this code to list recent posts and get post data. Remember to change [WORDPRESS_PATH] (line 3) to a path that suits your environment.

2. Upload code to wordpress

Now You need to upload facebookapp-get-recent-posts directory to the main directory of your wordpress installation. Now your application is available at facebookapp-get-recent-posts

3. Publish code as a facebook application

All You need to do now is publishing code as the application on facabook. Watch our quick tutorial on that topic here.

Done! :)

Download our sample code here.

Leave a Reply

Want to join the discussion?
Feel free to contribute!

Leave a Reply

Your email address will not be published. Required fields are marked *


You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>