Я не могу подключиться к wpcf7_mail_sent в моем классе php плагина - PullRequest
0 голосов
/ 24 ноября 2018

В моем плагине Wordpress у меня есть класс php, в котором я хочу подключиться к контактной форме 7 hook wpcf7_mail_sent.Но это не работает для меня.

do_something () не входит в процесс перехвата.

Я думаю, что я зарегистрировал этот перехват в неправильном месте (__construct()).

Не могли бы вы мне помочь?

<?php

class MyCF7 {

    public function __construct() {

        add_action( 'wpcf7_mail_sent', array( $this, 'do_something' ) );

    }

    public function do_something() {

    }
}

Расширение:

<?php

/*
Plugin Name: Contact Form 7 - My plugin
Description: My Integration
Version: 1.0
*/

class MyCF7 {

    public function __construct() {
        add_action( 'wpcf7_mail_sent', array( $this, 'do_something' ) );
    }

    public function activate() {
        // add_action( 'wpcf7_mail_sent', array( $this, 'do_something' ) );
        // This hook would not be registered in activate() method.
    }

    public function do_something( $contact_form ) {
        error_log( 'do_something was triggered.' );
        // Header( 'Location: https://google.com' );
    }
}

$my_cf7 = new MyCF7();
register_activation_hook( __FILE__, array( $my_cf7, 'activate' ) );

Теперь мой вопрос: Как я могу перенаправить наURL при отправке контактной формы?

1 Ответ

0 голосов
/ 25 ноября 2018

Я вдохновлен перенаправлением контактной формы 7 плагином и решил мою проблему.

Поскольку модуль контактной формы 7 обрабатывает отправку формы с помощью javascript, я должен добавить прослушиватель событий javascript ипоставьте этот файл в javascript-файл Wordpress.

<?php

/*
Plugin Name: Contact Form 7 - My plugin
Description: My Integration
Version: 1.0
*/

class MyCF7 {

    public function __construct() {
        $this->plugin_url       = plugin_dir_url( __FILE__ );
        add_action( 'wp_enqueue_scripts', array( $this, 'enqueue_frontend' ) );
    }

    public function enqueue_frontend() {
        wp_enqueue_script( 'wpcf7-redirect-script', $this->plugin_url . 'js/wpcf7-redirect-script.js', array(), null, true );
    }
}

wpcf7-redirect-script.js file

jQuery(document).ready(function () {
    wpcf7_redirect_mailsent_handler()
})

function wpcf7_redirect_mailsent_handler () {
    document.addEventListener('wpcf7mailsent', function (event) {
        location.href = 'https://google.com'
    }, false)
}

Извините, я должен был задать этот вопрос по-другому.

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...