I read that when creating new post types, creating a settings page, and creating a new taxonomy, you should create a plugin. So I have done that. But I have functionality, which could be generalized, or are identical, in my plugins. An example of this would be the function which saves to the database. Since everything seems to be put in the global scope, if there is such a thing in PHP, should I create another plugin with shared functionality, or put shared functionality in the functions.php
file?
1 comment
Comments are closed.
1.
First of all
functions.php
is for themes, that are different things than plugins.In WordPress themes should be used for presentation only, plugin for functionality.
If you want you can use themes for functionality, but you shouldn’t. In fact, themes and plugins are just php files that at a specific moment of a WordPress request are loaded.
(WordPress load just one file, that is functions.php for themes and the main plugin file for plugins, load all the other files you need is up to you).
The problem is that in WordPress you can have a non limited number of plugins, but only one theme. So, if you want to change the theme and you have functionality on
functions.php
you’ll lose it: in few words this is why is better put functionality on plugins and use theme for presentation only.2.
Yes, everything in WordPress core is in global namespace and there is a large use of global variables, but that is core, no one force you to follow same rules, you can use namespaces (if your PHP version is 5.3+) and also don’t use any global variable: when WordPress load your plugin it loads a PHP file, so you can do anything that you version of PHP can do: if you want you can write yourself all the functionality, but that make no sense: you use WordPress because of all the features it has, so try to use core function/classes when what you need is covered by core, but feel free to write the PHP code that does what WordPress does not. And to do that, feel free to use all the PHP techniques you know.
3.
I read in your question
I don’t know what you are saving to database and why, but if you have to save WordPress entities: post, taxonomy terms, meta, options, users, transient… WordPress already have functions to save and retrieve from database all them, so there is no need to write custom functions for that.
If you mean some custom tables, yes, if you want you can write some code that abstract and ease the creation and manipulation of custom tables, but in that case:
4.
Now talk about extract common feature from a plugin to another. Yes it is doable and, imho, it’s perfecly fair.
There are different way to do it:
4.1: just create different plugins and install them all
Just like already said, plugin are just php files that at a specific time during WordPress bootstrap are loaded using
require_once
, so really, when 2 plugins are installed, there is no difference of 2 files being in same plugin and loaded by yourself, or two files are loaded by WordPress. Sure you can’t say which file is loaded first and which later, but thanks to to WordPress hooks, you can know when all plugin are loaded, and start to do thing since that moment. Of course before to use a feature placed in another plugin just be sure that plugin/feature is available. A proof of concept code:4.2: Composer
I don’t know if you ever used/heard about Composer. It is a dependency managment tool.
When you need to write a plugin that require another plugin, if both supports Composer, than all the thing is done just adding a line in the
composer.json
file.How to use Composer for plugins is not complex, but too complex for an answer here.
In latest months interest in Composer grew up in the WordPress community (in PHP community it grew up some time ago), and now you can find some useful resources.
Start having a look composer.rarst.net, it’s a resource shared by Rarst, one of this site mods.