WPRM Little Longer Example: relevant related posts from Google

This plugin enables you to manage RESTful APIs data starting from establishing a connection to templating the results. Here is a example puts WPRM in action.

In this example, we will return a  relevant related posts from your favorite site using Google Custom Search API. Please, make sure that:

  1. Establishing Connection:
    If you checked the API’s documentation, as we learnt before, you will need to use a URL which have 2 parts:

    • base url : https://www.googleapis.com/customsearch/v1
    • parameters : key(global Google api access key), cx(custom Search API Engine ID), q(search term), num(number of results).
    • Data format is JSON.
    • If your API are authorized with Email and password, so you can check the option of authorized connection and add username and password.
    • If you have any header extra data (sometimes api documentation need you to add authorization key in the header instead of the parameters), add as many as you like in the ‘Extra header data’ section.

    Once you now that, go to Restful Manager > Connection, add a new connection, give it a relevant name(you will need it soon)  and add the data as in the following screenshot then click Save.
    Note: You can get api key to the API and get Engine ID read the documentation and get the id from here. Also, you will be asked to add a website or a blog to retrieve your results from.
    conn

  2. Create request and Connection Settings:
    Next, we need to assign those parameters to suitable data so that the web service can query it and return the required data response.
    Now, go to Restful Manager > Requests, add a new request. and give it a name, then select the connection name that you have just created. When the parameters show up, populate the parameter values as you see in the following screenshot.
    As you can see, there are a lot of options in Parameter values setting, most of them is to set the dynamic data retrieval which makes this plugin powerful:
    key: will be fixed value since it doesn’t change.
    cx: the same, fixed value.
    q: this one will be dynamic. We want the results to be as relevant as possible so we will use the post title as the search term so now the results will be different once the post ch
    num: here i make it fixed so it only output 3 related posts.
    Once you populated those fields, click save.
    req
  3. Request Display Settings:
    After you save your values, you will find new sections are generated in the request page. One of them is ‘Display Settings’ which responsible for what you want to do with the response results:

    • Display Vs. Save then Display: WPRM give a chance to save selected items from the results in database or just display it in runtime. In this example, we will just choose ‘runtime request’ to just display the results.
    •  Where to display results: decide where to show the results template by selecting post type and then before or after content or you can just use a shortcode to add it wherever you like. We will show the related posts after the post content.
    • Showing repeatable results: select where or not to loop through results. This used only when you have a group of results that share the same group of parameter keys and that is the case here. All search results share the same param key so we will check this option.
    • Repeatable Results Root: Sometimes the part of the results that you need is not quite in the result’s top level but in some deeper level. To populate it, you need to run the initial request with dummy data first(check the results screenshot below).
      Note: the results will be look look this:
      items[
      Repeatable-0 [
      data …
      ],
      Repeatable-1 [
      data …
      ],
      repeatable-2 [
      data …
      ],
      ]
      while Repeatable was referred to as Sequence in older versions.
      display
    • Start Checking dummy data: add all values in this section with full fixed(no dynamic work is here) dummy(but correct) data then click ‘check request’ once you see results check only the data that you want it to show up. Here we will check the 4 main data: htmlTitle, link, htmlSnippet and src(for post thumbnail). Once you’are finished click save.results
  4. Templating Results:
    Now every thing is ready except how the results will look like!! so here comes the last section, ‘Template Settings’. You will find in it 2 parts:

    • Header or non-repeatable Template: this is the template that we’ll use. you can add text inline style and html to template you results. The links below the editor is a shorthand to help you copy and paste to the editor:
      • the results keys
      • Functional helpers like: if/ifExists/section
    • Repeatable template: This will template each repeated item at a time.
      temp
      Save the changes once you finish.

To see the output, open one of your posts scroll to after content section. you will find the results.
samples

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s