• IoClaudio

    (@ioclaudio)


    Hi,

    I’m developing a plugin that adds some custom post types and makes some global configurations to the site.

    I’d like to use a more “object-oriented” approach.

    So I’ve developed a PluginManager that has a plugin_setup method that calls other managers that execute their setup functions. In these setup functions, there are the add_action calls. I have attached an example of this configuration.

    What do you think about this approach? There are better patterns?

    Any suggestions will be appreciated.

    Thank you

    claudio

    File functions.php:

    if ( ! class_exists( 'MyPlugin_Manager' ) ) {
    	include_once 'inc/classes/class-mypluginmanager.php';
    	global $myplugin_manager;
    	$myplugin_manager = new MyPlugin_Manager();
    	$myplugin_manager->plugin_setup();
    }

    File class-mypluginmanager.php:

    class MyPlugin_Manager {
    
      public function plugin_setup() {
        …
        // Setup of the People post type.
        $pm = new People_Manager();
        $pm->setup();
        …
      }
    
    }

    File class-peoplemanager.php:

    class People_Manager {
    
      public function __construct() {}
      public function setup() {
        // Register the taxonomies used by this post type.
        add_action( 'init', array( $this, 'add_taxonomies' ) );
    
        // Register the post type.
        add_action( 'init', array( $this, 'add_post_type' ) );
    
        // Customize the post type layout of the admin interface.
        add_action( 'edit_form_after_title', array( $this,  'custom_layout' ) );
      }
     ...
    }
    
    • This topic was modified 2 years ago by IoClaudio.
    • This topic was modified 2 years ago by IoClaudio.
    • This topic was modified 2 years ago by IoClaudio.
Viewing 3 replies - 1 through 3 (of 3 total)
  • Moderator threadi

    (@threadi)

    Yes, that is also a practicable way. One of many that you can take. There is no ‘best’ way, as is so often the case. I’ll describe 2 ways that I use:

    In the past, I often created a directory whose files were loaded completely with every loading process. The files in it each corresponded to a subject area (general, export, templates ..) and contained their individual register_settings() for all options used.

    In the meantime, I have switched to working with Composer and more with the hooks in WordPress. There is then an Init class which in turn directly initiates all other necessary classes. These additional classes then define their own options as an array via an individual hook such as

    add_filter( 'pluginname_add_settings', array( $this, 'add_settings' ) );

    The Init class then calls this hook and sets the settings at a central point. There is therefore only a single register_settings() call for the entire plugin, which is used to register all options.

    The wonderful thing about WordPress is that you can always go your own individual way here. Just make sure you adhere to the WordPress coding standards (which is not that difficult) and make it performant.

    Thread Starter IoClaudio

    (@ioclaudio)

    Hi @threadi thank you for your tips.

    It seems a smart alternative, but I’m not sure I’ve understood well.

    Do you add the same filter:

    add_filter( ‘pluginname_add_settings’, array( $this, ‘add_settings’ ) );

    to many classes, isnt’it?

    But where do you call the apply_filters function ?

    Thank you very much

    Claudio B.

    Moderator threadi

    (@threadi)

    I also have a class that takes care of the usage of the settings. There the apply_filters is used to transfer all the collected settings individually to https://developer.wordpress.org/reference/functions/register_setting/.

Viewing 3 replies - 1 through 3 (of 3 total)

The topic ‘Best pattern to configure a site using a plugin’ is closed to new replies.