DevinVinson / WordPress-Plugin-Boilerplate

[WordPress] A foundation for WordPress Plugin Development that aims to provide a clear and consistent guide for building your plugins.
http://wppb.io
7.67k stars 2.25k forks source link

WPB - custom meta boxes - call_user_func() expects parameter 1 to be a valid callback, function 'not found or invalid function name #393

Closed 0dp closed 8 years ago

0dp commented 8 years ago

Hi Folks,

Didn't feel like necro bumping https://github.com/DevinVinson/WordPress-Plugin-Boilerplate/issues/278 but I am facing similar issues on a fresh WordPress install when trying to create custom meta fields.

/MY HOOK/ private function define_admin_hooks() { $this->loader->add_action( 'add_meta_boxes', $plugin_admin, 'custom_juncker_metaboxes'); }

/IN admin/admin class/

public static function custom_juncker_metaboxes() {
        add_meta_box(
            'custom_meta_box', // $id
            __('Produkt Specifikationer', 'juncker'), // $title 
            'render_juncker_metaboxes', // $callback
            'juncker', // $page
            'normal', // $context
            'high' // $priority
        ); 
    }

     function render_juncker_metaboxes() {
        echo 'hi';
  }  

It gives me this fancy error Warning: call_user_func() expects parameter 1 to be a valid callback, function 'render_juncker_metaboxes' not found or invalid function name

Korsic commented 8 years ago

Hello :)

you miss " ' "

private function define_admin_hooks() { $this->loader->add_action( 'add_meta_boxes', $plugin_admin, 'custom_juncker_metaboxes'); }

Korsic commented 8 years ago

Hello :)

You miss http://joxi.ru/5mdQ8lzuv9Bxv2?d=1

3 авг. 2016 г., в 15:34, Johan Nielsen notifications@github.com написал(а):

Hi Folks,

Didn't feel like necro bumping #278 https://github.com/DevinVinson/WordPress-Plugin-Boilerplate/issues/278 but I am facing similar issues on a fresh WordPress install.

private function define_admin_hooks() { $this->loader->add_action( 'add_meta_boxes', $plugin_admin, 'custom_juncker_metaboxes); }

— You are receiving this because you are subscribed to this thread. Reply to this email directly, view it on GitHub https://github.com/DevinVinson/WordPress-Plugin-Boilerplate/issues/393, or mute the thread https://github.com/notifications/unsubscribe-auth/ATyMqRwB8TuZ9YYARfVAKq57uNEsIDRBks5qcIrqgaJpZM4JbnL7.

0dp commented 8 years ago

Hey Korsic,

Thanks for pointing that out, but somehow it got lost in my copy/paste from source. The ' is in the original code. So that is not the issue ;)

I have updated my snippet

slushman commented 8 years ago

If your callback function is in a class, you'll need to call it like this:

array( $this, 'render_juncker_metaboxes' ), // $callback

0dp commented 8 years ago

That was my thought too, but I am then presented with another error:

Notice: Undefined variable: this in /juncker/admin/class-juncker-admin.php

widoz commented 8 years ago

You cannot use $this within a static method. Try to use the class name instead

array( 'Admin_Class', 'render_juncker_metaboxes' ) I think this should work too array( __CLASS__, 'render_juncker_metaboxes' )

In any case, why are you defining the method static?

slushman commented 8 years ago

Ah, I overlooked that it was static. @widoz is correct. And why is it static?

0dp commented 8 years ago

I am not that well versed in the OOP paradigm, and so I thought that render_juncker_metaboxes() didn't have to be part of the instance since it is called directly and is never altered.

But as I read not I can see that trying to do what I did will always throw an error and is super bad practice. So thanks guys.

@widoz, I did in fact try CLASS but that gave some error too (don't recall what though)

So I ended up doing:

function custom_juncker_metaboxes() { add_meta_box( 'custom_meta_box', // $id __('Produkt Specifikationer', 'juncker'), // $title array($this, 'render_juncker_metaboxes'), // $callback 'juncker', // $page 'normal', // $context 'high' // $priority ); }

 function render_juncker_metaboxes() {
    echo 'hi';

}

Thanks 💃

widoz commented 8 years ago

@0dp Just to clarify, the __CLASS__ way didn't work because of the method 'render_juncker_metaboxes' was not static too. I missed it, sorry.