I can not hook into wpcf7_mail_sent in my custom plugin php class












1















In my Wordpress plugin, I have a php class in which I want to hook into the contact form 7 hook wpcf7_mail_sent. But it does not work for me.



the do_something() does not come in to the hook process.



I guess that I have registered that hook in a wrong place (__construct()).



Could you please help me?



<?php

class MyCF7 {

public function __construct() {

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

}

public function do_something() {

}
}


Expansion :



<?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' ) );


Now my question is: How can i redirect to an url when a contact form is submitted?










share|improve this question

























  • It's generally not a good idea to put action hooks in the class constructor, gives you a really tightly coupled code, plus every time you instantiate a class, you have an action call. Also are you instantiating your class at all? As a last resort see if increasing the priority of that hook will work.

    – dingo_d
    Nov 24 '18 at 14:09











  • Thank you @dingo_d. Yes I've instantiated my class and other hooks are working in the instantiated object.

    – Iman Kiani
    Nov 24 '18 at 14:13











  • @dingo_d Also increasing the priority of that hook didn't work.

    – Iman Kiani
    Nov 24 '18 at 14:28











  • @dingo_d I have an activate() member function and have registered it via register_activation_hook. I added hook wpcf7_mail_sent in the activate() method, but it did not work too. After that I added it to the __construct() .

    – Iman Kiani
    Nov 24 '18 at 14:39













  • @dingo_d I think i made a mistake. In the activate() method it will not be triggered, but be triggered in the __construct(). Now , I want to redirect to a custom url, but it does not redirect in the __construct().

    – Iman Kiani
    Nov 24 '18 at 17:35


















1















In my Wordpress plugin, I have a php class in which I want to hook into the contact form 7 hook wpcf7_mail_sent. But it does not work for me.



the do_something() does not come in to the hook process.



I guess that I have registered that hook in a wrong place (__construct()).



Could you please help me?



<?php

class MyCF7 {

public function __construct() {

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

}

public function do_something() {

}
}


Expansion :



<?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' ) );


Now my question is: How can i redirect to an url when a contact form is submitted?










share|improve this question

























  • It's generally not a good idea to put action hooks in the class constructor, gives you a really tightly coupled code, plus every time you instantiate a class, you have an action call. Also are you instantiating your class at all? As a last resort see if increasing the priority of that hook will work.

    – dingo_d
    Nov 24 '18 at 14:09











  • Thank you @dingo_d. Yes I've instantiated my class and other hooks are working in the instantiated object.

    – Iman Kiani
    Nov 24 '18 at 14:13











  • @dingo_d Also increasing the priority of that hook didn't work.

    – Iman Kiani
    Nov 24 '18 at 14:28











  • @dingo_d I have an activate() member function and have registered it via register_activation_hook. I added hook wpcf7_mail_sent in the activate() method, but it did not work too. After that I added it to the __construct() .

    – Iman Kiani
    Nov 24 '18 at 14:39













  • @dingo_d I think i made a mistake. In the activate() method it will not be triggered, but be triggered in the __construct(). Now , I want to redirect to a custom url, but it does not redirect in the __construct().

    – Iman Kiani
    Nov 24 '18 at 17:35
















1












1








1








In my Wordpress plugin, I have a php class in which I want to hook into the contact form 7 hook wpcf7_mail_sent. But it does not work for me.



the do_something() does not come in to the hook process.



I guess that I have registered that hook in a wrong place (__construct()).



Could you please help me?



<?php

class MyCF7 {

public function __construct() {

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

}

public function do_something() {

}
}


Expansion :



<?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' ) );


Now my question is: How can i redirect to an url when a contact form is submitted?










share|improve this question
















In my Wordpress plugin, I have a php class in which I want to hook into the contact form 7 hook wpcf7_mail_sent. But it does not work for me.



the do_something() does not come in to the hook process.



I guess that I have registered that hook in a wrong place (__construct()).



Could you please help me?



<?php

class MyCF7 {

public function __construct() {

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

}

public function do_something() {

}
}


Expansion :



<?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' ) );


Now my question is: How can i redirect to an url when a contact form is submitted?







php wordpress contact-form-7






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 25 '18 at 14:36







Iman Kiani

















asked Nov 24 '18 at 14:05









Iman KianiIman Kiani

63




63













  • It's generally not a good idea to put action hooks in the class constructor, gives you a really tightly coupled code, plus every time you instantiate a class, you have an action call. Also are you instantiating your class at all? As a last resort see if increasing the priority of that hook will work.

    – dingo_d
    Nov 24 '18 at 14:09











  • Thank you @dingo_d. Yes I've instantiated my class and other hooks are working in the instantiated object.

    – Iman Kiani
    Nov 24 '18 at 14:13











  • @dingo_d Also increasing the priority of that hook didn't work.

    – Iman Kiani
    Nov 24 '18 at 14:28











  • @dingo_d I have an activate() member function and have registered it via register_activation_hook. I added hook wpcf7_mail_sent in the activate() method, but it did not work too. After that I added it to the __construct() .

    – Iman Kiani
    Nov 24 '18 at 14:39













  • @dingo_d I think i made a mistake. In the activate() method it will not be triggered, but be triggered in the __construct(). Now , I want to redirect to a custom url, but it does not redirect in the __construct().

    – Iman Kiani
    Nov 24 '18 at 17:35





















  • It's generally not a good idea to put action hooks in the class constructor, gives you a really tightly coupled code, plus every time you instantiate a class, you have an action call. Also are you instantiating your class at all? As a last resort see if increasing the priority of that hook will work.

    – dingo_d
    Nov 24 '18 at 14:09











  • Thank you @dingo_d. Yes I've instantiated my class and other hooks are working in the instantiated object.

    – Iman Kiani
    Nov 24 '18 at 14:13











  • @dingo_d Also increasing the priority of that hook didn't work.

    – Iman Kiani
    Nov 24 '18 at 14:28











  • @dingo_d I have an activate() member function and have registered it via register_activation_hook. I added hook wpcf7_mail_sent in the activate() method, but it did not work too. After that I added it to the __construct() .

    – Iman Kiani
    Nov 24 '18 at 14:39













  • @dingo_d I think i made a mistake. In the activate() method it will not be triggered, but be triggered in the __construct(). Now , I want to redirect to a custom url, but it does not redirect in the __construct().

    – Iman Kiani
    Nov 24 '18 at 17:35



















It's generally not a good idea to put action hooks in the class constructor, gives you a really tightly coupled code, plus every time you instantiate a class, you have an action call. Also are you instantiating your class at all? As a last resort see if increasing the priority of that hook will work.

– dingo_d
Nov 24 '18 at 14:09





It's generally not a good idea to put action hooks in the class constructor, gives you a really tightly coupled code, plus every time you instantiate a class, you have an action call. Also are you instantiating your class at all? As a last resort see if increasing the priority of that hook will work.

– dingo_d
Nov 24 '18 at 14:09













Thank you @dingo_d. Yes I've instantiated my class and other hooks are working in the instantiated object.

– Iman Kiani
Nov 24 '18 at 14:13





Thank you @dingo_d. Yes I've instantiated my class and other hooks are working in the instantiated object.

– Iman Kiani
Nov 24 '18 at 14:13













@dingo_d Also increasing the priority of that hook didn't work.

– Iman Kiani
Nov 24 '18 at 14:28





@dingo_d Also increasing the priority of that hook didn't work.

– Iman Kiani
Nov 24 '18 at 14:28













@dingo_d I have an activate() member function and have registered it via register_activation_hook. I added hook wpcf7_mail_sent in the activate() method, but it did not work too. After that I added it to the __construct() .

– Iman Kiani
Nov 24 '18 at 14:39







@dingo_d I have an activate() member function and have registered it via register_activation_hook. I added hook wpcf7_mail_sent in the activate() method, but it did not work too. After that I added it to the __construct() .

– Iman Kiani
Nov 24 '18 at 14:39















@dingo_d I think i made a mistake. In the activate() method it will not be triggered, but be triggered in the __construct(). Now , I want to redirect to a custom url, but it does not redirect in the __construct().

– Iman Kiani
Nov 24 '18 at 17:35







@dingo_d I think i made a mistake. In the activate() method it will not be triggered, but be triggered in the __construct(). Now , I want to redirect to a custom url, but it does not redirect in the __construct().

– Iman Kiani
Nov 24 '18 at 17:35














1 Answer
1






active

oldest

votes


















0














I am inspired from Contact Form 7 redirection plugin and solved my problem.



As the Contact Form 7 Module handles it's form submission by javascript, I must add a javascript event listener and enqueue that js file to my Wordpress javascript files.



<?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)
}


Sorry I should have asked this question in another way.






share|improve this answer


























  • I think the problem still remains. If I want to redirect to a dynamic url, what shall I do?

    – Iman Kiani
    Nov 25 '18 at 18:57











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53458957%2fi-can-not-hook-into-wpcf7-mail-sent-in-my-custom-plugin-php-class%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









0














I am inspired from Contact Form 7 redirection plugin and solved my problem.



As the Contact Form 7 Module handles it's form submission by javascript, I must add a javascript event listener and enqueue that js file to my Wordpress javascript files.



<?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)
}


Sorry I should have asked this question in another way.






share|improve this answer


























  • I think the problem still remains. If I want to redirect to a dynamic url, what shall I do?

    – Iman Kiani
    Nov 25 '18 at 18:57
















0














I am inspired from Contact Form 7 redirection plugin and solved my problem.



As the Contact Form 7 Module handles it's form submission by javascript, I must add a javascript event listener and enqueue that js file to my Wordpress javascript files.



<?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)
}


Sorry I should have asked this question in another way.






share|improve this answer


























  • I think the problem still remains. If I want to redirect to a dynamic url, what shall I do?

    – Iman Kiani
    Nov 25 '18 at 18:57














0












0








0







I am inspired from Contact Form 7 redirection plugin and solved my problem.



As the Contact Form 7 Module handles it's form submission by javascript, I must add a javascript event listener and enqueue that js file to my Wordpress javascript files.



<?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)
}


Sorry I should have asked this question in another way.






share|improve this answer















I am inspired from Contact Form 7 redirection plugin and solved my problem.



As the Contact Form 7 Module handles it's form submission by javascript, I must add a javascript event listener and enqueue that js file to my Wordpress javascript files.



<?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)
}


Sorry I should have asked this question in another way.







share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 25 '18 at 17:48

























answered Nov 25 '18 at 17:26









Iman KianiIman Kiani

63




63













  • I think the problem still remains. If I want to redirect to a dynamic url, what shall I do?

    – Iman Kiani
    Nov 25 '18 at 18:57



















  • I think the problem still remains. If I want to redirect to a dynamic url, what shall I do?

    – Iman Kiani
    Nov 25 '18 at 18:57

















I think the problem still remains. If I want to redirect to a dynamic url, what shall I do?

– Iman Kiani
Nov 25 '18 at 18:57





I think the problem still remains. If I want to redirect to a dynamic url, what shall I do?

– Iman Kiani
Nov 25 '18 at 18:57




















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53458957%2fi-can-not-hook-into-wpcf7-mail-sent-in-my-custom-plugin-php-class%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Wiesbaden

Marschland

Dieringhausen