Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7021809
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T23:29:56+00:00 2026-05-27T23:29:56+00:00

I am working on a WordPress Plugin and am trying to ensure best practices.

  • 0

I am working on a WordPress Plugin and am trying to ensure best practices. I have two classes, my plugin class “Jargonaut” which is required and then another class called “Dictionary” which is included with require_once() into my main plugin file.

Most of the code in the Jargonaut class addresses initialization and provides controller-like functionality but much of it is highly dependent upon using the Dictionary object (i.e. tightly coupled from my understanding of the term). I wish to keep the Dictionary class separated as it is acting more like a model (in MVC architecture) and interfaces with my database.

I see a lot of gray area in the tight vs. loose coupling and am having a hard time deciding how much is too much?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T23:29:57+00:00Added an answer on May 27, 2026 at 11:29 pm

    If your plugin needs the dictionary object, it has to ask for it:

    class MyPlugin
    {
        /**
         * @var Dictionary
         */
        private $dictionary;
        private function __construct(Dictionary $dictionary)
        {
            $this->dictionary = $dictionary;
        }
    

    You now have loosely coupled your plugin with the Dictionary, the plugin class is not responsible any longer to create the Dictionary for itself, because it’s injected. It takes what it needs.

    So how would that work? The plugin needs to be created somewhere, so this needs a factory. The factory build method knows what your plugin needs:

    class MyPluginFactory
    {
        public static function build($pluginName)
        {
            $plugin = NULL;
            switch($pluginName)
            {
                case 'MyPlugin':
                    $dictionary = new Dictionary();
                    $plugin = new MyPlugin($dictionary);
            }
            return $plugin;
        }
    }
    

    As this is wordpress we know that the bootstrapping of the plugin is done by including the plugin file. So at it’s beginning, the plugin needs to be created. As includes are done in the global scope we want to preserve the plugin object in memory but without being available as a global variable probably. This does not prevent you from creating more than one plugin instance, but it will ensure that when wordpress initializes your plugin (loads your plugin), it will make only use of that single instance. This can be done by making the plugin factory some additional function:

    class MyPluginFactory
    {
        ...
        public static $plugins;
        public static function bootstrap($pluginName)
        {
            $plugin  = self::build($pluginName);
            self::$plugins[] = $plugin;
            return $plugin;
        }
    

    Take care here, that the only usage of the static class member variable is only to ensure that the plugin stays in memory. It technically is a global variable we normally want to prevent, however, the instance needs to be stored somewhere, so here it is (I changed this to public because it is a global variable and it shouldn’t be shy about it. Having a public can help in some circumstances in which private or protected are too restrictive. Also it shouldn’t be a problem. If it is a problem, there is another problem that should be fixed first).

    This basically decouples your plugin code from wordpress itself. You might want to also create a class that offers and interface to any wordpress function you’re making use of, so you’re not bound to these functions directly and your plugin code stays clean and loosely coupled to wordpress itself.

    class WordPressSystem
    {
        public function registerFilter($name, $plugin, $methodName)
        {
            ... do what this needs with WP, e.g. call the global wordpress function to register a filter.
        }
        ...
    }
    

    Then add it as a dependency again if your plugin needs the WordPressSystem to perform tasks (which normally is the case):

    class MyPlugin
    {
        ...
        public function __construct(WordPressSystem $wp, Dictionary $dictionary)
        ...
    

    So to finally wrap this up, only the plugin php file is needed:

    <?php
    /*
     * MyPlugin
     * 
     * Copyright 2010 by hakre <hakre.wordpress.com>, some rights reserved.
     *
     * WordPress Plugin Header:
     * 
     *   Plugin Name:    My Plugin
     *   Plugin URI:     http://hakre.wordpress.com/plugins/my-plugin/
     *   Description:    Yet another wordpress plugin, but this time mine
     *   Version:        1.2-beta-2
     *   Stable tag:     1.1
     *   Min WP Version: 2.9
     *   Author:         hakre
     *   Author URI:     http://hakre.wordpress.com/
     *   Donate link:    http://www.prisonradio.org/donate.htm
     *   Tags:           my
     *
     * This program is free software; you can redistribute it and/or modify
     * it under the terms of the GNU General Public License as published by
     * the Free Software Foundation; either version 3 of the License, or
     * (at your option) any later version.
     *
     * This program is distributed in the hope that it will be useful,
     * but WITHOUT ANY WARRANTY; without even the implied warranty of
     * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
     * GNU General Public License for more details.
     *
     * You should have received a copy of the GNU General Public License
     * along with this program; if not, write to the Free Software
     * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
     */
    Namespace MyPlugin;
    
    # if your file is named 'MyPlugin.php' this will be 'MyPlugin'.
    return PluginFactory::bootstrap(basename($plugin, '.php'));
    
    class PluginFactory
    {
        private static $plugins;
        public static function bootstrap($pluginName)
        {
            $plugin = self::build($pluginName);
            self::$plugins[] = $plugin;
            return $plugin;
        }
        public static function build($pluginName)
        {
            $plugin = NULL;
            switch($pluginName)
            {
                case 'MyPlugin':
                    # Make your plugin work with different WordPress Implementations.
                    $system = new System\Wordpress3();
                    $dictionary = new Dictionary();
                    $plugin = new Plugin($system, $dictionary);
            }
            return $plugin;
        }
    }
    
    class Plugin
    {
        /**
         * @var System
         */
        private $system;
        /**
         * @var Dictionary
         */
        private $dictionary;
        private function __construct(System $system, Dictionary $dictionary)
        {
            $this->system = $system;
            $this->dictionary = $dictionary;
        }
    
    ...
    

    The bootstrap method can also take care of registering an autoloader or do the requires.

    Hope this is useful.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am working on integrating two wordpress plugins. What I am trying to do
I'm working on a wordpress plugin, and have come across a barrier of sorts.
I am working on a wordpress site, and I have downloaded a plugin called
I'm working on a WordPress plugin. At the admin page, I have a ajax
I have a problem when trying to manually upload a new plugin in wordpress.
I have developed a WordPress plugin which needs to perform additional processing when a
I'm working with a WordPress plugin. It adds a flash player to the output
I developed a plugin with Settings link that was working fine in WordPress 2.7.
A wordpress theme I'm working on has headlines which span across the entire content
I was working on a WordPress plugin, and I found that when the_content filter

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.