Skip to content
Advertisement

i am adding add_action inside a model of wordpress plugin but it is showing error unexpected identifier

this is my model file

class customactionsModel {

     function getMessagekey(){
       $key = 'customfunction'; if(is_admin()){$key = 'admin_'.$key;}return $key;
     }

    function __construct(){
        add_action( 'wp_ajax_admission_form', 'admission_form');
        add_action( 'wp_ajax_nopriv_admission_form', 'admission_form');
    }
    function admission_form() {
     //call hook;
        echo "ljdlf";
        wp_die();
    }
 }

i am facing error that is:

error - unexpected identifier "add_action", expecting "function" or "const" in Standard input code

Advertisement

Answer

Your class definition is not correct. your not defining the scope of the functions, and you need to refer back to the class in the add action

class customactionsModel {

 public function getMessagekey(){ //May needs to be a private function
   $key = 'customfunction'; if(is_admin()){$key = 'admin_'.$key;}return $key;
 }

  public function __construct(){
      add_action( 'wp_ajax_admission_form', [$this,'admission_form']);
      add_action( 'wp_ajax_nopriv_admission_form', [$this,'admission_form']);
  }
  public function admission_form() {
   //call hook;
      echo "ljdlf";
      wp_die();
  }
}
User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement