Question

I do have a few PHP custom functions that I use almost every time on my applications. Those functions are not all related to WordPress, but some are and thus need that WP is initialized before I can use them (generic load for the text domain or custom post queries for example).

I would like to be able to use those function in all my WP applications (themes and plugins) and I wanted to know where would be the best place to put them.

I first thought that I might use a master parent theme that would be the parent theme on all my websites, but I don't know if that would be sufficient as the theme is only loaded after the plugins and I don't know if some hooks might be triggered before my theme is loaded.

Any thoughts?

Was it helpful?

Solution

Since you want to be able to use these functions within your themes AND plugins, and taking into consideration your comment regarding loading, I'd recommend the following approach.

Organize your functions into a library - depending on your methods and approaches, this could be multiple or single files. It could be just functions or as a class.

Wrap your functions or classes with conditional checks to make sure they are not defined already (in case you inadvertently load it twice). if ( function_exists( ... or if ( class_exists( ...

Establish one point in your plugin or theme where you check if a key function or class exists, and if not, use include_once() to load your library file. Hook this to the initialization of your plugin or theme, so that you're just doing it once (although each theme or plugin would check - but if it's already loaded, it will only be loaded once). For example:

if ( ! function_exists( 'my_cool_utility' ) ) { include_once( 'my_function_library.php' ); }

Doing it this way allows you to use your library across multiple themes and plugins that may or may not be used together, while making sure (1) that the code is there and available while (2) avoiding issues of it being loaded more than once.

This is a method that I personally use in developing both free and premium plugins and themes. I have a number of libraries that get reused across different products that may or may not be used together. By packaging the library with the theme or plugin, I know it will be there and the user doesn't have to load something separate to use it.

Hope I explained that well enough for you. If not, ask questions in the comments and I'll try to edit for clarity.

OTHER TIPS

Then do it as a plugin and then use include(); to organise your PHP custom functions in their own PHP files either within the plugin directory or outside of it.

Also, you should consider using it as a Must Use plugin if the site heavily relies on the functions.

Here is my final solution. Credits to @butlerblog for the hint.

I created a component package that I can include on every website (and update with new functionnalities anytime).

Here is the composer.json file.

    {
        "name": "tim/wp-utils",
        "description": "WordPress utility functions",
        "type": "component",
        "version": "1.0.2",
        "authors": [
            {
                "name": "Timothée Moulin",
                "email": "timothee.moulin@gmail.com"
            }
        ],
        "require": {
            "php": ">=7.3",
            "robloach/component-installer": "*"
        },
        "extra": {
            "component": {
                "files": [
                    "*"
                ]
            }
        }
    }

Notice the robloach/component-installer requirement and the extra component configuration. This allows me to place the "plugin" anywhere logical for me in my app (instead of the vendor directory).

And in my plugin file I surrounded every function declaration with a function_exists check

<?php
if (!function_exists('my_utility_function')) {
    function my_utility_function() {
        // @todo
    }
}

Then in every plugin plugin.php and theme functions.php file I include the utility file

<?php require_once WP_CONTENT_DIR . '/components/wp-utils/wp-utils.php';
Licensed under: CC-BY-SA with attribution
Not affiliated with wordpress.stackexchange
scroll top