About Brian Hogg

I’m an experienced freelance developer, currently focused on WordPress / PHP.  If you have a project you’d like to discuss, get in touch!

1-877-774-4767 | @brianhogg | E-mail

Removing the author from a WordPress feed

One client didn’t want the author’s e-mail address directly visible, and though it was removed from the theme it still appeared in the feed. Fortunately there’s a filter for that:

function my_remove_author_from_feed( $author ) {
    if ( is_feed() ) {
        $author = '';
    }
    return $author;
}

add_filter('the_author', 'my_remove_author_from_feed');

Wordcamp Toronto

I’ll be speaking at Wordcamp Toronto on November 3rd, about working with geolocation data in WordPress. Will post the slides on Slideshare. Hope to see you there!

How to access a FileValut 2 encrypted drive using Lion Recovery

After upgrading from 10.7.1 to 10.7.2 with the hard drive encrypted with FileVault 2, the system would not boot (displayed a circle with a line through it). After searching I found a thread that seemed to point to PGP still having some parts left around, even though I wasn’t using PGP Whole Disk Encryption (WDE) anymore thanks to FileValut 2.

Unfortunately I could not seem to access to the files because a) the system would not boot and b) the system was encrypted with FileValut 2. Calling Apple didn’t yield a solution except re-formatting the machine and losing all data, but stumbled upon a way to not have to do that:

1. Restart the machine and hold Cmd+R to restart in Recovery Mode
2. Under the Apple menu (top-left) select ‘Startup Disk…’
3. Click on your encrypted disk, then click ‘Unlock…’ (mine is labelled ‘Macintosh HD’)
4. Enter the password of a user who can normally start up the machine and click ‘Unlock’
5. Hit Cmd+Q or go to Startup Disk menu and choose Quit Startup Disk
6. Under the Utilities menu, choose Terminal
7. Type cd /Volumes/Macintosh\ HD (enter the name of your drive instead of Macintosh HD) and hit enter

Voila, now you have access to all of your files on the encrypted drive!

WordPress Plugin Repository not updating

When pushing a recent update to the Weever Apps plugin, the version number did not seem to be updating. Most documentation mentions updating the readme.txt file, but not the main plugin file version number also. Without updating both the new version will appear in the dropdown and as ‘development version,’ but not as the main downloadable file.

Moral of the story, update both or risk losing some hair!

More detail here. If you still have issues you should be able to e-mail plugins@wordpress.org for assistance (though certainly try the forums first).

WordPress AJAX response codes using status_header

As I’m relatively new to WordPress plugin development, I’m constantly searching around for the cleanest and most “WordPress way” to do things. One function that helps write cleaner AJAX call handlers is status_header(), to inform the caller when things go wrong:

add_action( 'wp_ajax_myajaxaction', 'my_ajax_handler' );

function my_ajax_handler() {
    if ( ! empty($_POST) and check_ajax_referer( 'mynonce', 'nonce' ) ) {

        if ( things_go_wrong ) {
            status_header( 500 );
            echo "some informative message or code";
        }

        // ...
    }
}

and in the javascript using success and failure functions rather than deconstructing response text:

var nonce = jQuery("input#nonce").val();

jQuery.ajax({
   type: "POST",
   url: ajaxurl,
   data: {
	   name: encodeURIComponent(tabName),
	   id: tabId,
	   mynonce: nonce,
	   action: 'myajaxaction'					  
         },
	 success: function(msg) {
               // Handle a successful call
	 },
	 error: function(v,msg) {
            // Handle the error, with the text in msg
         }
});

You could also output the headers manually, but this gives a nice clean way to do it.

jQuery Validator Plugin with a Custom Validation Function and Dependency Selectors

The jQuery validator plugin is a great way to add validation to forms in a clean way.  Error messages shown and hidden when they attempt to submit, and on the fly when they correct the input.

I recently added a new validation function to see if the field they entered was a valid twitter hashtag or username.  Though it could be improved (ie. looking for spaces), it does the job initially.  The problem was that the same field name was used for both depending on the value of another dropdown.

To handle enabling or disabling the validation function based on a certain condition, I initially thought you just pass in true or false, such as you do with required:

jQuery('#socialAdminForm').validate({
    rules: {
        component: { twitteruserrequired: true }
    }
    // ...

However the the 3rd parameter (which I’ve labelled, ‘isactive’) needs to be evaluated based on the condition, as the boolean value is passed in here. Here’s an example that determines whether or not to use the validation function based on the select element with id wx-select-social:

jQuery.validator.addMethod('twitteruserrequired', function(value, element, isactive) {
    return !isactive || (value.trim() != '@' && value.substr(0, 1) == '@');
}, "Please enter a valid value");

jQuery.validator.addMethod('twitterhashtagrequired', function(value, element, isactive) {
    return !isactive || (value.trim() != '#' && value.substr(0, 1) == '#');
}, "Please enter a valid value");

jQuery('#socialAdminForm').validate({
    rules: {
        component: { required: true },
        name: { required: true },
        "component_behaviour": { required: true, twitteruserrequired: function(element) {
                return (jQuery("select#wx-select-social").val() == 'twitteruser');
            }, twitterhashtagrequired: function(element) {
                return (jQuery("select#wx-select-social").val() == 'twitterhashtag');
            }
        } }
    }
    // ...

Without the third parameter of the function in addMethod being evaluated for true or false, the validation function will always execute.