Lee Willis

February 26, 2013
by Lee
0 comments

Easily embed WordPress.org plugin details into your posts

I’m slowly working through tidying up information about my free plugins. Part of this meant that I wanted a way to easily include the latest information about my free plugins in the page as a summary. For plugins hosted on GitHub I’m using my GitHub oEmbed plugin, but I have quite a few plugins hosted solely over on WordPress.org.

So, I’ve also now published a plugin that will let you embed plugin summaries from WordPress.org into your posts and pages just by pasting in the URL.

Check it out here:

February 9, 2013
by Lee
0 comments

Simple embedding for non oEmbed services

I recently posted about my GitHub embed plugin for WordPress. The plugin performs a neat trick of hooking into WordPress’ oEmbed infrastructure to allow you just to paste in a URL and retrieve an embed for a service that doesn’t natively support oEmbed.

This post is just a quick walk through explaining the approach. In general the plugin:

  • Registers an oEmbed handler for the selected URLs (http://github.com/{something} in our example)
  • Registers an internal oEmbed handler for that URL
  • Handles the oEmbed call itself, retrieving the details it needs via the 3rd party’s API and then passing WordPress back an oEmbed response

Effectively you make your own site an oEmbed provider for the service you want to embed. Here’s the key bits of code:

First – we register an oEmbed handler, and point it to an internal URL:

function register_oembed_handler() {
    $oembed_url = home_url ();
    $key = get_key();
    $oembed_url = add_query_arg ( array ( 'github_oembed' => $key ), $oembed_url);
    wp_oembed_add_provider ( '#https?://github.com/.*#i', $oembed_url, true );
}
add_action ( 'init', 'register_oembed_handler' );

Note: get_key() just generates a site-specific key to stop other people using your oEmbed service.

Next, we tell WordPress to look out for an inbound oEmbed request:

function handle_oembed() {

    if ( ! isset ( $_GET['github_oembed'] ) ) {
        return;
    }
    // Check this request is valid
    if ( $_GET['github_oembed'] != $this->get_key() ) {
        header ( 'HTTP/1.0 403 Forbidden' );
	die ( 'Sad Octocat is sad.' );
    }

    // Check we have the required information
    $url = isset ( $_REQUEST['url'] ) ? $_REQUEST['url'] : null;
    $format = isset ( $_REQUEST['format'] ) ? $_REQUEST['format'] : null;

    // Call the 3rd party service, and create an oEmbed response here

}
add_action ( 'init', 'handle_oembed' );

All we need to do now, is retrieve the details we need using whatever API tools are available, then create an oEmbed response, e.g.

    $response = new stdClass();
    $response->type = 'rich';
    $response->width = '10';
    $response->height = '10';
    $response->version = '1.0';
    $response->title = $repo->description;
    $response->html = 'Your info here';
    
    header ( 'Content-Type: application/json' );
    echo json_encode ( $response );
    die();

And that’s it in theory, simple as pie. If you want to see working example, checkout out the github embed plugin on github:

WordPress Github "oEmbed" plugin
https://github.com/leewillis77/wp-github-oembed
19 forks.
69 stars.
3 open issues.

Recent commits:

February 9, 2013
by Lee
0 comments

Embed Github repo information in WordPress

WordPress offers an “oEmbed” service for a number of external services. If you’re not familiar with this, then it offers an easy way to embed external content into your posts and pages, without having to mess around finding embed code, pasting it in, and hoping the important bits don’t get stripped out.

Instead, WordPress’ oEmbed support allows you to simply paste in the URL to the page from your browser, and WordPress does all of the hard work contacting the provider and agreeing how they can embed it.

I wanted to use this to embed a summary of a GitHub repository, but unfortunately GitHub doesn’t support oEmbed – although they do have a fairly simple API that can be used to retrieve information about the repository.

I could have written a shortcode to interrogate the API, but I wondered if I could achieve an oembed style user experience instead. The result is the Github Embed plugin which is available from WordPress.org. The plugin allows you simply to paste in the URL to either a GitHub profile, or a repository, and have information embedded into your post automatically.

As you might expect, the plugin is also hosted on GitHub, and is embedded below …

WordPress Github "oEmbed" plugin
https://github.com/leewillis77/wp-github-oembed
19 forks.
69 stars.
3 open issues.

Recent commits:

It needs a bit of UI love, and pretty sure it can show some more useful information, but it’s usable now.

January 25, 2013
by Lee
0 comments

Adding UK counties to WooCommerce

I originally posted this to Github a while ago, but I had another enquiry about it just the other day, so thought it probably worth a quick post. WooCommerce by default ships with the United Kingdom as one entity – there’s no counties.

If you want to add counties (Particularly useful if you want to set different rates per county using something like my WooCommerce Pro Shipping extension) then it’s actually pretty straightforward.

I’ve posted a quick plugin to github that registers the UK counties – you could adapt this if you needed to split out the individual countries, or define your own regions.

Enjoy 🙂

January 13, 2013
by Lee
4 Comments

Adding body classes based on Drupal role / user ID

I’m writing this down here quickly because it strikes me that it may well come in handy to others. This little module snippet that will add classes to Drupal’s html (On the body element) to indicate the current user’s role, and user ID.

Before:

After:

Handy if for example you want to style things differently based on the user’s role. Just drop it into your module, and replace MODULE with your module name.

The code:

function MODULE_preprocess_html ( &$variables ) {

  global $user;

  foreach ( $user->roles as $role_id => $role ) {
    $variables['classes_array'][] = "role-id-".$role_id;
    $variables['classes_array'][] = "role-".strtolower(drupal_clean_css_identifier($role));
  }

  $variables['classes_array'][] = "user-uid-".$user->uid;

}

November 4, 2012
by Lee
20 Comments

Adding a dynamic select list to Contact Form 7

Contact Form 7 is a great, plugin that allows you to easily add contact forms to your WordPress site. Today, I wanted to make a small tweak to one of my contact forms so I could capture some better information.

Specifically, I often receive blog enquiries from people asking questions like “Can this plugin do XXX?” without any context. Since I have around 13 free plugins on WordPress.org, and around 14 sold through my own premium plugin site, and numerous others through other websites – it’s not always obvious which plugin people are asking about.

So – I did the obvious thing, and added a drop-down list and asked people to confirm which plugin they were contacting me about. The challenge was that rather than just hardcoding in the list of options, I wanted it to be dynamic, and generated in realtime.

I can see this being useful in lots of different situations so here’s my solution:

function ses_add_plugin_list_to_contact_form ( $tag, $unused ) {

    if ( $tag['name'] != 'plugin-list' )
        return $tag;

    $args = array ( 'post_type' => 'wpsc-product',
                    'numberposts' => 50,
                    'orderby' => 'title',
                    'order' => 'ASC' );
    $plugins = get_posts($args);

    if ( ! $plugins )
        return $tag;

    foreach ( $plugins as $plugin ) {
        $tag['raw_values'][] = $plugin->post_title;
        $tag['values'][] = $plugin->post_title;
        $tag['labels'][] = $plugin->post_title;
        $tag['pipes']->pipes[] = array ( 'before' => $plugin->post_title, 'after' => $plugin->post_title);
    }

    return $tag;
}
add_filter( 'wpcf7_form_tag', 'ses_add_plugin_list_to_contact_form', 10, 2);

That example picks up a select list called “plugin-list”, and adds to the options for every post retrieved by the call to get_posts. Here’s the setup of the default contact form:

You can see we’ve set the field name to “plugin-list” – so that our code above can make the changes to the right field, and we’ve provided a couple of fixed options – our dynamic ones will be added on after these.

Hopefully this comes in useful!

October 5, 2012
by Lee
0 comments

Social Checkout for WooCommerce

I’ve just released my latest WooCommerce extension. It’s a fairly simple – but beneficial plugin for any store. Simply put, the Social Checkout plugin allows you to encourage your users to share their purchases on their favourite social networks.

The plugin lists the products purchased, together with simply sharing links for the social networks you’ve configured. Everything happens post purchase – so you’re not making your purchase process more complicated, or making barriers to checkout.

The plugin already supports Facebook sharing of purchased items, Twitter posting, and pinning to Pinterest.

Check out the screenshots below of the setup options.

The plugin’s under active development, so any other features you think should go in there, or networks should be added – let me know in the comments!

April 13, 2012
by Lee
0 comments

Including a git repo as an SVN external

I have a couple of bits of code that I keep in a local SVN repo1

Some of that codes extend existing projects, and include external projects in subfolders. SVN has a great piece of functionality that allows you to embed other SVN repositories in sub-folders in a project, and have them automatically updated.

Unfortunately – while I’m using SVN locally, the code I’m including is hosted in GIT repositories.

Natch.

Fortunately, the Internet is a wonderful place, and after a woeful tweet, the ever-helpful @tarendai came up with the spectacularly useful observation:

Turns out that github implemented SVN access to their repos. So – you can include github2 projects into SVN repositories as SVN externals – hurrah!

To get it set up, it’s just the same as a standard SVN external, e.g.

$ svn propedit svn:externals .

and then give the folder you want it put in, and the github SVN url, e.g. to include the Campaign Monitor PHP API:

createsend-php https://github.com/campaignmonitor/createsend-php/

#win

1. Note: I’ll probably move to git shortly, tempted by the fact I use it at work, and by the awesomeness that is git-flow
2. Yes, I know that doesn’t solve it for the general git case, but seriously – how cool anyway 🙂

January 2, 2012
by Lee
3 Comments

Sorting the list of downloadable files in WP e-Commerce

Those of you who run stores with downloadable products may well have struggled with the following problem. If you try an attach an existing file to a product (Either because the same file is supplied with different products, or you have product variants that you need to set downloads against), then you may well have run into the following:

This list is a bit of a nightmare to find the file you want. It’s not sorted by anything obvious – not name, and not date-modified as far as I can tell. Finding a file is a bit of a scroll-and-hope affair. Much better to have a nice, ordered list like this:

This is an itch I’ve been wanting to scratch for a while now, and it turns out the code is pretty simple. Just drop the following in your theme’s functions.php file and revel in the glory of a nice, ordered file list.

function site_plugin_custom_sort_function ( $a, $b ) {
  return strtolower($a['display_filename']) > strtolower($b['display_filename']);
}

function site_plugin_sort_download_list ( $download_list ) {
  // Sort the multidimensional array
  usort($download_list, "site_plugin_custom_sort_function");
  return $download_list;
}
add_filter ( 'wpsc_downloadable_file_list', 'site_plugin_sort_download_list' );

November 9, 2011
by Lee
0 comments

Capital D dangit…

Just a short post I promise.

I’m pleased to announce that I’ll shortly be joining the fabulous team over at Hydrant. If you haven’t heard of them before, they’re a growing Drupal agency with big ideas. Can’t wait!

PS. Yes, I said Drupal 😉