Bundling plugins with WordPress themes.

First off, many people will disagree with including Plugins with your themes. Yes and generally rightly so:

  1. Automatic Updates: If the plugin is freely available from WordPress.org already, then so are automatic updates to the plugin. The original author can add features, fix bugs, and deploy them quickly. Your clients and customers then benefit from on-going development. If you were to package them in your theme, though, you are essentially freezing the code at that point in time – any further updates, bug fixes, etc. would have to come from you. This means you’ll need to continuously release theme updates whenever the included plugins update.
  2. Core: When WordPress updates, some plugins will break. This is because the original authors didn’t take the time to remove deprecated functionality or test with new versions of WP. Do you really want to commit to maintaining your theme + someone else’s code?
  3. Interoperability: As the great mfields once said, “If you’re building a bathroom and you change the wallpaper, the toilet shouldn’t disappear, too.” Users should be able to swap out themes whenever they want without losing their content, their custom data, or the additional functionality they have on their site. Remember, themes are meant for presentation, plugins are meant for functionality.
  4. Bad Practice: Whether the theme updates or not, whether the plugin updates or not … WordPress will eventually update. Limiting your client to a single version is, frankly, insulting and bad business. Instead of hard-coding a drop-in plugin, just make your theme play nice with provided hooks and encourage users to install the other system. If you’re using WordPress hooks (actions and filters) rather than direct function calls, you aren’t risking much in terms of stability. If a hook changes, the feature is just disabled as if the plugin weren’t installed.

Though there may be specific cases where including a plugin in our themes is needed. For example, a recent case where I was building a content platform for a network of sites that would use the same basic theme across each site. I needed a way to bundle these plugins with a theme. The plugins were the fantastic advanced custom fields using the options page, the main plugin and some addons such as the repeater field.

I bundled all the plugins within my theme using the TGM Plugin activation library. With this plugin we create a hook and use php require_once function to call our plugins.

admin-notice

Sources: Adding plugins to WordPress themesAre drop-in plugins a product of design, TGM Plugin Activation, Advanced Custom Fields

One thing you didn’t know about WordPress images.

By default in WordPress when you server up a .JPEG file, WordPress optimizes the image to save bandwidth and space. 90% of the time, no one will realize this as the WordPress function behind image optimization does a fantastic job at keeping it’s quality the same, unless you are using very large high quality photo .jpeg files.

By default WordPress uses a default quality of 90%, if you wish to revert the image quality throw the following function in your functions.php file:

add_filter( 'jpeg_quality', 'smashing_jpeg_quality' );
function smashing_jpeg_quality() {
return 100;
}

Interacting with WP_Query, listing more posts from the current category.

For a new project of mine I was creating a single.php for the blog side of the website. The website had a few specific categories for the blog, these being News, Races and Events.

Whenever you were reading a blog post under one of these categories, the sidebar should show readers more posts from that specific category. May it be News, Races or Events. So we needed a way to show only the posts from the category that particular post was tagged under.

I decided that interacting with WP_Query here would be the best thing to do. The next thing we need to do is, because we are on a single.php page we need to find out what category that post is in. We can do this by using the get_the_category function.


// Assign get_the_category to a variable
$catData = get_the_category();

Here we have simply just stored the wordpress function get_the_category(); to a variable $catData. This is so we can use it later. The next step is to get the Category ID, in WordPress Categories are counted as Taxonomies and we can get the ID by passing the get_the_category(); variable through the term_id. Like so:


// Assign get_the_category to a variable
$catData = get_the_category();
$catID = $catData[0]->term_id;

As you can see we have a ‘[0]’ within our $catData. This is because get_the_category returns an array of objects due to a post potentially having numerous categories. By using ‘0’ our array will just loop through the first category. Now that we have found our current category, no matter what post single.php we are on, this will show us the category for this post, which is great if there are going to be more categories created down the line. This is a good way to future proof the website.

The final bit is putting this all together within the WP_Query. So let’s do that:


// Assign get_the_category to a variable
$catData = get_the_category();
$catID = $catData[0]->term_id;
$query = new WP_Query('cat'=.$catID);
// The Loop
if ( $query->have_posts() ) {
while ( $query->have_posts() ) {
$query->the_post();
echo '
<ul>
	<li>' . get_the_title() . '</li>
</ul>
&nbsp;

';
}
} else {
// no posts found
}

This will simply loop through all the posts found in the current category and output them in a list item showing the title.