3

I would like to do the following:

Registered users in my wordpress application can receive messages. They can choose to receive messages in the following ways:

  • The Wordpress application (Inbox system)
  • SMS message
  • Email

I would like to use the Front End PM plugin to send messages in portal. For the sms messages I would use the Twilio plugin.

So when they sent a message (functionality by Front end PM plugin) I would like to call another function in the Twilio plugin to send an SMS.

What's the proper way to do this? I don't think editing the function in the Front End PM plugin is the correct way to do this? How should it be done?

4 Answers 4

3
+100

Levi Dulstein has a lot of righ, but in my opinion Front-end-pm uses two hooks to send messages:

  • fep_save_message (from back-end)
  • fep_action_message_after_send (from front-end)

Before sending a email few things are checked, like publishig or sending status (includes/class-fep-emails.php):

function save_send_email( $postid, $post ) {
    if ( ! $post instanceof WP_Post ) {
        $post = get_post( $postid );
    }
    if ( 'publish' != $post->post_status ){
        return;
    }       
    if ( get_post_meta( $postid, '_fep_email_sent', true ) ){
        return;
    }
    $this->send_email( $postid, $post );
}

So, for test try add something like this to your plugin or theme:

function send_sms( $postid, $post ) {
    if ( ! $post instanceof WP_Post ) {
        $post = get_post( $postid );
    }
    if ( 'publish' != $post->post_status ){
        return;
    }       
    if ( get_post_meta( $postid, '_fep_email_sent', true ) ){
        return;
    }
    // use Twilio plugin function twl_send_sms
    if( function_exists('twl_send_sms') ) {

        $participants = fep_get_participants( $postid );
        // check if recipients exists, for each get the phone number, 
        // send message and mark sms as sent (or save sms send time)
        $args = array( 
            'message' => $post->post_title, // or $post->post_content
        ); 
        foreach ($participants as $participant) {
            // get usermeta with phone number for $participant ID 
            $args['number_to'] = '098765'; 
            twl_send_sms( $args );
        }
    }
}
add_action( 'fep_save_message', 'send_sms', 99, 2 ); //sending from back-end
add_action( 'fep_action_message_after_send , 'send_sms', 99, 2 ); //front-end

Sorry for my english. I hope you understand what I want to say.

1

I took a look at both plugins docs and it seems that this what you need can be done relatively easy, since both plugins provide nice action hooks here and there.

According to WP Twilio docs you should be able to "send a text message on virtually any WordPress action"and they even provide a simple example of how to that. I would try to use that function and hook it into the other plugin's action.

As for Front End PM plugin there seems to be a nice action hook:

do_action( 'fep_action_message_after_send', $message_id, $message, $inserted_message );

It is executed every time a message is saved in DB after using the front-end UI, so it it seems like a perfect place to plug you SMS sending function in. You even get the message data inside that hook, pretty cool, we'll use that!

So to sum up, I'd try something like this:

  1. Create a function that sends a SMS using Twilio's example, all the arguments are taken from the hook mentioned above:

    function send_sms_with_twilio( $message_id, $message, $inserted_message ){
         // Now the $message should be an array with Front End PM data, 
         // so just check the plugin's code to determine what data you want to pass to a text message.
         // i'll do a simple example:
         $sms_message  = '';
         if ( is_array( $message ) && ! empty( $message['message_title'] ) ) {
              $sms_message = sprintf( 'Hello mate, you have received a new PM titled "%s"', esc_html( $message['message_title'] ) );
         }
    
         // [message_to_id] key should contain WP users ID's, again - you should doublecheck that
         $receivers = ! empty( $message['message_to_id'] ) ? (array) $message['message_to_id'] : [];
    
         // Now I'm not sure where you keep phone numbers, but let's assume it's in user's meta, so I'll try this
         // assuming 'phone_number' is right meta key:
         foreach( $receivers as $receiver ){
              $to = get_user_meta( $receiver, 'phone_number', true );
    
              // Let's send this message finally! But only if we have a number and text.
              // you probably should do some additional validation here 
              // to make sure that the phone number is properly formatted.
              if ( empty( $to ) || empty( $sms_message ) ) {
                    return;
              }
    
              // this is Twilo's plugin function
              twl_send_sms( [
                   'number_to' => $to,
                   'message'   => $sms_message,
            ] );
         }
    }
    
  2. Now you can hook your function to Front End PM's hook:

    add_action( 'fep_action_message_after_send', 'send_sms_with_twilio', 100, 3 );
    

Bear in mind that what I'm doing here is just a quick draft based on what I saw in plugins' code. I haven't tested it but it should give you a general idea how to tackle your issue.

The question might be where to put that code - I think you could try in your theme's functions.php but only for testing purposes and when you are sure that both plugins are active. For production purposes, I would separate that as a third plugin and use is_plugin_active() function to make sure that all the dependencies are in place.

1
  • Thank you for your answer! Is this also possible with announcements?
    – nielsv
    Commented Jul 25, 2018 at 10:17
0

Disclaimer: I'm a relatively new developer and don't know those plugins so my suggestions are general.

You're right not to edit the existing functions.

My thought would be to create your own plugin and look to see if there are any hooks that you can tie into in Front End PM plugin. See if you can add a filter (?) to piggy-back the Twilo function onto it.

You'd need to have your plugin test to make sure both of the other plugins are available before trying to run.

My gut tells me that this would be tremendously unstable, however.

Less useful answer, but possibly better in the long run: consider contacting the developers of those plugins and seeing if they have any ideas.

Good luck!

-4

I would check if possible to intercept the "send message" button, call via javascript a custom ajax function that collects the necessary data and pass them to a function of the "twilio plugin" php classes to send also the SMS..not so easy without good programming skill though.

3
  • 1
    sorry but that is not an actual answer, more like waving your hands. Commented Jul 24, 2018 at 11:32
  • ok, it's not an answer... but I don't see much difference form the other one.. Also, i guess such a question cannot be "fully answered" at all. I should have commented instead of answering ( quite newbie here) but it was something to point in some direction Commented Jul 24, 2018 at 11:57
  • 1
    I have downvoted the question as well, something you can also do. Didn't downvoted the other answer mostly based on it trying more, and being a relative new user.... you already been here long enough to know which answers are worthy the time of writing them, and which not. Commented Jul 24, 2018 at 12:32

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.