Lee Willis

May 11, 2013
by Lee
0 comments

Adding SKU to email notifications in WP e-Commerce

Adding the SKU (Or other information) to order emails in WP e-Commerce has been an often-requested feature. Up until recently it meant editing plugin files to apply the changes, and remembering to re-apply them on every update.

Thanks to the great work that’s been going on in WP e-Commerce core – there are now a bunch of useful hooks and filters that allow the content to be changed by filters rather than by editing the plugin direct.

So – if you want to add the SKU to your order emails you can just install and enable the plugin below to add it for you.

Say What?

March 10, 2013
by Lee
0 comments

How to change text in a WordPress plugin

One of the great things about using an out-of-the-box solution like WordPress is that you can get something up and running pretty quickly. That’s been one of WordPress’ strengths over the years. However, if you’re building sites for other people then sometimes parts of the generic-ness seep through and detract from the overall feel of the solution. Specifically – terminology that is great in a generic solution isn’t always helpful in a custom, or specific solution.

None of this is a criticism of WordPress, but as you start building larger, or more complex sites you’ll probably want to start smoothing off some of these rough edges, and make sure the language of the site (frontend or admin side) makes sense in the specific context of the site you’re working on.

That’s something I come across frequently in my day job as a Drupal developer. Fortunately Drupal has the excellent String Overrides module. This lets you specify the current string, and a replacement, and will change the text whenever that string is used, with the caveat that the original string has to be passed through Drupal’s translation function t().

When someone recently asked me how to change some text in one of my own WordPress plugins – my first suggestion was this exact same approach. After all, all of my Premium plugins use translatable strings, and suggesting to someone that they should create a translation file just to change one or two strings for their needs has always seemed a bit excessive. So – I had a hunt around the WordPress.org repo for something similar, but couldn’t really find anything that did the job.

Figuring it’d be something fairly simple to achieve I set out to knock up a plugin that did the job. So next time you need to quickly change a string in WordPress, or a plugin you’re using – don’t hack it in the plugin – or in WordPress core, leaving your client unable to upgrade, give “Say What?” a go:

The strings we want to change

The strings we want to change

The say what page - setting up our string replacements

The say what page – setting up our string replacements

The text - automatically replaced for us

The text – automatically replaced for us

 

Say what?
by Lee Willis

An easy-to-use plugin that allows you to alter strings on your site without editing WordPress core, or plugin code. Simply enter the current string, and what you want to replace it with and the plugin will automatically do the rest!

The plugin’s available for forking and contribution over on GitHub

Stats:

  • Current version: 0.9.1
  • Rating: 100 (2 ratings)
  • Downloaded 105 times

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:

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 …

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
0 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
1 Comment

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 :)

Proudly powered by WordPress | All content (c) 2013 Lee Willis

Top