PHP code example of devuri / plugin-interface

1. Go to this page and download the library: Download devuri/plugin-interface library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

devuri / plugin-interface example snippets




use Urisoft\PluginInterface;

class MyPlugin implements PluginInterface
{
    public static $plugin_dir_path;
    public static $plugin_dir_url;

    public static function init(string $plugin_dir_path = '', string $plugin_dir_url = ''): object
    {
        static $instance = [];

        $called_class = static::class;

        if (!isset($instance[$called_class])) {
            $instance[$called_class] = new $called_class();
        }

        self::$plugin_dir_path = $plugin_dir_path;
        self::$plugin_dir_url = $plugin_dir_url;

        return $instance[$called_class];
    }

    public function hooks(): void
    {
        // Register hooks here using WordPress's hook registration functions
        // For example:
        // add_action('init', [$this, 'my_init_function']);
        // add_filter('the_content', [$this, 'my_content_filter']);
    }
}



use Urisoft\AbstractPlugin;

class MyPlugin extends AbstractPlugin
{
    public function hooks(): void
    {
        // Register hooks here using WordPress's hook registration functions
        // For example:
        // add_action('init', [$this, 'my_init_function']);
        // add_filter('the_content', [$this, 'my_content_filter']);
    }
}



// Get the plugin directory path
$plugin_dir_path = wp_plugin_dir_path(__FILE__);

// Define the plugin URL
$plugin_dir_url = plugin_dir_url(__FILE__);

// Initialize the plugin
$my_plugin = MyPlugin::init($plugin_dir_path, $plugin_dir_url);

// Optionally, call the hooks() method to register hooks
$my_plugin->hooks();

    if (MyPlugin::is_installed('example-plugin/example-plugin.php')) {
        echo 'Plugin is installed.';
    }
    

    if (MyPlugin::is_active('example-plugin/example-plugin.php')) {
        echo 'Plugin is active.';
    }
    

    $installed_plugins = MyPlugin::get_installed_plugins();
    print_r($installed_plugins);
    

function is_example_plugin_ready() {
    $plugin_file = 'example-plugin/example-plugin.php';
    return MyPlugin::is_installed($plugin_file) && MyPlugin::is_active($plugin_file);
}

if (!is_example_plugin_ready()) {
    // Show admin notice or handle plugin dependency
}