Enabling Auto Updates on WordPress

If you’re hitting problems with having WordPress update itself (i.e. you’re seeing this error: “To perform the requested action, connection information is required.” ), chances are you need tofix file permissions and/or change the owner of them (likely from root).

Please feel free to cry in the comments below about this not being a secure solution, but it works.  In any case, if you’re using Ubuntu, you’ll need to make sure your permissions are right.  Once you’ve done this, change the owner of your public_html directory (and its children) by going to its parent directory and running the following command:

 chown -R www-data: public_html/

This should do it.  If you’re not using Ubuntu you might not have ‘www-data’ .. it might be something like ‘httpd’ instead.  Additionally, if your WordPress installation isn’t in the public_html directory, you’ll need to change the command accordingly.

Multidimensional Array Sorting in PHP (sorting arrays of arrays)

For today’s PHP coding trick, the function usort is your friend, though I found the PHP documentation wanting on this topic.

Basically, you have an array of arrays (multidimensional) and you want to sort the first array by the values in the secondary arrays.  In my case, I had an array of 3 value associative arrays (title, link, date).  I wanted to sort the primary array by the datetimes found in the 2nd array.  Turns out, this is incredibly easy using usort.

Just create a comparative function that will take two children arrays and compare two elements.  In my case, comparing articles (or more specifically, the dates):

function compare_times($article_one, $article_two) {
 if ( $article_one['Date'] == $article_two['Date'] )
 return 0;
 else if ( $article_one['Date'] < $article_two['Date'] )
 return 1;
 else
 return -1;
 }

Then… just call that by giving usort two parameters: the master array and the compare function name as a string:

usort($todays_stories[$section], 'compare_times');

(N.B. In my case above, I was using a three dimensional array, but only sorting the 2nd dimension ($section) by the times (more specifically the ‘Date’ key) in the third dimension.  The first dimension went unsorted/untouched.  This was inside of a foreach loop, so each $section was sorted independently from most recent article to oldest article).

Hope this helps — comment below if you have questions.