I want to call my customClass when ever new post publish or update.but after I add add_action to my code , the function will run multiple times even before posting new post. can anyone help me? what is the right solution? I 've been searching for some days but i didn't find the right answer.


class customClass{
   function dothisfunction($mobiles , $message ){
      //send sms to $mobiles number with $message body      

function.php: include('custom.php'); $mobiles = '09112443344'; $message = 'new msg testing';

$myClass = new customClass();

add_action( 'publish_post', array( $myClass ,'dothisfunction'), 10, 2 );//
do_action( 'publish_post', $mobiles, $message );
  • It's because you're manually calling do_action(). This will trigger that action on every page load. Why are you doing this? Jun 8, 2019 at 8:32
  • yes your right it will trigger on every page load. then how should i send my parameters?
    – Nadia
    Jun 8, 2019 at 9:57

1 Answer 1


You're partly there. This is the code you should be using:

add_action( 'publish_post', array( $myClass ,'dothisfunction' ), 10, 2 );

You can't pass parameters to the action that's being executed. You can only "hook" into the action after do_action is called, and whatever parameters have been added to that action.

If there are particular variables that you want that function dothisfunction to have that weren't originally sent along with the event, you'll have to get them from somewhere else.

You can't "add" more parameters to a hook - you can only grab the variable that were sent with it.

So you could probably do something like:

    class customClass {

        public $smsMobileNumber;

        public $message;

        function dothisfunction( $postId, $post ) {

            $fullMessage = sprintf( $this->message, $postId, $post->post_title );

            // code to send SMS message

    $myClass = new customClass();
    $myClass->smsMobileNumber = '012345456';
    $myClass->message = 'Post published with ID %s and title %s';

    add_action( 'publish_post', array( $myClass ,'dothisfunction' ), 10, 2 );

So you can see here that you're populating your SMS mobile number and message template in your custom class and then you hook into the publish_post action. You then create a message which includes your new post ID and you have your code that sends your SMS message.

You'll see in the function dothisfunction that I can only use the parameters that were originally sent in this action. In this case, after I review the WordPress source code, I see that this hook is fired with 2 parameters: the post ID, and the Post itself. That's all I can use in my hooked-function. I must supply any other parameters myself in another way.

Also worth noting that your code doesn't need to call do_action. WordPress will run this action and all you need to do is hook into it.

  • thank you very much
    – Nadia
    Jun 8, 2019 at 10:11
  • Would you like to mark the answer as accepted if it works for you?
    – Paul G.
    Jun 8, 2019 at 10:23

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.