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.
After that, you’ll need to add some more code to the beginning of all the PHP files associated with your theme. I suggest you go over the Getting Started guide and really pay attention so you get a good understanding of what Unyson can do. The documentation is really awesome, most issues or questions you could have are more than likely covered in the docs.
I haven’t had much of a chance to play with Unyson, but will get the opportunity to on an upcoming client project, so I’m really looking forward to that.
Unyson is quite new, so hopefully we will see more features as it matures. It’s definitely worth checking out.
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.
When you share a link on Facebook, an image from the link is shown, usually. Sometimes, no image is displayed, as if Facebook couldn’t find a suitable image at the URL.
Adding Open Graph protocol tags to your site will ensure that Facebook knows which image to use when someone shares a page on your website. Open Graph markup is similar to schema.org markup. Both allow you to define values for certain aspects of your website, as seen by other sites like Facebook, Twitter, and many other social media sites.
There’s a couple of ways we can do this. You can use a plugin, like Open Graph Protocol Framework, or you can add Open Graph markup to your WordPress theme manually.
We’ll be covering how to add Open Graph markup to your theme manually. Especially useful for theme developers who want to build Open Graph protocol support into their themes.
Every Open Graph property will be set on-the-fly, depending on which post or page is being viewed or shared. To add the necessary markup, we need to use the add_action WordPress function inside your theme’s functions.php file. Here’s how I’ve been handing it: