YahnisElsts / plugin-update-checker

A custom update checker for WordPress plugins. Useful if you don't want to host your project in the official WP repository, but would still like it to support automatic updates. Despite the name, it also works with themes.
MIT License
2.27k stars 411 forks source link

Calling the project in a file other than core #554

Closed mehdi-najaran closed 10 months ago

mehdi-najaran commented 10 months ago

Is there a way to call this project in a file other than the plugin core?

For example, create a file in the include folder. And that file will be include_once

YahnisElsts commented 10 months ago

Of course. You just need to adjust your initialization code to pass the correct file path (i.e. the full path to the main plugin file) to the update checker since using __FILE__ will no longer work.

mehdi-najaran commented 10 months ago

Can you provide me with a sample code?

YahnisElsts commented 10 months ago

Not in a useful way. The actual code will depend on how your plugin is set up and what you're using to pass in the plugin file path. You could define a custom constant in your main plugin file and replace __FILE__ with that, or have the initialization code in a class and pass the path via dependency injection, or something else.

mehdi-najaran commented 10 months ago

Yes, you are right, but I will do anything. I face an error

Please provide me with a simple code sample for this issue, or send me another communication method such as Telegram so that I can communicate with you there.

YahnisElsts commented 10 months ago

Please post your current code and the error.

mehdi-najaran commented 10 months ago

This was the code that I had previously put in the main file of the plugin:

function auto_update_setup()
{
    require(plugin_dir_path(__FILE__) . '/include/update-checker/plugin-update-checker.php');
    $checkerClass = 'YahnisElsts\\PluginUpdateChecker\\v5\\PucFactory';
    if (class_exists($checkerClass)) {
        $UpdateChecker = $checkerClass::buildUpdateChecker(
            'https://my-site.com/main.json',
            __FILE__,
            'my-slug'
        );
    }
}
add_action('plugins_loaded', 'auto_update_setup');

Now I want to put the code in the include folder and include_once in the main file Please give me the new code

function auto_update_setup()
{
    require(plugin_dir_path(__FILE__) . '/update-checker/plugin-update-checker.php');
    $checkerClass = 'YahnisElsts\\PluginUpdateChecker\\v5\\PucFactory';
    if (class_exists($checkerClass)) {
        $UpdateChecker = $checkerClass::buildUpdateChecker(
            'https://my-site.com/main.json',
            __FILE__,
            'my-slug'
        );
    }
}
add_action('plugins_loaded', 'auto_update_setup');

I changed it myself, but it didn't work

YahnisElsts commented 10 months ago

As I mentioned, perhaps you might consider using a constant to pass the plugin file name to the code. In your main plugin file:

define('MY_MAIN_PLUGIN_FILE', __FILE__);

Then in your auto_update_setup() function, replace __FILE__ with MY_MAIN_PLUGIN_FILE. (Of course, it's recommended to replace MY_MAIN_PLUGIN_FILE with a unique name to avoid plugin conflicts.)

mehdi-najaran commented 10 months ago

thank you . The problem was solved ❤️