If you’ve tried to run a javascript function within WordPress you may have run into this issue. You grab the script, something that starts with a ‘script’ statement and then a shorthand function like:

$(function(){
...
});

and you drop it into your plugin or template file and… nothing. Or more correctly, JavaScript errors that the ‘$’ is not recognized. Very frustrating.

Here’s what’s going on.

No Conflicts, man

This is a syntax issue, the jQuery library included with WordPress loads in “no conflict” mode. This is to prevent compatibility problems with other javascript libraries that WordPress can load. In “no-confict” mode, the $ shortcut is not available and the longer jQuery is used, i.e.

jQuery(document).ready(function ($) {

So, replace the

$(function(){

line with

jQuery(document).ready(function ($) {

and you’ll be running your JavaScript and on to the next issue!

Thanks to the RedEyedMonster for this description on stackoverflow.

Note: I blame Perl programmers for all errors resulting from shortcut functions and all problems resulting from shortcuts of any kind.

Here’s how to save a click for your readers by automatically redirecting to the post if only one result is found.

Just paste the following into your functions.php:

add_action('template_redirect', 'redirect_single_post');
function redirect_single_post() {
    if (is_search()) {
        global $wp_query;
        if ($wp_query->post_count == 1) {
            wp_redirect( get_permalink( $wp_query->posts['0']->ID ) );
        }
    }
}

Hat tip to WPSearchEngine

Backup Gmail Using Getmail

Getmail is an open source Python program that can backup your Gmail account. Getmail will work on any *nix system, including OS X, and even Windows as long as you have Cygwin installed.

 

Installing Getmail

Ubuntu users have it easy, since Getmail is in the Ubuntu repositories.

For other operating systems, you can either add the repository to aptitude or you can compile from the source like so:

Download Getmail, then open a terminal windows and type:

tar xzvf getmail*.tar.gz

cd [the directory it was unpacked into]

sudo python setup.py install

For Cygwin, you will probably first have to “rebase” your Cygwin installation. Briefly, that means closing Cygwin, opening cmd.exe and using it to run “dash” from C:\cygwin\bin, then typing “/bin/rebaseall” at the prompt it gives you. More on that here.

Setting Up Getmail

Now you just need a configuration file to tell Getmail how to connect to Gmail. There are a ton of sample configuration files on the Getmail site, but here’s one that works well with Gmail. Create a file at ~/.getmail/getmail.gmail and put the following text in it:

 

   [retriever]
   type = SimplePOP3SSLRetriever
   server = pop.gmail.com
   username = yourname@gmail.com
   password = yourpassword
   [destination]
   type = Mboxrd
   path = ~/gmail-archive/gmail-backup.mbox
   [options]
   verbose = 2
   message_log = ~/.getmail/gmail.log

 

Edit the yourname@gmail.com and yourpassword in the file to your specific values, of course. Note: this doesn’t have to be an @gmail.com address. If you are using Google for mail on a custom domain, just fill it in here.

Getmail won’t create the mbox file, so before we get started, create a new empty file called gmail-archive.mbox in the gmail-archive folder of your home directory (or where ever you choose to store it). From the command line this will do the trick: touch ~/gmail-archive/gmail-backup.mbox

 

Running Getmail

You have everything set up, so it’s time to actually make the backup. Fire up a terminal window and enter the following line:

getmail -r ~/.getmail/getmail.gmail

You should see a long string of messages begin printing out as Getmail starts grabbing the contents of your Gmail account. If the script stops, fear not — Google has some limits on how many messages can be retrieved at one time. Just run the above command again the Getmail will pick up where it left off. SeeGetmail’s documentation for more about this “cutoff” issue.

Now you have an mbox-formatted local backup of your Gmail account and you can access it with any e-mail client that understands mbox files, which is pretty much everything except Microsoft Outlook.

To automate this process, just create a shell script and attach it to a cron job that runs once a day or however often you feel is necessary.

On a vanilla BuddyPress install, the members page shows the Activity tab. For many communities the is great, but for the team site we’re building for booktrope.com the activity is not the main thing right now. I wanted to have the profile page show up as active when users go to the a member’s page. If you’re a coder and know your way around WordPress and BuddyPress the three steps can become one:

<?php

// force profile to show first on members page.

if ( ! defined( 'BP_DEFAULT_COMPONENT' ) ) define( 'BP_DEFAULT_COMPONENT', 'profile' );

BOOM! You know where to put that code and you’re off to the races.

If not, it’s just 2 more steps:

1. Create a bp-custom.php file in your plugins folder /wp-content/plugins and drop the skeleton code there.

2. Add the lines above to the file.

3. Save the file and reload a member’s page.

You should see the Profile tab show up as active from now on!

I was asked last week how best to develop a large redesign and update a WordPress site without destabilizing the live site. John Hawkins had described a great setup at Las Vegas WordCamp which I had adopted.

I added a few additional elements via a recommendation from John James Jacoby after seeing his setup at BuddyCamp in Vancouver, and my local dev setup was in great shape. I had the answer.

Below are the basic steps

  1. Download and install Desktop Server.
  2. Set up a .dev site locally.
  3. No Production Site? Go for it!
  4. On Production: MySQL Dump of your production database.
  5. On Production: Copy wp-content folder to your local (Local site is in the Documents/Websites folder with the name you gave the site).
  6. Delete Local MySQL tables ( http://127.0.0.1/phpmyandy.php )
  7. Import the production data.
  8.  Get a copy of searchreplacedb2.php (from here) and copy it into the root folder of your dev site.
  9. run devsite.dev/searchreplacedb2.php and use it to replace all occurrences of your production site name to the .dev site name. I.e., change mysite.com to mysite.dev via the script.
  10. Go for it! You may now work on your local box.
  11. I add one more element that allows me to have full syntax highlighting and reference, code tips, and trace running code via the NetBeans IDE, so I do the following: Download NetBeans for PHP and install it.
  12. Set up a project, set a break point on the code you’re changing and Press ‘Play’ on the IDE. You’ll be able to reference each line of code as it executes and see the value of all variables.

Full details on getting started in NetBeans will need to be a topic for another day.

The exquisite Timeline Verite .js framework has arrived as a plugin for WordPress. There is a plugin (find it here) but it doesn’t pull from my WordPress posts or RSS feed as I might have expected. The plugin expects a Google Drive Spreadsheet or JSON object to feed the ‘line. No need to despair, a quick and free hack will get your site content feeding the timeline automatically. Here’s how to set up Timeline with a WordPress blog to auto generate the ‘line.

1. Install the plugin. The plugin page is here.

2. Create a Google Spreadsheet from the timeline template, here.

3. Follow the instruction to publish the spreadsheet to the web and use the link as you build the shortcode. Keep the sample data in place for a few minutes so you can  test the timeline and have something to display.

4. Create a new Post in your blog and insert the timeline by clicking the ‘T’ above the post edit area. Save it.

5. Test the timeline page you’ve just created. (Note: if the page doesn’t display, you can add an iframe from the generator instead of using the shortcode.)

6. Now, here’s the trick. Sign up for If This Then That and activate a WordPress and a Google Drive channel.

7. Create a recipe to watch for WordPress posts and add a line to the Google spreadsheet based on this one I built for Booktrope.com.
(Note the recipe watches for “any new post” on the site and takes the Date, Title, Content, and 1st Image and adds it to the Google Drive Spreadsheet specified in the recipe.)

8. Finally, create a new post in your blog. Be sure to insert an image IN THE POST as the IFTTT.com WordPress channel doesn’t pick up the featured image, it looks for the 1st imbedded image in the body of the post.

9. Now, just wait about 10 minutes and IFTTT.com should kick in and append your a new line to the Google Spreadsheet which Timeline will see and you’ll have a new datapoint on your timeline.

10. Now you can go back and delete the sample data and replace it with your previous posts to fill out the timeline.

Once you get the historical data entered into the spreadsheet, you’ll be set. From then on, IFTTT.com and our sweet new recipe will add to your timeline automatically as you post.