Skip to content

Instantly share code, notes, and snippets.

@apermo
Created April 15, 2025 13:00
Show Gist options
  • Save apermo/e30b4b9eb925908cf46951c867c9ee5c to your computer and use it in GitHub Desktop.
Save apermo/e30b4b9eb925908cf46951c867c9ee5c to your computer and use it in GitHub Desktop.
<?php
/**
* This is a snippet if your WordPress plugin uses composer autoloading.
*
* Instead of just dying, it will fail gracefully and add an admin notice to the backend,
* informing the dev to run the autoloader.
*
* Could also be adjusted to check for other things like a /build/ folder or similar things.
*
* Remember to adjust `textdomain` and the `plugin_name` to your needs.
*/
if ( ! file_exists( __DIR__ . '/vendor/autoload.php' ) ) {
add_action(
'admin_notices',
static function (): void {
wp_admin_notice(
sprintf(
/* translators: %1$s: composer command, %2$s: the plugin path */
esc_html__( 'Please run %1$s in %2$s to install the required dependencies.', 'textdomain' ),
'<code>composer install</code>',
'<code>plugin_name</code>'
),
[
'type' => 'error',
]
);
}
);
return;
}
require_once __DIR__ . '/vendor/autoload.php';
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment