Skip to content

Integration of ShoutOut SMS Platform with a Wordpress ninjaform plugin

License

Notifications You must be signed in to change notification settings

SurfEdge/shoutout-wp-ninjaform

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

4 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Integrating ShoutOut SMS Gateway with Wordpress Ninja Forms

By using a WP Hook you can easily integrate the sms gateway to a wordpress site. Using Ninja Forms on submit action listener the follwoing hook will be called.

How to integrate

Include the SDK

// Include the ShoutOut SDK
require __DIR__ . '/shoutout/autoload.php';

use Swagger\Client\ShoutoutClient;

Add the WP Hook & Write the ninja form callback function

add_action( 'shoutout_sms', 'ninja_forms_processing_callback' );

function ninja_forms_processing_callback( $form_data ){
    // in the body all the validation and the requests will be handle
    // check the index.php for an example code
}

Get form fields required

Check the index.php file for the complete implementation.

    $form_id       = $form_data[ 'id' ];
    $form_fields   =  $form_data[ 'fields' ];
    
    foreach( $form_fields as $field ){
        $field_id    = $field[ 'id' ];
        $field_key   = $field[ 'key' ];
        $field_value = $field[ 'value' ];

        // Here you have to assign the values which will be sent to shoutout
        // The field key is coming from the form element
        switch ( $field_key ) {
            case 'name':
                $name =  $field_value;
                break;
            case 'org':
                $org =  $field_value;
                break;
            case 'email':
                $email =  $field_value;
                break;
            case 'mobile':
                $mobile =  $field_value;
                break;                         
            default:
                break;
        }
    }

Use ShoutOut methods

Initialize the sdk as follows

    $apiKey = 'INSERT_API_KEY';
    $client = new ShoutoutClient($apiKey,true,false);
  1. Add new contact
    $contact = array(
        'mobile_number' => $number,//Required if not specified user_id
        'user_id' => $number,
        'email' => $email,
        'tags' => ['your_tag'],
        'name' => $name,
        'university' => $org
    );

    $contacts = array($contact);
    try {
        $result = $client->createContacts($contacts);
        print_r($result);
    } catch (Exception $e) {
        echo 'Exception when creating contacts ', $e->getMessage(), PHP_EOL;
    }
    
  1. Add new Activity (Event)
    $activity = array(
        'userId' => $number, //Required
        'activityName' => 'activity name',
        'activityData' => array(
            'mobile' => $number,
            'name' => $name,
            'org' => $org,
            'email' => $email // can add any custom fields here
        )
    );

    try {
        $result = $client->createActivity($activity);
        print_r($result);
    } catch (Exception $e) {
        echo 'Exception when creating activity ', $e->getMessage(), PHP_EOL;
    }
    

Helpers

To convert the Phone format from the form field to the supported format

    // Here the number format is converted from (077) 123-4567 to 94771234567
    // Which is needed by ShoutOut to process SMS
    $number = $mobile;
    $number = str_replace('(', '', $number);
    $number = str_replace(')', '', $number);
    $number = str_replace(' ', '', $number);
    $number = str_replace('-', '', $number);
    $number = preg_replace('/^0/', '94', $number);
  • Add screenshots

About

Integration of ShoutOut SMS Platform with a Wordpress ninjaform plugin

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published