All posts by Sean

How to Add a WordPress theme via composer with a Private GitLab Repository

I have been exploring using Composer for managing themes and plugins for WordPress. For public themes and plugins, this could not be easier using WordPress Packagist. However, I had a hard time finding a straightforward explanation for how to integrate a theme I am developing on a private GitLab repository.

It turns out that the solution is very simple. In your GitLab theme/plugin:

{
    "name": "<username>/<package>",
    "type": "wordpress-theme|wordpress-plugin",
    "require": {
        "composer/installers": "~1.0"
    }
}

Adding composer/installers as a require library allows you to set the type attribute to wordpress-theme or wordpress-plugin.

When you are including your theme/plugin in a composer file at your WordPress root, include these configurations:

{
    "repositories": [
        {
            "type": "git",
            "url": "git@gitlab.com:<username>/<package>.git"
        }
    ],
    "require": {
        "<username>/<package>": "<version>"
    }
}

Important: For a private repository, you need to add the private key beforehand by loading it into an SSH config file or using ssh-add.

Windows Users: I recommend using VVV for testing WordPress. If, however, you want to stick with Windows and a WAMP server, you can download Git-SCM and follow these instructions to add SSH keys. I was able to set up an SSH key when running Git Bash (not Git CMD). You can also run plink.exe by following the instructions.

Installing PHPBrew with VVV

VVV is awesome. For all its very impressive features, there are some things it doesn’t do (yet). One of those is run PHP 5.6, or PHP 7 (or, while we’re at it, older versions like PHP 5.3). PHPBrew makes this a walk in the park. If, that is, you use the right configurations and install the correct dependencies. I had a hard time finding a good step-by-step guide detailing missing dependencies and configuration options needed for PHPBrew. I’ve put one here to save others (and myself in the future) some time. And for those curious, many of these steps are helpful when installing on any Ubuntu dev server, not just VVV or WordPress.

Without further ado:

A Walkthrough for Installing PHPBrew on VVV

1. Install Dependencies

This was one of the most annoying things to discover. Every time I tried to install PHPBrew, it failed with a missing dependency.

On VVV, you can install these dependencies with the following command:

sudo apt-get install libxml2-dev libbz2-dev libmcrypt-dev libxslt-dev

2. Download & Install PHPBrew

Install PHPBrew using their recommended fashion (at least, as of March 2016, when this is written):

curl -L -O https://github.com/phpbrew/phpbrew/raw/master/phpbrew
chmod +x phpbrew
sudo mv phpbrew /usr/local/bin/phpbrew

And, as Lance Cleveland mentions in the comments, you should also type in:

phpbrew init

When this is complete, you can review available versions with phpbrew known. Here you will see a list of PHP versions, new and old. For example, I spotted the latest available version on phpbrew (again, as of March 2016), and installed it with options that are required for WordPress and VVV’s server configuration (note: cli is not strictly required, but I installed it so I could use WP-CLI):

phpbrew install 5.6.19 +default +fpm +mysql +cli

You can add other options, like +curl, for example.

Then, switch. I received a message which gave me a command that didn’t work, so here is the one that did:

phpbrew switch 5.6.19

Or whichever version you are installing.

3. (Optional) Install extensions

If you’re looking for xdebug, a feature that comes standard on VVV but gets overwritten with a new version of PHP, you can add the extension with:

phpbrew ext install xdebug stable

You can install APC and Memcache similarly, if you need to test it.

If you’re looking for any additional configuration options, there are a good number, as well as some helpful hints. These are not WordPress specific, so rather than try to copy them all, take a look at PHPBrew‘s web page, which has a lot of documentation.

I hope this helps you as much as it would have helped me when beginning my PHPBrew adventure.

Addendum

For a non-VVV server install, I needed to include these dependencies for Ubuntu as well when I installed PHP 5.3:

sudo apt-get install libreadline-dev libicu-dev

Your Guide to VVV

VVV, or Varying Vagrant Vagrants, is a tool which will make your life as a WordPress developer infinitely easier. After I learned about it at WordCamp NYC‘s Contributor Day, I found myself drawing a blank as to how I survived WordPress development without it.

Here are a few of my favorite features of VVV:

  • Install once, add as many WordPress websites as you want.
  • Extensions are available. Josh Lee, a WordPress extraordinaire I met at the event, told me about vv. This allows you to create WordPress installations on-the-fly.
  • Basic installation includes Stable, Developer, and Trunk so you can easily test your themes on the newest version of WordPress
  • Uses vagrant-hostsupdater and creates easy to remember host names for your base installations. I used to do this on my local machine, but this is better.
  • Built on Ubuntu. I love my Mac, but BSD is not the same as Linux, and my server runs Linux. I can use it the same as I use vagrant by simply typing `vagrant ssh`, and I can make any modifications that might exist on my server.
  • Like Vagrant, it is FREE and can run on almost any machine. All you need is VirtualBox.

So, how can you get started? VVV has made it very easy. Simply follow the instructions on GitHub and you’re good to go!

Regular Expression to Change Text Domains

I found myself in a situation where I had to change my text domain on WordPress. I changed it from a single word to hyphen separated (example: foo to foo-bar). Like a dummy, my first call was to just do a Replace All. That didn’t work, because convention in WordPress is to put a namespace in front of functions, and my namespace was foo. As you know, hyphens do not belong in PHP functions.

If you ever find yourself in a similar situation, I wanted to share a RegEx trick that may help save you time, just as I hope it will save me some time when I find myself in this situation again.

(__\((?:[^'"]*)(?:\'|\")(?:[^']*)(?:\'|\"),(?:[^'"]*))(?:[^'"]*)(?:\'|\")<OLD NAMESPACE>((?:\'|\")(?:[^'"]*)\))

(Replace [YOUR OLD NAMESPACE] with the namespace you want to replace).

From there, it’s as simple as replacing your match. This regular expression makes it easy to do this by wrapping your replace with your first match and your second match.

In vim, you could simply type this:

:%s/\(__(\(?:[^'"]*\)\(?:\'|\"\)\(?:[^']*\)\(?:\'|\"\),\(?:[^'"]*\)\)\(?:[^'"]*\)\(?:\'|\"\)<YOUR OLD NAMESPACE>\(\(?:\'|\"\)\(?:[^'"]*\))\)/\<NEW NAMESPACE>\2

(Note that you want to replace the text in brackets with the text you want to find and replace).

WordCamp NYC Recap

First of all… First post: Woohoo! I couldn’t resist buying this domain name because I’m a sucker. Besides, if I didn’t do it, some other Sean would have. There certainly are enough of us.

Anyway, moving on. I just wrapped up a weekend at WordCamp NYC, and participated in my first contributor’s day. If you haven’t done this, I highly recommend it. My big takeaway? The folks who build WordPress are some of the most down-to-earth, approachable people I’ve ever met. It’s a really easy community to feel drawn to, and, it being the most popular CMS on earth, they certainly can use the help. You don’t even need to be technically savvy to get involved. If you’re a WordPress user, designer or developer, it can easily be the best $40 you ever spend.

I plan on going to WordCamp US in Philadelphia to keep my energy up.

I will also be blogging on the implementation of some of the awesome WordPress tools I learned about including VVV and WP-CLI. Stay tuned.