Use Composer in Your WordPress Plugin or Theme

wordpress-composer

Simple Tutorial Showing How To Use Composer in Your WordPress Plugin or Theme

I love Composer. It just makes including libraries or scripts in your app incredibly easy. So easy that it’s stupid not to use it (in many, if not most cases).

The number of libraries/scripts available on Packagist is astounding, all of which can be included in your plugin with Composer. Packagist is the main Composer repository. It basically aggregates all types of PHP packages that can be installed via Composer.

I’d never used Composer with a proprietary WordPress plugin before. The plugin is for a client so it’ll never be available to the public.

Here’s the steps I took to make this WordPress plugin compatible with Composer so that I can easily bring in third-party libraries.

We’ll be using mailgun-php throughout this example, as the plugin that inspired this post uses Mailgun to send all sorts of emails.

1. First, install composer on your server.

I install Composer globally, like so:

2. Add Mailgun as a dependency.

3. Check your composer.json file.

We’re including Mailgun and guzzle from Packagist. Your composer.json file should look similar to the example below.

4. Tell composer to install Mailgun.

5. Autoload Our Mailgun Classes in Our Plugin.

The following should go in your plugin-name.php file, before any other PHP code.

You can now use Mailgun in your WordPress plugin or theme, some basic examples of using Mailgun can be found on GitHub and in their official documentation.

Fix StanleyWP WordPress Theme Portfolio Grid

wp-stanley

Fix display of portfolio grid rows

Back in September of 2014 I wrote about using the StanleyWP WordPress theme for a portfolio site. After I added some projects, I noticed the grid on the Portfolio page template wasn’t displaying rows correctly. I even noted it in my original post, towards the end.

I’ve had a few people contact me about how to fix the StanleyWP portfolio grid issue, and earlier today Arun left a comment asking how to fix the grid issue.

You need to be using a child theme for this, it’s just good practice. If you don’t know how to create a child theme, read my post on creating a child theme. It’s really easy to do, but may require you to reset your menu or some widgets after changing to the child theme.

Anyway, Arun confirmed that this gist fixed the problem for him:

Just save that code as template-portfolio.php and put it in your child theme directory. Your portfolio should now show three projects per row. No CSS or anything else needs to be modified, just that one page template.

Let me know if you have any issues or questions.

Using Gmail SMTP Servers to Send Email From WordPress on DigitalOcean

wpdo

Use Gmail SMTP Servers for Sending Emails from WordPress

After quite a bit of back and forth between sendmail, postfix, and exim, I’ve settled on using msmtp for sending emails from my servers/droplets at DigitalOcean (affiliate link).

MSMTP is very lightweight and has the ability to send emails via an existing SMTP server, like Gmail’s or Yahoo’s.

To get it working, there’s a few tricks. I’ve pieced this together from this post and this post. And when on DigitalOcean, there’s an IPv6 issue that causes major delays in sending the email, which there’s a fix for at the end of this post.

1. Install msmtp

2. Configure msmtp to use Gmail

Open up /etc/msmtprc as root: sudo nano /etc/msmtprc, and add the following, removing whatever else is there:

You’ll want to replace the user directive with a valid Gmail email address, a Gmail account or a Google Apps email address will work, too. Don’t forget to change enter-password-here! to the actual password for the Gmail account your using.

Save /etc/msmtprc.

3. Remove Sendmail

Run this:

4. Setup Some Aliases

Lots of software on Linux systems uses the sendmail command. Instead, we’re using msmtp, so we’re essentially invoking msmtp when the sendmail command is run.

5. Tell PHP About msmtp

First, locate your php.ini file that’s being used by Apache. It’s typically in /etc/php5/apache2/php.ini. If that’s not it, use PHP’s phpinfo() function to find the location of your php.ini file.

Find sendmail_path = in php.ini and replace it with this:

Now you should be able to send mail using PHP’s mail() function, which will use the Gmail SMTP server to send emails. Add this to a PHP file and access it through your browser to see if it works:

6. Disable IPv6 If You Experience Slowness

Open up /etc/gai.conf like so:

Now, look for a line that looks like this: #precedence ::ffff:0:0/96 100. Uncomment that line (remove the #) and save /etc/gai.conf. An explanation of why this helps can be found in this comment at the DigitalOcean article.

All Done

That should be it. If you run into any issues please do leave a comment, I’ll do my best to help you out. I may have missed a part, so no guarantees this will work for you. It does however work wonderfully on a DigitalOcean droplet that’s running Ubuntu 14.04 with a pretty standard LAMP stack.

You should now be able to send email from WordPress on DigitalOcean.

Simple Server Monitoring with Ping.gg

ping.gg

It really is the world’s most simple server monitoring service

Best of all, Ping.gg is currently free! Ping.gg will ping your server constantly, with an interval of 10 seconds.

Victor, the ping.gg creator, will be releasing all the Go code on GitHub eventually, but will keep the UI/PHP pieces to himself. It sounds like HTTP response checking is also in the works:

There is a ping daemon (Go app) that is listening for a couple of redis pub/sub channels for hosts to start and stop pinging. Each host is handled by a different goroutine. When something goes up or down, it publishes the host in another 2 redis pub/sub channels.

This is what I’ll release as open source, before I do I’d like to refactor it so it’s not tightly couple with redis, but rather have an interface there, so it’s easy to change the redis pub/sub interface with, for example, HTTP calls.

Monitor a Site with Ping.gg

To monitor a site (my.example.com), issue this command:

I’ll let Victor explain how it works:

After you provide a hostname or IP and your email address, you’ll be sent an email with 3 generated URLs that you can to click to start, stop and delete your tracking. Every time you server goes down or back online you will receive a notification, which will also include the control URLs. BTW, check your spam folder… you know the drill.

Every time my.example.com is unreachable, you’ll receive an email at [email protected]. An example email is below.
ping.gg-down-email

That’s It

Issuing that curl command is all you need to do. You’ll receive an email after adding a site to monitor asking you to active the monitoring. There’s also a link in that email so you can stop or pause monitoring of a site if you wish.

Ping.gg allows 10 sites to be monitored per email address. Ping.gg considers your server/site to be down when it fails to answer 6 pings in a row.

Hoping that Victor builds this into a full fledged service with account dashboards and all, just because it’s sooo simple. The Terms of Service possibly indicate that a professional service may be available at some point:

As previously stated, this is not a professional service (not now at least) so by using this service you agree to the following:

  1. You use this service at your own risk.
  2. There is no warranty that the service will work properly or at all.
  3. Your alerts might be terminated without notice.
  4. The service can stop operating anytime without notice.

Go ahead and give Ping.gg a try, it’s been very reliable for me and I’ve had no issues with it. Keep the Terms of Service in mind, however.