
When creating a plugin that is meant to modify the front-end of the WordPress site, you may need to execute code within the theme, using common hook or otherwise. Let’s take on the example of if we’re creating a plugin that outputs a random quote.
For starters, you’re going to want to have a function within your plugin that will output something to the browser. You’ll probably want the user of the plugin to be able to decide where their want to quote to appear in their theme. While you could certainly tell them to simply reference the function where they want the quote, however there’s one major flaw in this method. If the user (for whatever reason) decides to deactivate the plugin, your function will no longer be loaded into the theme. That function no longer exists will cause PHP to issue an error, which will have a negative effect on the site.
A better way of having the user add something to their theme is to have them create an action. Creating an action will allow you to plug a function (or many) into the hook. The difference is, that if your plugin gets disabled, the hook will still be created, your function will just no longer be hooked into it. Thus, the user will not experience any errors (related to your plugin) in their theme.
By simply having the user add a line that includes apply_action() with a specific name, you’re then able to hook into that action normally and create a better experience for your plugin.




