Skip to content

Instantly share code, notes, and snippets.

@smartdeal
Last active March 1, 2022 07:11
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save smartdeal/c23a7e88ac2dc9ce22875ea5fe98c2e8 to your computer and use it in GitHub Desktop.
Save smartdeal/c23a7e88ac2dc9ce22875ea5fe98c2e8 to your computer and use it in GitHub Desktop.
пример интеграции #Wordpress #Sendpulse #WP_CF7
<?php
function add_email_to_sendpulse($contact_form) {
$wpcf7 = WPCF7_ContactForm::get_current();
$submission = WPCF7_Submission::get_instance();
$data = $submission->get_posted_data();
$cur_email = $data['Email'];
if ($cur_email != '') {
define( 'API_USER_ID', '' );
define( 'API_SECRET', '' );
define( 'TOKEN_STORAGE', 'file' );
$SPApiProxy = new SendpulseApi( API_USER_ID, API_SECRET, TOKEN_STORAGE );
$SPApiProxy->addEmails(633919, array($cur_email) );
}
}
?>
@RuslanKuligin
Copy link

Привет, скажи пожалуйста, куда это нужно вставить?)) И как это использовать.

@burre
Copy link

burre commented Oct 19, 2017

@RuslanKuligin Вам нужно взять свои креденшелы в панели SendPulse, чтобы присвоить значения константам API_USER_ID и API_SECRET. Предполагается, что при вызове функции add_email_to_sendpulse() ваши данные будут передаваться в Sendpulse и оттуда могут автоматически отправляться приветственные письма или целая цепочка писем.

Подробности работы с API и примеры можно найти здесь https://github.com/sendpulse/sendpulse-rest-api-php
Также, вместо плагина ContactForm вы можете использовать формы подписки, созданные с помощью конструктора форм SendPulse, а также использовать плагин для вордпреса, который упрощает работу с этим формами https://wordpress.org/plugins/sendpulse-email-marketing-newsletter/.

Смотрите также дополнительную информацию здесь:
https://sendpulse.com/ru/knowledge-base/email-service/mailing-list-recipients/create-subscription-form
https://sendpulse.com/ru/blog/subscription-forms

@snmazko
Copy link

snmazko commented May 26, 2018

Рабочий вариант интеграции без использования автозагрузчика:

my-theme - папка темы, в которой будет вызываться метод

  1. в папке wp-content/themes/my-theme создать директории sendpulse-library/sendpulse-rest-api-php в которую разархивировать содержимое папки sendpulse-rest-api-php-master архива API Sendpulse, (zip архив кода https://github.com/sendpulse/sendpulse-rest-api-php/archive/master.zip), должна получиться следующая структура: http://joxi.ru/12MEngMc43M0G2

  2. в папке wp-content/themes/my-theme создать файл cf7-sendpulse.php со следующим содержимым
    (API_USER_ID, API_SECRET, 17145866 подставлять свои с действующего акк):


use Sendpulse\RestApi\ApiClient;
use Sendpulse\RestApi\Storage\FileStorage;

require("sendpulse-library/sendpulse-rest-api-php/src/ApiInterface.php");
require("sendpulse-library/sendpulse-rest-api-php/src/ApiClient.php");
require("sendpulse-library/sendpulse-rest-api-php/src/Storage/TokenStorageInterface.php");
require("sendpulse-library/sendpulse-rest-api-php/src/Storage/FileStorage.php");
require("sendpulse-library/sendpulse-rest-api-php/src/Storage/SessionStorage.php");
require("sendpulse-library/sendpulse-rest-api-php/src/Storage/MemcachedStorage.php");
require("sendpulse-library/sendpulse-rest-api-php/src/Storage/MemcacheStorage.php");

function add_email_to_sendpulse() {

    $data = WPCF7_Submission::get_instance()->get_posted_data();
    $cur_email = $data['your-email'];

    if ($cur_email) {
        define( 'API_USER_ID', 'ade13b2a21171971f9e0d13d' );
        define( 'API_SECRET', 'd0228e77e88830e1b0aa03df2' );
        $SPApiProxy = new ApiClient( API_USER_ID, API_SECRET, new FileStorage() );
        $SPApiProxy->addEmails(17145866, array($cur_email) );
    }
}

  1. в конец файла wp-content/themes/my-theme/functions.php добавить код:
    require get_parent_theme_file_path('/cf7-sendpulse.php');
    remove_all_filters('wpcf7_before_send_mail');
    add_action('wpcf7_before_send_mail', 'add_email_to_sendpulse');

@ivantuzov
Copy link

ivantuzov commented Sep 14, 2018

@snmazko Не срабатывает, выдаёт ошибку в CF7 при отправке данных: Unexpected token u in JSON at position 0

@farik92
Copy link

farik92 commented Feb 15, 2022

@snmazko действительно рабочий вариант!!!
Спасибо за подробную инструкцию.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment