Error adding button in wordpress admin

-1

I'm developing a plugin for wordpress and when I use object orientation to add a button in the post section it gives me the following error:

  

"Warning: call_user_func_array () expects parameter 1 to be a valid   callback, function 'add_media_button' not found or invalid function   name in /var/www/jovempan/wp-includes/class-wp-hook.php on line 286 "

NOTE: This error only appears when I use POO

My code:

class PostagensRelacionadas{
   public function __construct(){
      add_action('media_buttons', 'add_media_button');
   }

   public function add_media_button() {
      $dados = printf( ' <a href="#TB_inline?&width=600&height=550&inlineId=my-content-id" class="button my-button my-custom-button" id="my-custom-button">' . '<span class="wp-media-buttons-icon dashicons dashicons-art"></span> %s' . '</a>', 'Notícias Relacionadas', __( 'Notícias relacionadas', 'textdomain' ) );
   }
}

$class = new PostagensRelacionadas();
$class->add_media_button();
    
asked by anonymous 19.11.2018 / 20:51

1 answer

0

See, since you are in a class, you must pass it itself, plus the function name as parameter in "add_action":

    public function __construct(){
      add_action('media_buttons', array($this,'add_media_button'));
   }

In this way, "$ this" represents the current class, and 'add_media_button', its function.

Take a look at the Callbacks documentation. For a good introduction to object-oriented plugins, there is a very useful "skeleton", Wordpress Plugin Boilerplate

    
21.11.2018 / 03:18