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.22k stars 403 forks source link

Not in functions.php #502

Closed gwuluo closed 1 year ago

gwuluo commented 1 year ago

Does it have to be in functions.php? It is not executed in other php, how should I write it? Thank you

YahnisElsts commented 1 year ago

No, it doesn't have to be in functions.php. You can put the code that initializes the update checker in a different file, but you need to be careful to pass the correct file path to the buildUpdateChecker() method. The second argument should be the full path to a file in your theme's main directory, like functions.php or style.css. If the code is in functions.php you can just use the __FILE__ constant, but in a different file, you'll need another solution.

How you do that is up to you. For example, you could define a new constant in your functions.php, like

defined('MY_THEME_FUNCTIONS_PHP_FILE', __FILE__);

... and then pass that to the update checker:

$myUpdateChecker = Puc_v4_Factory::buildUpdateChecker(
    'http://example.com/path/to/details.json',
    MY_THEME_FUNCTIONS_PHP_FILE,
    'unique-plugin-or-theme-slug'
);
gwuluo commented 1 year ago

Thank you very much.