Error adding wordpress admin button

Asked

Viewed 18 times

-1

I am developing a wordpress plugin and when I use object orientation to add a button in the post section it displays 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 using 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();

1 answer

0

See, since you are in a class, you should pass it, next to the function name as parameter in add_action:

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

Thus, the $this represents the current class, and add_media_button, her job.

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

Browser other questions tagged

You are not signed in. Login or sign up in order to post.