sharing code across several Wordpress plugins - php

Just wondering about best practices around shared code between WordPress plugins.
I'm thinking of creating a core plugin that has all of the shared setup and
functions used in the other plugins.
I realize I could probably combine all of the plugins into one big one,
but prefer separate, smaller plugins for agility.
So, how do i include common code across several WordPress plugins?
Thanks

All active wordpress plugins run all of the time, so any defines or functions you declare in one plugin will be available in the next. (Edit: As long as they're declared publicly that is)
The problem with what you're suggesting is that you'll have plugins that're explicitly dependent on other plugins so what you're doing is effectively having one big plugin but split across three.
It would be better practice to have one plugin where you have several php files within it that're included in the main php file for that plugin. These can even be in different folders within the main plugin folder if it keeps it easier to organise for you. This way you can split your code out to relevant classes/files etc to keep it neat/maintainable but you're not making plugins that can't be used on their own.

Related

How to customize/extend WordPress without getting impacted from future core/plugin updates?

I'm developing a website and need to customize WordPress using any of the following possible approaches:
Search & Use native WordPress plugins as per the business requirement and customize them for as per the (business) requirement! This methods is easy to move with, although gets difficult to keep the customizations in sync with plugin's future updates.
Should go ahead and write own custom-plugins for intended/specific features and manage/maintain. It also helps keeping the customization-code isolated. Using this method requires lots of development efforts, regular efforts for future-compatibility (more or less deprives us from community benefits, that come with WordPress and its plugin-community)
I wonder, how mature WordPress practitioners achieve future compatibility with WordPress Core, Plugins & Themes etc. for custom application development and what are the best practices to keep WordPress custom development compatible with future updates?
First of all - never modify the core WordPress files. There may be some circumstances where you have to, but if you have to ask the question "can/should I modify core files for this"? The answer is no.
Secondly - you shouldn't be modifying plugin or theme files either, because modifications will be lost in updates.
Now, about best practice. Typically, you'll find a plugin that works well enough to accomodate your client's needs and call it good. Many Good plugins will have relatively straightforward ways to modify or extend them using custom hooks or template overrides (look at Extending Gravity Forms with plugins, hooks, etc. as well as extending WooCommerce again with plugins, hooks, filters, template overrides, or custom API).
Outside of extending plugins (not editing files directly), your best alternative is to write custom code to introduce the desired functionality, though that begins to introduce the issue of falling back to your for updates/security/maintenance, etc.
So, never modify core files or plugin files. Either extend plugin functionality, or write your own code to achieve said functionality.

Will moving a wordpress site create any problems with plugins?

I am currently working on a wordpress site, and the folders are just such a mess, that I want to clean them up and make it a bit more bearable to look at. The site uses multiple plugins, and the main site is in a folder inside the httpdocs folder, which makes me wonder if the plugins will cause any problems by moving the site to the root.
Anyone that knows the answer to this?
Wordpress uses a standard structure for plugins and themes. While these may be overwritten, it is not recommended to do so.
If you really wanna do so, you could take a look at this https://codex.wordpress.org/Editing_wp-config.php#Moving_wp-content_folder for reference, and move it around. Remember that the plugin may use wordpress hooks and filters for adding their own CSS/JS, and thus moving the folder out of the web-directory will make these unavailable, and break the plugin functionality.

How to go about allowing plugins for a custom framework?

First off, this isn't really a programming question but more of a programming concept question. Basically, I've built a bespoke PHP framework to speed up deployment on my end and I want some kind of plugin system in place that will allow me to add specific features to the base of the framework (like the SQL class or maybe a Twitter package) that will allow me to throw them into a folder and not have to actually edit the base for every new project.
Any ideas of the best way of going about this?
Here is a nicely written post by #ircmaxell on how to do that and what are the options:
Handling Plugins In PHP
Also check out:
Best way to allow plugins for a PHP application
what im doing in my cms:
for each plugin i make a folder latin-named of this plugin's name.
i create a /translations folder in there too. Check here.
have a single php file that has 2 basic functions, the plugin_install and plugin_uninstall (you know, things to happen on install/unistall like tables creation/drop)
create a special page of your system that reads these plugins, installed and not and give an on/off switch so users can install/unistall them.
load these single files mentioned above by a single call to include_once on top of your index page (or administration page) so to include whatever functionality they offer.
enabled plugins will be loaded (include_once) from your main page, and also their functionality, so each plugin can call each other's as well.

Recommended method for quickly creating a dynamic programmable php-mysql blog?

I want to create a blog-like website. At the begining I'll need a simple one person blog software, but in the future I'd like to have full access to the code and database (control the way posts are organized, offer special forms for creating new posts, allow users to register, implement a rating system, etc.)
What's the best solution for this? Is there a specific tool that will generate a database and php files, to which I'll have access, or should I code everything from zip?
I know Wordpress and Joomla are good blogging tools, but couldn't figure out whether I'll be able to freely redesign a blog that was created using those tools.
Wordpress is an open-source, flexible website infrastructure that is pluggable; and also quite simple to setup without any code modifications. It will generate its own database files and is capable of upgrading itself with minimal administrative overhead.
If you choose at a later date to expand the blog, you can manipulate and customize the theme (100% of what the end-users see), as you see fit.
Wordpress also has a strong plugin repository that may provide extended functionality without any need to code. For example, a post rating plugin already exists that shouldn't require coding to implement;
http://wordpress.org/extend/plugins/rate-this-page-plugin/
Just install wordpress, you'll have full access to the code and database, and it supports writing plugins to extend functionality. You can change page layout with their template engine.
Writing a blog from scratch is not a simple job
Just use Wordpress for now.
There are plenty of plugins to customize Wordpress
You can later develop your own plugins or write a new weblog system and migrate to it.
I'll promise when getting familiar with Wordpress. you won't leave it anymore!

Making a php script that supports modules

Ok, this is kind of non-descriptive, but I wish to make a site in which I am able to add and remove "functions" via adding and removing php modules.
Basically, is it possible to make a site like a framework where you can insert and remove various php modules, similar to how you can enable and disable modules in any other program
Does this make any sense? :)
The way to go is design the Interface of your core application to the various plugins (or modules...) .
For example, you can decide that all plugins must have a certain directory structure.
This enables, for example your core libary to know where to find view files, new classes etc.
You might want to decide that All plugins have a the same name init file, where you write all the plugin initialization codes, same with other events (init, end etc).
All plugins that are to integrate into a specific menu in your core app should have a menue.php or some other, strictly named and structured configuration file that will tell your core library which menu to integrate it and what will be the texts etc etc.
CORE APPLICATION
The core application should have hookups in various important places, hookups that plugins can use/overidde to change behavior of the core application.
I would also suggest adding events, which is similar to behaviors, just that this time the core app triggers it and calls all functions who where registered to the event.
SUGGESTION
While I personally do not like Elgg too much, the way they designed it will let you understand pretty well how to design a FW which is easily expendable with plug-ins (try tha same with Joomla, wordpress).
You can do both procedural and objected-oriented ways.
Write bunch of functions or classes and then include or require that file
when you using them.
include
include_once
require
require_once
can be used to use those functions and classes

Categories