How to load jQuery from Google CDN with fallback to a local copy

I’m not saying Google Content Delivery Networks (CDN) is not stable enough to serve just about anything, it is, however, always better to rely on an alternative in case anything went wrong.

Therefore, after researching for a reliable solution and trying defer and dependency options, I must confess that I find the below solution to be acceptable and reliable enough to be adopted.

<!-- Load jQuery from Google CDN -->
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script>
<!-- Load jQuery from a local copy if loading from Google fails -->
<script type="text/javascript">window.jQuery || document.write('<script type="text/javascript" src="URL/to/your/local/jQuery/file"><\/script>')</script>

Just copy and paste the above code into the <head> of your HTML, or preferably just before the </body> and it will do the trick. Just remember to change the URL to the local jQuery file.

Easily add code to wordpress posts

Adding code to wordpress posts, especially if you, like me, use the HTML editor to write your posts, requires that you use HTML escape codes for changing every ‘>’ character into ‘&gt;’ and so on and so forth as described in the wordpress codex.

Several solution are there including plugins that do more than just displaying the code but go beyond that towards formatting it as well. I have switched to using the google-code-prettify and therefore I needed a simple solution to just escape the HTML in <pre> HTML elements.

Therefore, I have put together the following piece of code that can be easily added to the template functions.php file to achieve that simple task.

function skinju_escape_pre ($content)
{
  $pieces = preg_split ( '/(<pre.*>.*?<\/pre>)/si', $content, -1, PREG_SPLIT_DELIM_CAPTURE );

  $procesed_content = '';
  foreach ($pieces as $piece)
  {
    if ( preg_match ( '/(?P<opening><pre.*>)(?P<content>.*)(?P<closing><\/pre>)/imxsU', $piece, $matches ))
    {
      $matches['content'] = str_replace ( '&', '&amp;', $matches['content']);
      $matches['content'] = str_replace ( '<', '&lt;', $matches['content']);
      $matches['content'] = str_replace ( '>', '&gt;', $matches['content']);
      $matches['content'] = str_replace ( '"', '&quot;', $matches['content']);
      $matches['content'] = str_replace ( '\'', '&#39;', $matches['content']);
      $procesed_content .=  $matches['opening']  . $matches['content'] .  $matches['closing'] ;
    }
    else
    {
      $procesed_content .= $piece;
    }
  }
  return ( $procesed_content );
}

add_filter('the_content', 'skinju_escape_pre', 10);

How to Change the Frequency Tweet Import Runs

One of the most frequent questions I get regarding the Tweet Import plugin, is whether the update, or import, frequency can be changed or, in other terms, if the plugin can be made to import tweets faster than the default 15 minutes minimum allowed interval.

My reply is usually that technically speaking it is possible but there are some considerations to take care of first.

First of all, it is important to understand that Tweet Import uses the built in WordPress scheduling engine which runs scheduled tasks at the end of page loads. What does that mean? It means that if nobody is browsing your website, the scheduled events will not run and therefore tweets will not be imported. Having said that, you need to make sure that you have enough traffic to your website for the scheduled events to run at the set intervals.

In principle, adding more intervals is easy and it requires updating both the frequency options and the caching duration of the WordPress internal RSS processor, SimplePie.

Adding frequency options to the plugin is easy. As a matter of fact, Tweet Import already adds two options “Once Every 15 Minutes” and “Four Times a Day” to the default WordPress built in schedule intervals like that:

if (!has_filter ('cron_schedules', 'skinju_tweetimport_add_schedule_intervals')) {add_filter('cron_schedules', 'skinju_tweetimport_add_schedule_intervals');}
if (!function_exists('skinju_tweetimport_add_schedule_intervals')):
function skinju_tweetimport_add_schedule_intervals()
{
  return array( 'everyquarter' => array('interval' => 900, 'display' => 'Once Every 15 Minutes'),
                '4timesaday' => array('interval' => 21600, 'display' => 'Four Times a Day'));
}
endif; //skinju_tweetimport_add_schedule_intervals

If you would like to add more frequencies, like let’s say “Every 5 Minutes”, you can either edit the plugin itself, or better yet, to still be able to update the plugin automatically and not lose the changes you made, add the following to your theme “functions.php” file:

// *** Requires Tweet Import Version 1.3 or newer ***
if (!has_filter ('cron_schedules', 'more_schedule_intervals')) {add_filter('cron_schedules', 'more_schedule_intervals', 100);}
if (!function_exists('more_schedule_intervals')):
function more_schedule_intervals()
{
 return array( 'every5minutes' => array('interval' => 300, 'display' => 'Once Every 5 Minutes'),
               'everyquarter' => array('interval' => 900, 'display' => 'Once Every 15 Minutes'),
               '4timesaday' => array('interval' => 21600, 'display' => 'Four Times a Day'));
}
endif; //more_schedule_intervals

In brief, every interval requires the following to be defined. An internal name, “every5minutes”, an interval, the frequency in seconds, and a display name, “Once Every 5 Minutes” in this case.

As for the caching duration, as of version 1.3, Tweet Import allows changing the caching duration using an external function without changing the plugin code itself. Why is it being cached to start with? Right?

Well, when I first designed the plugin, I thought that since the plugin is supposed to import tweets once every 15 minutes minimum, and in order to prevent unnecessary calls to Twitter API in case something went wrong, the plugin can use a cached version of the Twitter response. By default, the plugin will cache the response just little bit less than 15 minutes (880 seconds to be exact). After all, the plugin should never process the cached version of the response anyway. Today, I still hope this design choice is right :)

In order to change the caching duration, and suppose you would like to change it to 295 seconds, you should add the following to your “functions.php” file.

// *** Requires Tweet Import Version 1.3 or newer ***
if (!has_filter ('tweetimport_cache_duration', 'other_cache_duration')) {add_filter('tweetimport_cache_duration', 'other_cache_duration', 100);}
if (!function_exists('other_cache_duration')):
function other_cache_duration($current_duration)
{
 return (295);
}
endif; //other_cache_duration

This second addition will change the caching duration to 295 seconds, which is a little bit less than the 5 minutes update interval of 300 seconds.

How to know the user name of the logged out user with Remote Login

So you’re using the Remote Login plugin for elgg to display an elgg login form on another site of yours. Once this plugin is setup and enabled, it will detect remote login and will automatically bring the user back to the page where that user did login.

It is sometimes required to know who is the coming back user to provide more integration capabilities or personalize the user experience.

In order to get the user name at the external page, you can hack the logout event implemented in the Remote Login plugin to include the elgg user name as a parameter in the URL.

To do that, can simply edit the “logout.php” file located in the Remote Login plugin directory (“/mod/remotelogin/actions/logout.php”).

Change the following (near the top pf the file):

if (isset($_SESSION['remote_login_referer']) && $_SESSION['remote_login_referer'] != '')
  $redirect_after_logout = $_SESSION['remote_login_referer'];

and replace it with the following:

if (isset($_SESSION['remote_login_referer']) && $_SESSION['remote_login_referer'] != '')
{

  $redirect_after_logout = $_SESSION['remote_login_referer'];

  //Rename previous usernames... In case one is re-logging-in from the same page!
  $redirect_after_logout = preg_replace ('/rlelggusername/', 'rlprevelggusername', $redirect_after_logout);

  //Parameter name and value... The value should be saved here as the logout will clear the session
  $paramName = "rlelggusername";
  $paramValue = $_SESSION['username'];

  //Check what to use for the parameter '&' or '?'
  $separator = (strpos ($redirect_after_logout, "?")!==false) ? '&' : '?';

  //Is there a # in the URL... if so... insert right before it... otherwise insert parameter at the end
  $position = (strpos ($redirect_after_logout, "#")!==false) ? strpos($redirect_after_logout,"#") : strlen ($redirect_after_logout);

  //Insert Parameter
  $redirect_after_logout = substr_replace ($redirect_after_logout, "$separator$paramName=$paramValue", $position, 0);
}

This new code will add a parameter called “rlelggusername” to the URL. This parameter will hold the user name of the elgg user who did logout.

Allow access to the profile page with Log In Required

If you have Log In Required plugin for elgg running on your website, it might still be required to allow public access to member profile pages. To achieve this, it is required to edit the plugin itself. Don’t worry, the steps are pretty simple and straight forward.

To allow access to profile pages, all you need to do is change the following lines:

if (!in_array(strtolower(trim($current_url)), array_map('strtolower', $allow)))
  gatekeeper();

to

if (!in_array(strtolower(trim($current_url)), array_map('strtolower', $allow)) && get_context() !== 'profile')
  gatekeeper();

This change will allow access to the profile page showing the profile information only without the content of the widgets available for public viewing and guests.

If moreover you would like to allow the profile widgets to show content, use the following instead of the above code:

if (!in_array(strtolower(trim($current_url)), array_map('strtolower', $allow)) && get_context() !== 'profile' && get_context() !== 'widget')
  gatekeeper();