how can I use add_action run another function when publishing new post?

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.

tech