LiveLink Case Study<>

Adding a PHP script to the WordPress template

Adding the script

Before performing this procedure you must install the Executable PHP Widget plugin. This plugin allows you to inject the necessary PHP code into the dynamic sidebar you will use on the course page.

» For more information see http://wordpress.org/extend/plugins/php-code-widget/

Note: If you are concerned about the security aspects of this method (e.g. in a situation where multiple administrators had access to the content editing functions) you could add the PHP directly to the WordPress PHP template files.

To add the PHP script

Assuming that you have installed the Executable PHP widget:

1Choose Appearance > Widgets in the WordPress administration section.

2Locate the sidebar used with the course pages and drag across a PHP Code widget.

3Open the code widget and paste in the following PHP:

<?php
  $liveLinkFeed = "https://demo.campusit.net/pls/demo/solar.qll_web.common";
  $liveLinkService = "ONLINE_APP";
  $courseInstanceCode = get_field('course_instance_code');
  $liveLinkButton = "/_layout/images/but_apply.png";
  if ($courseInstanceCode=='')
    moreinfo();
  else {
    $liveLinkURL = $liveLinkFeed."?serviceCode=".$liveLinkService."&instanceCode=".$courseInstanceCode;
    $xml = simplexml_load_file($liveLinkURL);
    $serviceURL=$xml->xpath('//@url');
    if (empty($serviceURL))
        moreinfo();
    else
        echo "<p class='applybutton'><a href='".$serviceURL[0]."'><img src='".get_bloginfo('template_url').$liveLinkButton."'/></a></p>";
    }
function moreinfo()
{
    echo "<p class='moreinfobutton'><a href='".get_bloginfo('url')."/?page_id=362'><img src='".get_bloginfo('template_url')."/_layout/images/but_more.png'/></a></p>";
}
?>


The components of the script are analysed below.

Breakdown of the script

Set variables

  $liveLinkFeed = "https://demo.campusit.net/pls/demo/solar.qll_web.common";
  $liveLinkService = "ONLINE_APP";
  $courseInstanceCode = get_field('course_instance_code');
  $liveLinkButton = "/_layout/images/but_apply.png";


The location of the LiveLink feed and the LiveLink service required (Apply Online) are assigned to variables.

The course instance code which the user entered when setting up the course details page is also assigned to a variable.

These variables will be used to construct the LiveLink URL.

The file name of the button graphic is also assigned to a variable. This helps to make the script easy to reuse for other services such as Book and Pay.

Create the LinkLink URL query string

  if ($courseInstanceCode=='')
    moreinfo();
  else 
    $liveLinkURL = $liveLinkFeed."?serviceCode=".$liveLinkService."&instanceCode=".$courseInstanceCode;


Checks whether a value has been entered in the Course Instance field. If no value is entered, the moreinfo() function is called. This displays the More Information button.

If a value has been entered the script creates the LiveLink URL. This URL will limit the returned XML to the Apply Online service node relating to the specified course instance.

Parse the returned LiveLink XML to get the service’s URL

    $xml = simplexml_load_file($liveLinkURL);
    $serviceURL=$xml->xpath('//@url');


This uses PHP’s simplexml functions (see http://www.php.net/manual/en/book.simplexml.php) to get the URL of the service from the url attribute in the service element of the returned course instance.

The returned URL is stored in the $serviceURL array. Since we have one apply online service per course instance there will be only a single value in the array.

If no URL returned, display the More Information button

    if (empty($serviceURL))
        moreinfo();


If the course instance is NOT matched, no nodes are returned and the $serviceURL array will be empty. In these circumstances the moreinfo() function is called. This displays the More Information button.

If URL is returned, create the Apply Online button

else
        echo "<p class='applybutton'><a href='".$serviceURL[0]."'><img src='".get_bloginfo('template_url').$liveLinkButton."'/></a></p>";
    }


If the course instance IS matched an Apply Online button linked to the URL is created using the URL value stored in the $serviceURL array.

Create the More Information button

function moreinfo()
{
    echo "<p class='moreinfobutton'><a href='".get_bloginfo('url')."/?page_id=362'><img src='".get_bloginfo('template_url')."/_layout/images/but_more.png'/></a></p>";


The moreinfo() function creates a ‘More Information’ button. This button is displayed if the Apply Online service is not available. It directs you to a WordPress page (in this example page_id=362) which tells you who to contact for more information.