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.
3. Create a WooCommerce template file in your child theme
Create a directory in your child theme folder named woocommerce. Inside the woocommerce folder, create another new folder named single-product. Inside the single-product folder, create a file named price.php. The contents of your price.php file should look like this:
That’s all that’s required to add schema.org markup to individual WooCommerce product pages. Pretty simple.
If you run into any issues or it doesn’t seem to be working for you, let me know. I’ve only tested this with two themes, Vantage and Virtue. Remember, this only works with well-crafted WordPress themes. Doing this with purchased themes from ThemeForest or other paid theme marketplaces can be significantly more difficult.
Comments are open so let me know if you have any issues, additions, questions, or suggestions.
I’ve been using qunb for quite a while. They generate what they call datastories for your sites using Google Analytics data. The datastories include data like number of visitors, top countries, and most viewed pages. They do a really nice job of presenting your analytics data in a more human readable fashion, a good birds-eye view of it all.
After installing the plugin, you’ll need to authorize it to use your Google Analytics account. You can also connect your Facebook page and Twitter account, but those don’t require any special permissions.
Qunb has also setup a Google Group that will host discussions about bugs and ideas for improvements. As of this post, however, the Google Group gives me a page titled “Authorization Failed“, with a message stating “Please sign in with an authorized account to view this content“.
The WordPress plugin will add a new sub-menu to the main Dashboard menu. It has 4 main parts to it. The first shows stats about visitors or pageviews, you can select which you want to view. The second part shows pageview stats on the most recent post. The third part, titled Tops of the day, lists your top posts, best social network in terms of referrals, and the best non-social referrer. The fourth and last part displays the growth comparison compared to the last week. You can also view growth comparison for the last month, but you’ll need to have a paid qunb account for that.
The starter plan is $5/month and includes unlimited datastories, and there’s no limit on the number of websites you track.
I found Advanced Custom Fields (also known as ACF) about 6 months ago while working on a project for a client. They didn’t want to have to mess around with editing the Custom Fields that come native with WordPress, it just wouldn’t have worked as smoothly.
The client needed to require one image, one PDF, one year selection, and one category. The category consisted of two options, “Weekly” or “Daily”. If you’re wondering, it was a newspaper client who wanted to categorize their posts as being either a “weekly issue” or a “daily issue”. Makes sense for a newspaper!
So, enter the hero of this post, Advanced Custom Fields. I was able to set everything up with Advanced Custom Fields within about 20 minutes, and that even counts the time that I took to make various theme templates pull data from Advanced Custom Fields. The actual setup of Advanced Custom Fields took about 2 minutes.
I’ve since started using Advanced Custom Fields here at longren.io, too. Independent Publisher, the WordPress theme I’ve been using, likes to show one main category when you’re viewing a single post, even if it’s not the most relevant category. So instead of a post about WordPress having the Git category shown at the top, I can now specify which category I want to be shown. So, for a post like this, I would obviously choose WordPress as my primary category.
I’ve already added the necessary parts to my Independent Publisher child theme, and have sent a pull request to Raam Dev to get his thoughts. It’s a very easy thing to support in a theme, however, it requires that everyone using that theme use the same field name in ACF.
I named my field primary_category, since that’s exactly what it is.
After you’ve added your “Primary Category” custom field, you can then use the value of that field throughout your theme. I’ll have a short post later this week on exactly how you can display the primary category value in your theme. Or, if you want to know right now, you can see this pull request at GitHub.
As you can tell, Advanced Custom Fields is a beast of a plugin. I also love that Advanced Custom Fields is totally free, which is kind of amazing to me. I’ve come across many paid plugins that are nowhere near as polished and user friendly as Advanced Custom Fields.
Advanced Custom Fields doesn’t skimp on the documentation, either. Their documentation site is extremely helpful, I never once ventured away from it while getting familiar with Advanced Custom Fields for the first time.
The great thing about this is that it can be applied to any theme, not just Independent Publisher. So, if you’re not using Independent Publisher, just setup Advanced Custom Fields as I described and make the necessary changes for your theme.
A follow-up post will have more details on using data from Advanced Custom Fields, no matter what theme you’re using.