php class as a plugin in wordpress

2019-09-18 15:23发布

How can I write a plugin that only intents to provide the class to be called by other plugins. for example, if I have a class like this:

class Test{
     public static function testMethod(){}
}

then within wordpress envrionement, we will be able to call Test::testMethod();

Does the following works?

<?php
/*
 Plugin Name: My Plugin
 ...other WP plugin headers
*/
add_action('init', 'test_init', 0);
function test_init(){
    $test=new Test();
}

class Test{
   public static function testMethod(){}  
}
?>

I don't think this is a right way to do it. any input is appreciated.

1条回答
小情绪 Triste *
2楼-- · 2019-09-18 15:57

You wouldn't need the add_action - just activating the plug-in would cause WP to include the class, and a static function like that would then be available.

查看更多
登录 后发表回答