0

When running sudo wp install plugin pluginname --allowroot

It causes an error:

PHP Fatal error: Uncaught Error: Using $this when not in object context in /var/www/html/wp-content/plugins/pluginname/blocks.php:89

We have a custom plugin that has this line:

 class Block{ 
    public static function Run() {           
      add_action('enqueue_block_editor_assets',array($this,'RegisterBlock')); //complains on this line

When installing via WP admin - it works fine. But when using WP-CLI it fails.

Any help will be appreciated

1
public static function Run() {

The "static" here means this function doesn't have an object context i.e. it's intended to be called as Block::Run() without actually making a Block. That said, $block = new Block(); $block->Run(); will still work, but it still doesn't have $this set inside the method.

Instead you can use the class name instead of $this to make a callable for a static method:

class Block{ 
    public static function Run() {           
      add_action('enqueue_block_editor_assets', array('Block', 'RegisterBlock') );

But I've no idea how the original code is working in wp-admin. Is it code definitely being called?

| improve this answer | |
  • Thank you. If there was a namespace, do I add it before the class name like: add_action('enqueue_block_editor_assets', array('Namespace\Block', 'RegisterBlock') ); – Michael Soriano Nov 7 at 0:52
  • I'm not sure sorry, but that looks sensible yes. – Rup Nov 7 at 1:40

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.