init plugin without construct?
-
Hello,
i am writing my first own plugin and it seems to work more and more.
Now i getting problems with wp functions which are not useable in constructor but in functions after that. I googled little bit and found that hint:
https://wordpress.stackexchange.com/questions/70055/best-way-to-initiate-a-class-in-a-wp-pluginTo use an own contructor-function seems to be a coll idea, but i stuck on the problem, that i can use variables in the new constructor function and i dont know why.
My code:
add_action( 'plugins_loaded', array( 'MyPlugin', 'plugin_setup' )); class MyPlugin { protected $url; protected $path; protected $basename; public static function plugin_setup() { $class = __CLASS__; new $class; $this->url = plugin_dir_url(__FILE__); $this->path = plugin_dir_path(__FILE__); $this->basename = plugin_basename(__FILE__); } } /* global $myplugin; $myplugin= new MyPlugin(); */
Error:
Fatal error: Uncaught Error: Using $this when not in object context in /www/htdocs/XXXXX/wordpress/wp-content/plugins/myplugin/myplugin.php:41 Stack trace: #0 /www/htdocs/XXXXX/wordpress/wp-includes/class-wp-hook.php(286): MyPlugin::plugin_setup('') #1 /www/htdocs/XXXXX/wordpress/wp-includes/class-wp-hook.php(310): WP_Hook->apply_filters(NULL, Array) #2 /www/htdocs/XXXXX/wordpress/wp-includes/plugin.php(465): WP_Hook->do_action(Array) #3 /www/htdocs/XXXXX/wordpress/wp-settings.php(374): do_action('plugins_loaded') #4 /www/htdocs/XXXXX/wordpress/wp-config.php(115): require_once('/www/htdocs/XXXXXX...') #5 /www/htdocs/XXXXX/wordpress/wp-load.php(37): require_once('/www/htdocs/XXXXXX...') #6 /www/htdocs/XXXXX/wordpress/wp-blog-header.php(13): require_once('/www/htdocs/XXXXXX...') #7 /www/htdocs/XXXXX/wordpress/index.php(17): require('/www/htdocs/XXXXXX...') #8 {main} thrown in /www/htdocs/XXXXX/wordpress/wp-content/plugins/myplugin/myplugin.php on line 41
- The topic ‘init plugin without construct?’ is closed to new replies.