0

We're working on a Wordpress project which requires:

- a list of Therapists with a "Contact" button for each

  • (done, listed with Store Locator and an Ultimate Member user group for the Therapists)

- upon clicking the contact button, a modal window should appear with a contact form which also registers the user by requiring an email address

  • (done with a modal plugin and a custom Ultimate Member registration form)

  • I also have to store the selected Therapist's user ID in a variable upon clicking on it, so we know whom to send the message, sofar I'm doing this by using data-attribute on the button and getting that value with jQuery on modal open

- upon submitting this form, an on-site message should be sent to the selected Therapist with the details provided on the form

  • this is where things get complicated, I am trying to hook the form submission and send the message with Front End PM

The message handling function (based on Front End Pm docs):

  • I tested this, just to be sure, by calling it with Ajax upon modal open, the function does send the message with the corresponding info to the selected Therapist. Yay.

    function sendMessage_withAjax() {
    
       // Getting the selected Therapist's ID from the Ajax call
       $selectedTherapist = $_POST['selectedTherapist'];
    
       // Composing the message
       $message = array(
            // sample data for testing if the function actually works
            'message_title' => 'Sample Title',
            'message_content' => 'Sample Message',
            'message_to_id' => $selectedTherapist
       );
    
       // Setting the message sender with sample data
       $override = array(
            'mgs_author' => '1',
       );
    
       // Sending the message
       fep_send_message( $message, $override );
    }
    

The function with UM hook / without Ajax:

    // This is not doing anything. I'm trying to call the
    // function on registration, this is the only method
    // I've found, I would be more confident with an Ajax call
    // on form submission (no clue about how to), since that worked,
    // but maybe I'm missing something and this should be
    // the more reliable method

    // One more thing: this should only run on that specific form,
    // not on ALL registration (eg from header registration, etc).
    // I have a feeling that this would run on all form submission 
    // across the site.
    // How do I limit this to the "Contact Therapist" form?

    add_action('um_user_register','sendMessage_withUMhook',10,1);

    function sendMessage_withUMhook($user_id,$args) {
       // Safety Issues
       if ( $user_id < 1 )
            return;
       if ( ! function_exists( 'fep_send_message' ) )
            return;

       // Getting the selected Therapist's ID by populating
       // a hidden form field with jQuery, I have no other
       // clue about how to pass that data-attribute to this
       // function, but I'm open for more elegant solutions
       // as this adds unnecessary reg datas to the user
       $message = array(
            'message_title' => 'New Client Request',
            'message_content' => um_user('contactTherapist_message'),
            'message_to_id' => um_user('selectedTherapist')
       );

       // Setting the message sender to the created user's ID
       $override = array(
            'mgs_author' => $user_id,
       );

       // Sending the message
       fep_send_message( $message, $override );
    }

Quick recap:

- How do I pass a variable from front-end data-attribute to the function?

  • (without url parts if possible, because I need this to be done on the same page)

- How do I send a Front End PM message on UM form submission?

  • (should run only on that specific form)

  • (with the form's data + to the selected Therapist)

I tried a couple of more experimental methods, but it only led me to "Backend Fetch Failed" and other errors, so I'm considering those as dead ends.

I'm open to tips on any methods, including jQuery, Ajax or PHP functions, any help would be appreciated.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.