Turn That Snippet Into A Plugin

Tell me what you think

This post is sort of a recap of a talk I gave a couple years ago.

Whenever you get a WordPress code snippet, where do you put it? It’s not uncommon all those bits of code to end up cluttering up the functions.php file of your theme, whether out of habit or suggested by tutorials.

But wouldn’t it be better if all those non-theme snippets were plugins instead? Yes!

As plugins, you can switch/upgrade themes without losing that functionality and toggle the funtionality without editing the theme.

And great news! It’s really easy to turn a snippet into a plugin.

The Snippet

Let’s say this is the snippet we’re working with:

add_filter( 'robots_txt', 'prefix_robots_txt' );
function prefix_robots_txt( $robots ) {
    $robots .= "Disallow: /assets/\n";
    return $robots;
}

This snippet just appends a line to the virtual robots.txt file – but that’s not really important for this post

Give the Snippet Its Own File

There are 2 options for ‘pluginifying’ the snippet, but they both have the same first step, get that plugin into its own php file.

First, we’ll take that snippet and put it into a php file, say robotstxt-additions.php. Don’t forget the opening <?php tag.

<?php

add_filter(...

Option #1: The Standard Plugin

For a standard plugin, we need to add a plugin header. Plugin headers are just php comments that WordPress reads for information about the plugin. There are several headers, but the required one is Plugin Name

<?php
// Plugin Name: Robots.txt Additions

add_filter(...

Then, move robotstxt-additions.php to the plugins folder of your WordPress install. By default that would be wp-content/plugins/.

The plugin file can either be “loose” in the main plugins folder, or in a subfolder. If the plugin has or will have style or script assets, you’ll want to put it in a subfolder.

Easy
wp-content/plugins/robotstxt-additions.php
Better
wp-content/plugins/robotstxt-additions/robotstxt-additions.php
It’s not required for the folder to match the plugin basename, but it is common convention.

Now you’ll be able to see the plugin listed on the Plugins screen of your WordPress install and activate it.

plugin

Option #2: The “Must Use” Plugin

Must-use (mu) plugins are always on, which can be really handy. But there’s also no de/activation switch in the admin.

Since we already have the snippet in a file, all we need to do is put it in the mu-plugins folder. This folder sits right next to the regular plugins folder, but is not included in a default install, so you may have to create it.

wp-content/mu-plugins/robotstxt-additions.php

It’s important for mu-plugins that they not be in any sub folders. For example
wp-content/mu-plugins/subfolder/robotstxt-additions.php
will not work.

You can see mu-plugins on the admin’s plugin screen.

mu-plugin1

Although plugin headers are not required for mu-plugins, if present they will provide additional information in the admin. In this case the Plugin Name header will be used instead of just the filename.

mu-plugin2

And there you have it.

Things You Should Know About the Command Line

1 response

The command line is really powerful and there are a quite a few things made easier by using it.

When I started learning things, I did a lot of copy-and-paste. It got the job done (usually…), but I rarely understood what I was doing or why it worked. So I want to outline a handful of basic commands, and some tips and tricks.
(more…)

Personal Project Sprint

3 responses

Here’s the deal — I’m really good at making lists… but in quantity not quality. I can make a dozen or more to-do lists between assorted notebooks, sticky notes, email, Evernote, Trello, and who-knows-what-else. (I’ve created a super simple list tool that I’m hoping will help combat that problem)

But I’m not always very good at following through with the tasks. Lists from 6 months ago might be the same as a list I jotted this morning. That’s right, I still haven’t cleaned up my worktable, or fixed that bug in that code…

Some I’m issuing this challenge to myself:
get as much done on my projects and to-dos as possible during April.
(more…)

Nginx, robots.txt, and Copy-Pasta

Tell me what you think

Did you know that if you don’t have an actual robots.txt file, WordPress will create a virtual one for you? For example, I have not created a robots file for trepmal.com, yet you can see one at https://trepmal.com/robots.txt.

So either you have created your own file, or you’re relying on the virtual one. Unless you’ve explicitly disabled WordPress virtual robots.txt file, you’ll have something at yoursite.com/robots.txt

However, if you (1) use nginx, and if you (2) followed certain popular guidelines* for configuring your site, and if you (2) are relying on the virtual file, you might discover that you get a 404 if you try to view your robots file.

(more…)

WP-CLI in the Real World

4 responses

I’m looking to spread the word about WP-CLI. One way I’ve decided to do this is to submit a WordCamp talk proposal on the subject.

Of course I can talk about how I’ve benefitted and about my favorite things, but I’d really like to make sure my talk is well-rounded and takes into account real world usage. Basically, I would love to hear from you about your experiences with WP-CLI.

  • Biggest hurdle to overcome?
  • Favorite command?
  • Most irritating aspect?
  • That feature you’re looking forward to using?
  • When did WP-CLI save you hours on a project?
  • Did it teach you something?
  • something else?

Whether there’s something you want to learn or a tip to want to pass on, let me know in the comments!

Automating Backups to Amazon S3

2 responses

My Setup & Prerequisites

I’ve got a LEMP stack, that is Linux (ubuntu), Nginx, MySQL, and PHP. It’s pretty common, but this should also work just fine for other LAMP stacks.

I want to backup some databases as well as web files, including some non-WordPress stuff.

There are tools out there like BackupBuddy that might be perfectly sufficient for your needs, especially if you want access to support forums and such.

But I like to dig into server-y stuff. Doing it this way will require a few things

  • ssh access to your server
  • sudo permissions (usually…)
  • amazon s3 account
  • some basic familiarity with the command line

(more…)

Getting Started with WP-CLI

1 response

By getting started, I mean really just getting started. I’m just going to show you how to get WP-CLI running. I think once you do, you’ll take off on your own.

If your local development environment is on VVV, then stop reading, you’ve already got WP-CLI installed!

Here’s a summary of what we’re going to do:

  1. Download the application
  2. Make it executable from the command line
  3. Move and rename it
  4. In case of XAMPP (or MAMP), add its binary directory to PATH (need to make sure XAMPP’s php is used, and not your machine’s)

Yes, the instructions for installing WP-CLI can be found on its site, but sometimes I just like to reiterate and expand.
(more…)

The publish_post hook

Tell me what you think

The publish_post hook is one I’ve seen a lot in tutorials and such. Even has its own page in the codex.

But surprise! That hook doesn’t actually exist, at least not if you search for do_action\(\s?.publish_post.

When I was learning to dig into core, this baffled me. What was this publish_post sorcery? Why wouldn’t it work on my custom post types???

Here’s a secret: The actual hook in core looks like this "{$new_status}_{$post->post_type}". It’s part of a small function in wp-includes/post.php (line 3321 at the time of writing), and there’s another equally useful variable hook right before it.

function wp_transition_post_status($new_status, $old_status, $post) {
	do_action('transition_post_status', $new_status, $old_status, $post);
	do_action("{$old_status}_to_{$new_status}", $post);
	do_action("{$new_status}_{$post->post_type}", $post->ID, $post);
}

There are quite a few of these variable hooks in core, I encourage you to go look at them, here’s a list I scraped together:

(more…)

0
0

Created by Gabriele Cirulli. Based on 1024 by Veewo Studio and conceptually similar to Threes by Asher Vollmer.