• I have tow libraries of code for use in more than one module I want to develop. Can I create an add on module that makes the library available to other modules or do I have to add the code to every module?

    Both libraries have a base bit of code to create autoloading for that library. Directory example contains example.php and sets up autoloading for the example directory. Every file in the directory is a class named example_* in file example_*.php.

    The modules using the Example library would list the Example module as a prerequisite and need only a way to load example.php then all the classes in example would be autoloaded.

    I just need a pointer to a module already doing this or a documentation page. I have experience creating WordPress modules. For this task, my Internet searches have not produced a starting point.

    If sharing a library across modules is not legal, what is the recommended way to add the library unmodified so I can maintain the library in Github and just download the updated library direct into the module code as is?

Viewing 4 replies - 1 through 4 (of 4 total)
  • Moderator threadi

    (@threadi)

    I would advise against making these libraries available in separate plugins. It’s technically possible, yes, but it makes it harder for your users to keep track of everything.

    My recommendation would be to work with Composer packages instead. Make whatever you want to use repeatedly available as a Composer package and install it via Composer in every plugin before you publish it. This allows you to manage all your libraries centrally while still using them in every plugin you create.

    That’s exactly how I do it, and I’ve already made several Composer packages available to other developers as well. Of course, you don’t have to do this, but in my opinion, that’s the approach you should take.

    A separate library plugin. Put your autoloader in its own plugin. Since WordPress 6.5 your other plugins can declare it as a dependency in their header:

    Requires Plugins: example-library

    WordPress then requires the library plugin to be active before the dependents. Details are in the Plugin Handbook under “Header Requirements”. Note that the slug is matched against the WordPress.org directory, so for a private or GitHub-hosted plugin you’d need the wp_plugin_dependencies_slug filter, or check class_exists() yourself on plugins_loaded.

    Thread Starter petercode

    (@petercode)

    Thank you for the suggestions. I will experiment over the next few weeks.

    prateekshawebdesign

    (@prateekshawebdesign)

    Hi petercode  Welcome to the WordPress community!

    I’ve used shared libraries across multiple projects, and I’d also recommend Composer rather than creating a separate plugin just for the library.

    I’d keep the library in its own GitHub repository, make it a Composer package, and include it as a dependency in each plugin. That way I can maintain the library in one place, version it properly, and update individual plugins when needed.

    Another advantage is that each plugin remains self-contained after deployment. I don’t have to worry about whether another plugin containing the shared library is installed or active, which makes maintenance and troubleshooting much easier.

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

You must be logged in to reply to this topic.