1

I wan't to include his instant messaging app in Wordpress. I can't find the issue even with the multiple same questions in the multiple forums.

My code : intrachat.php

<?php
/*
Plugin Name: Intrachat
Plugin URI: *************
Description: Chattez en temps réel sur votre intranet
Version: 0.1
Author: *********
License: GPL2
*/

class Intrachat_Plugin
{

    public function __construct()
    {
        include_once plugin_dir_path( __FILE__ ).'/page_title.php';
        include_once plugin_dir_path( __FILE__ ).'/chat.php';
        new IC_Page_Title();
        $chat = new IC_Chat();

    }
}

new Intrachat_Plugin();
?>

chat.php :

<?php

class IC_Chat {

    public function __construct()
    {
        add_action( 'init', ic_show_chat_bar());
    }

    public function ic_show_chat_bar() {    
        if(is_user_logged_in()){
            echo "<script>alert(\"Connecté\")</script>"; 
        }   
    }


}

?>

My problem :

Fatal error: Call to undefined function ic_show_chat_bar() in /var/www/vhosts/dev/html/wp-content/plugins/Intrachat/chat.php on line 7

EDIT :

Trying this :

<?php

class IC_Chat {

    public function __construct()
    {
        add_action( 'init', array($this, 'ic_show_chat_bar()'));
    }

    public function ic_show_chat_bar() {    
        if(is_user_logged_in()){
            echo "<script>alert(\"Connecté\")</script>"; 
        }   
    }


}

?>

Still same error.

  • 1
    Change the line add_action( 'init', ic_show_chat_bar()); to add_action( 'init', array( $this, ic_show_chat_bar() ) );. – Luis Sanz Apr 18 '16 at 10:16
  • Thanks but it's not working, same error. I've edited the post – Nicolas Frbezar Apr 18 '16 at 10:20
  • Sorry, my fault: you need to change the line add_action( 'init', ic_show_chat_bar()); to add_action( 'init', array( $this, 'ic_show_chat_bar' ) );. Note the back-ticks wrapping the function call. – Luis Sanz Apr 18 '16 at 10:35
  • @LuisSanz, you have to make it an answer. Also, there are no backticks, there are single quotes. – Max Yudin Apr 18 '16 at 10:40
  • Even when io had the quote, it still doesn't work :/ – Nicolas Frbezar Apr 18 '16 at 12:17
2

The problem, as pointed out in the php error, is on line 7 of the "chat.php" file:

add_action( 'init', ic_show_chat_bar());

This line has two problems:

  1. The correct syntax for a valid callback function should be without the parenthesis and wrapped in single quotes.
  2. Since the function is declared inside a php class, it won't be accesible to add_action unless we pass the class instance itself as an argument. Since the function is not static, we can use $this to pass the current instance.

Thus, the correct syntax in this case would be:

add_action( 'init', array( $this, 'ic_show_chat_bar' ) );

I recommend visiting the code reference for add_action() and also reading the user contributed notes at the bottom, where several examples of how to work with actions when using a class can be found.

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.