Creating GavinHayler.com and the Boldy/PHP Debacle

Jun 11, 2014   //   by Gavin Hayler   //   Blog  //  No Comments

I know this isn’t exactly a game dev post, but it’s what I’ve been doing lately so I may as well post it.

During the creation of this website I ran into a number of road blocks, mostly caused by the theme I chose or the particular¬†functionality that I wanted. I made this site inside WordPress using a free theme called “Boldy”. Unfortunately the last update to that theme happened some time ago and things have changed, and there doesn’t seem to be a lot of support from the theme’s creator which force me to delve into the world of PHP. Now I’ve never had even the smallest need or desire to use PHP so it was completely uncharted waters for me.

Other than some small tweaks that I could just do without any real knowledge of the language there were some things that I wanted that meant that I’d actually have to learn something about the ins and outs of PHP. Don’t get me wrong, I’m by no means an expert, but I have learnt some things so that I now have a passing understanding of what’s going on and it seems to be working so I’m going to count that as a victory. The first and most important thing I figured out is that PHP code is run on the server when a page request is received and is used to create a dynamic HTML page that is then served to the requester. After I wrapped my head around that, a lot of other things fell into place. I’m guessing this is the first thing you learn if you don’t try to cowboy code your way through it.

One of the features in the theme that was broken was the twitter feed, mostly because Twitter has changed it’s API (in not insignificant ways) twice since the last update of the theme completely breaking the original code. So happy¬†times fixing time. For anyone doing this for themselves, this code sits in the footer.php file. After much googling and too many sources to remember I got a solution that works. Firstly the authentication had to be rewritten using Twitter’s new API calls and a handy library I got from GitHub.

 

require_once("twitteroauth/twitteroauth/twitteroauth.php"); //Path to twitteroauth library
                    
echo "<script type='text/javascript' src='";
echo bloginfo('template_directory');
echo "/js/twitter.js'></script>";
                    
$twitteruser = get_option('boldy_twitter_user'); 
if(get_option('boldy_number_tweets')!="")
{
	$notweets = get_option('boldy_number_tweets');
}
else
{
	$notweets = "1";
} 
$consumerkey = "key";
$consumersecret = "secret";
$accesstoken = "token";
$accesstokensecret = "tokensecret";
 
function getConnectionWithAccessToken($cons_key, $cons_secret, $oauth_token, $oauth_token_secret) 
{
    $connection = new TwitterOAuth($cons_key, $cons_secret, $oauth_token, $oauth_token_secret);
    return $connection;
}
 
$connection = getConnectionWithAccessToken($consumerkey, $consumersecret, $accesstoken, $accesstokensecret);
 
$tweets = $connection->get("https://api.twitter.com/1.1/statuses/user_timeline.json?screen_name=".$twitteruser."&count=".$notweets);
$formattedTweet = json_encode($tweets);

 

Great now we’ve got the most recent tweet… oh. but Twitter has also introduced a request limit that just returns errors if too many requests are sent within an hour. So now we need to cache the results on the server when a successful request is made and if a request fails, to display the last cached twitter message.

 

//Check twitter response for errors.
if ( isset( $tweets->errors[0]->code )) 
{
    $file = $twitteruser."-tweets.txt";
    if (file_exists($file)) 
    {
        $fh = fopen($file, 'r') or die("can't open file");
        $formattedTweet = fread($fh, filesize($file));
        fclose($fh);
    }
    else
    {
        // If errors exist, print the first error for a simple notification.
        echo "Error encountered: ".$tweets->errors[0]->message." Response code:" .$tweets->errors[0]->code;
    }
 
} 
else 
{
    // No errors exist. Write tweets to json/txt file.
    $file = $twitteruser."-tweets.txt";
    $fh = fopen($file, 'w') or die("can't open file");
    fwrite($fh, json_encode($tweets));
    fclose($fh);
      
    if (file_exists($file)) 
    {
        //echo $file . " successfully written (" .round(filesize($file)/1024)."KB)";
    } 
    else 
    {
        echo "Error encountered. Tweet cache file could not be written.";
    }
}
 
echo '<script>';
echo 'twitterCallback2(';
    echo $formattedTweet;
echo ')</script>';

 

I even found a file which duplicates the formatting functionality in the blogger.js file that Twitter used to host and then threw away. So after all that I now have a working Twitter feed. Yay!

The other thing that wasn’t working was the contact form and mail sending. My web server didn’t allow unauthenticated mail to be sent out so i had to switch over to using SMTP, but my theme wasn’t written in a way that just adding a wordpress plugin would just work so I had to change that in sendmail.php. After much wailing and gnashing of teeth I finally found the solution to this one: change the mail() call to use wp_mail(). Yeah, that part wasn’t exactly rocket science, but sadly wp_mail isn’t defined in the sendmail.php file. This was where the real wailing and gnashing came in. Eventually the solution appeared in a random forum post: add these lines at the top of the file:

 define('WP_USE_THEMES', false);
 require_once('../../../wp-load.php');

 

Voila! Problem solved. I think it’s made my page loading slower, but my mailing works so I’m just going to have to put up with that. It’s probably loading a lot more stuff than it needs to to just have the mail functionality but I’m tired of working on this site now and the load time isn’t too bad, so yeah. Done.

Hopefully now I can get back to actually making games. Next thing for me is the melee weapons system…

Leave a comment

css.php