Posts Tagged ‘wordpress’

How to install Composer inside WordPress

Sunday, March 26th, 2017 | Programming

WordPress has an ancient and ugly codebase. When you are writing plugins, you often want to make your code less horrible and use Composer to bring in dependencies as normal. How do these things fit together?

The first thing I tried was to create plugin-specific dependencies list. Inside the plugin directory, I created a composer.json> file, then ran the install and uploaded it.

This approach works fines when you only use a dependency once and no other plugin uses the same dependency. However, when I wanted to re-use a library in another plugin I had a problem. If I included it twice it would crash.

Installing Composer globally

The solution I came to was to do a global install of Composer. I created a composer.json file in the root and listed all of the dependencies I needed for my plugins in there.

Then, in each plugin, I checked for the file and included it.

if (file_exists(sprintf('%s/vendor/autoload.php', ABSPATH))) {
    require_once sprintf('%s/vendor/autoload.php', ABSPATH);
    add_filter('the_content', 'simple_related_posts', 30);
} else {
    add_action('admin_notices', function() {
        echo '<div class="notice notice-warning is-dismissible"><p><em>Simple Related Posts</em> plugin requires <strong>Composer</strong>.</p></div>';
    });
}

Drawbacks to this approach

This means plugins are no longer self-contained. You have to install the plugin and add the dependencies to your central composer.json file. That is messy.

With the code above, it does not check if the _correct_ dependencies are there. This is something you could add in. However, given the problem above, it seems like it would require so much manual management anyway that I would know what was going on.

Optimise for performance

If you are using Composer on production, do not forget to optimise the autoloader before uploading your files.

composer dumpautoload -o

Conclusion

Personally, I would rather lose plugins being entirely self-contained and benefit from Composer libraries. This approach works well for me because they are purpose-written plugins for my blog.

However, this approach would not work well if you were publishing your plugins as most WordPress users would not know what was going on.

How to set up a web store with WooCommerce

Sunday, March 12th, 2017 | Tech

At the end of last year, I used Shopify to build an e-commerce store for Mountain Wallet. This worked well, but the costs quickly added up. The base Shopify subscription is only $30 but then there are themes and plugins that you will want to pay for on top of that. Last month, I ported it over to WordPress and WooCommerce, and I thought I would discuss my experience.

What is WooCommerce?

WooCommerce is a plugin for WordPress. Once installed, it provides you with a shop admin built into the WordPress admin, as well as front-end pages and a full checkout process. It installs like any other WordPress plugin making it very easy to get up-and-running.

Because of its popularity, many WordPress themes support WooCommerce out of the box. If not, you may need to play around until you find a suitable theme. I used a theme named Bento.

How easy is it to use?

As easy as Shopify. You add your products like you would add posts. It’s the same with adding product images: you use the media uploader. There was some messing around setting up shipping options and configuring payment integrations, but nothing too taxing.

Plugins I am using

As well as WooCommerce, I am using the following plugins:

  • Facebook Messenger Chat
  • Header and Footer
  • Instagram Feed
  • MailChimp
  • Social Media Flying Icons

There is nothing directly commerce related here. These allow me to add a chat link to the Facebook page, insert Google Tag Manager scripts, link the Instagram feed, add a mailing list sign-up form and some share icons.

I also have my standard array of security plugins installed.

SHOUTCLOUD WORDPRESS PLUGIN

Wednesday, July 1st, 2015 | Programming

HAVE YOU EVER WANTED TO CONVERT ALL YOUR BLOG CONTENT TO UPPERCASE? IF SO, I HAVE THE PLUGIN FOR YOU! I HAVE PUBLISHED A SHOUTCLOUD WORDPRESS PLUGIN THAT AUTOMATICALLY INTEGERGRATES YOUR BLOG WITH SHOUTCLOUD.

ADVANTAGES

  • AUTOMATICALLY CONVERTS ALL OF YOUR TITLES, CONTENT AND TAGS TO UPPERCASE
  • ZERO CONFIGURATION
  • ALL STRING MANIPULATION IS DONE IN THE CLOUD
  • INCREASES ANTIPCIPATION BY DRASTICALLY INCREASING LOAD TIME

DISADVANTAGES

  • NONE, EVERYTHING IS BETTER WHEN IT IS DONE IN THE CLOUD!

screenshot-1

Autop

Monday, June 22nd, 2015 | Programming

PHP has a function called nl2br that converts line breaks into <br> tags. This is really useful if you have content in a database and want to ensure the line breaks are in encoded in HTML without actually having to specify them in the content.

It is a bit of a clumsy tool though. It does not take into account that you might want to use paragraphs, or that you do not want br tags adding inside block elements such as pre or hidden elements such as script and style.

The programmers over at WordPress have done a great job of coming up with their own version that resolves all of these issues and many of us already benefit from it on our sites. Matt Mullenweg has written about it.

However, it is buried in the WordPress codebase, so if you want to use it in a non-Wordpress project, you have to mess about pulling it out of the codebase and inserting it into your own. To save myself from having to do this, I’ve created a project on Github that moves it to a standalone library. You can then drop this into your project using Composer and use away.

Using WordPress on real websites

Wednesday, May 21st, 2014 | Thoughts

WordPress is a super piece of software. It has made it easier and quicker to get a website up and running than any other content management system out there. It has a great range of plugins and themes, and is reasonably easy to add your own too.

That said, there are a couple of issues that I think make it a questionable choice for using on an important website.

It works fine if you deploy it to one location and just build everything there. However, often I will have a development version, a staging version and a production version. This allows me to pass a website through each stage while making sure it is working.

With WordPress, this is difficult.

You need to put the site URL into two different places in the config. This means that you have to update these values every time you deploy content to another server. So for example a typical use case would be to copy all your production data over to staging so you can perform major testing, and then potentially copy it back again. Or you want to develop the site locally and then push everything up to your server.

Unfortunately WordPress uses these URLs for the login process, so you cannot just log in and change them in the admin panel because you will not be able to get in. You have to do it via a script or by editing the database directly.

Secondly, it uses absolute URLs everywhere. Every time you upload a piece of media, or a post, it will store a URL for it in the database with the hostname included in it. When you insert the media into the post, it puts a absolute URL and SRC in there too.

This means that you have to do two things. Firstly, take our all the hostnames from the SRC and URL HTML content that WordPress generates every time it inserts a piece of media for you.

You also have to update the database to change all the URLs in there too. There does not seem to be any support to do this at all. I had to write a little plugin to change these. Arguably, you could use this plugin as well.

I am not sure why it does this. There does not seem to be any compelling argument to use absolute URLs. I had a look around for one, and the closest article was by Joost de Valk, none of which I think really stand up (certainly not in comparison to “is your site probably tested?”).

As pointed out here, if you really need to use absolute URLs, you could implement a short code to allow it to remain dynamic.

WordPress is a super piece of software, and indeed runs my blog. But I think it has it’s place in a certain set of use-cases and large production systems do not seem to be one of them.

Comment notifications

Sunday, January 11th, 2009 | News

I’ve finally got round to adding email notifications to further comments being posted on my blog entries. So if you comment on one of the entries you can now opt to be notified by email if someone posts a follow-up comment on that same entry.