wordpress contact form 7 disable email - php

I am facing an issue with Contact Form 7 for Wordpress. I want to disable the email notification which i did using
demo_mode: on
At the same time i want to redirect on submit which i used to do using
on_sent_ok: "location = 'http://domain.com/about-us/';"
Both would work when used individually.But i want to use both at the same time.
I tried doing
on_sent_ok: "location = 'http://domain.com/about-us/';"
demo_mode: on
Doesnt seem to work. Kindly advice.

The plugin author has changed as of at least 4.0 the way you should do this again. The skip_mail property is now private :
class WPCF7_Submission {
private $skip_mail = false;
...
}
You should use this filter : wpcf7_skip_mail
For example :
function my_skip_mail($f){
$submission = WPCF7_Submission::get_instance();
if(/* YOUR TEST HERE */){
return true; // DO NOT SEND E-MAIL
}
}
add_filter('wpcf7_skip_mail','my_skip_mail');

The author of the plugin Contact Form 7 has refactored some of the code for its version 3.9 and since then the callback function for the hook wpcf7_before_send_mail must be written differently.
To prevent Contact Form 7 from sending the email and force it to redirect after the form has been submitted, please have a look at the following piece of code (for version >= 3.9):
add_action( 'wpcf7_before_send_mail', wpcf7_disablEmailAndRedirect );
function wpcf7_disablEmailAndRedirect( $cf7 ) {
// get the contact form object
$wpcf7 = WPCF7_ContactForm::get_current();
// do not send the email
$wpcf7->skip_mail = true;
// redirect after the form has been submitted
$wpcf7->set_properties( array(
'additional_settings' => "on_sent_ok: \"location.replace('http://example.com//');\"",
) );
}

Hook into wpcf7_before_send_mail instead of using the flag .
add_action("wpcf7_before_send_mail", "wpcf7_disablemail");
function wpcf7_disablemail(&$wpcf7_data) {
// this is just to show you $wpcf7_data and see all the stored data ..!
var_dump($wpcf7_data); // disable this line
// If you want to skip mailing the data..
$wpcf7_data->skip_mail = true;
}

Just an update. The following works in 4.1.1.
on_sent_ok: "location = 'http://domain.com/about-us/';"
demo_mode: on

Set skip_mail: on does the trick.

There might have been a change to contact-form-7, because I wasn't able to access the $skip_mail variable in the WPCF7_Submission object. I looked at the submission.php object in the \wp-content\plugins\contact-form-7\includes\submission.php file and found this:
private $skip_mail = false;
Since the variable is private, and there are no getters or setters in the file, you're not going to be able to change it externally. Just change it to this:
public $skip_mail = false;
and then you can change the variable like this in your functions.php file:
add_filter('wpcf7_before_send_mail', 'wpcf7_custom_form_action_url');
function wpcf7_custom_form_action_url( $form)
{
$submission = WPCF7_Submission::get_instance();
$submission->skip_mail = true;
}
A reminder, if you update the contact-form-7 plugin, it will probably nullify your change, so keep that in mind.

Simple code
Copy and paste the following code in your activated theme functions.php file.
add_filter('wpcf7_skip_mail','__return_true');

UPDATE WPCF7 ver. 7.5: There is now a filter specifically to handle this.
function my_skip_mail($f){
$submission = WPCF7_Submission::get_instance();
$data = $submission->get_posted_data();
if (/* do your testing here*/){
return true; // DO NOT SEND E-MAIL
}
}
add_filter('wpcf7_skip_mail','my_skip_mail');

Related

How to Make a PHP Variable Return Text Instead of Nothing

My code needs to see and use the text held in a variable:
I'm working on a WordPress plugin. One section of it needs to place text above the website's login form. If I just type in text as in the code below (return "My New Header Text";) it works fine and "My New Header Text" appears above the form.
// Add a CUSTOM MESSAGE to the top of the WordPress login form
function new_plugin_login_message( $message ) {
if ( empty($message) ){
return "My New Header Text";
} else {
return $message;
}
}
add_filter( 'login_message', 'new_plugin_login_message' );
But when I use a variable that includes the text, as in: (return "$MyNewMessage";) it won't work. Below is what I have. Even with lots of experimentation and Googling, I can't find a way for it to actually take the text stored in the variable and place it above the form. How do I make it see and use the text that is in the variable, "$MyNewMessage"?
// Add a CUSTOM MESSAGE to the top of the WordPress login form
function new_plugin_login_message( $message ) {
if ( empty($message) ){
return "$MyNewMessage";
} else {
return $message;
}
}
add_filter( 'login_message', 'new_plugin_login_message' );
This must be easy, but it eludes me. Thanks for any assistance.

Gravity forms, checking for duplicate entry and redirecting to that post after once submission fails

I have a gravity form that creates a custom post. On success, I managed to redirect the user's browser to the new post.
However, when the validator fails because the user is typing the same data on the form, I would like them to be redirected to the already existing post.
I've been researching, this may be possible to achieve with the filter "gform_validation". But im not sure how the logic here would work.
Here is my approach, currently not working.
add_filter( 'gform_validation_1', 'custom_validation' );
function custom_validation( $validation_result ) {
$form = $validation_result["form"];
$entry = $_POST["input_21"];
if($entry != null ){
// Do nothing
}
else{
$validation_result["is_valid"] = false;
header("Location: http://www.anypage.com"); /* Redirect browser to some page */
}
$validation_result["form"] = $form;
return $validation_result;
};
Currently when the form fails, it keeps redirecting to the homepage.
I'll appreciate any help on this. Thanks.

dokuwiki - bypass core or template function from template

I would like to bypass core and plugin functions to customize them.
I didn't succeed to do it from template.
I try to add into my tpl_functions.php something like:
if (!function_exists('html_buildlist')) {
function html_buildlist($data,$class,$func,$lifunc='html_li_default',$forcewrapper=false){
// etc.
}
}
My first idea is to check if the page has been visited and then customize the indexmenu plugin.
For example, i make this function to check if a page has been visited:
function wt__pagevisited($id){
if ($id == null) {
global $INFO;
$id = $INFO['id'];
}
// get cookie session info
$crumbs = isset($_SESSION[DOKU_COOKIE]['bc']) ? $_SESSION[DOKU_COOKIE]['bc'] : array();
// check ID into breadcrumb
if( array_key_exists($id,$crumbs) ) {
return true;
}
return false;
}
Any help will be appreciated.
Thank you in advance.
Jean-baptiste
What you're asking has nothing to do with DokuWiki. You want to replace PHP functions. That's not possible without the help of certain PHP extensions. See Is it possible to replace a function in php (such as mail) and make it do something else? for more info.

Global variables vanish in WordPress plugin

I'm writing a WordPress plugin. I want to display a custom message after a post is saved. This message will depend on the outcome of function called when the post is saved.
Here's my code:
add_action('save_post', 'my_save_post_function');
function my_save_post_function() {
global $msg;
$msg = "Foo bar";
...
}
add_filter('post_updated_messages', 'my_post_updated_messages_function');
function my_post_updated_messages_function($messages) {
global $msg;
$messages["post"][1] = $msg; // !! $msg is undefined !!
...
}
Why is $msg undefined?
Is there any way I can get a result out of a save_post action? I've tried all sorts of tricks. Even the $_POST data seems to have been blown away by the time admin messages are shown.
have you tried session ? i think your problem will be fixed .
take a look at :
http://www.php.net/manual/en/function.session-start.php

Drupal how to redirect node form after form submission

On Drupal 7 when I post a node I redirect to the specific node created.
I'm searching to redirect to the main admin page when I post correctly the node.
I've tried to put this on template.php:
function node_submit($form, &$form_state) {
$form_state['redirect'] = 'admin';
}
But there was an error on submit:
Fatal error: Cannot redeclare node_submit() (previously declared in /var/www/XXX/modules/node/node.module:1004) in /var/www/XXX/sites/all/themes/XXX/template.php on line xx
If all you want to do is change where a user redirects to after they submit an add node form, from a specific link, there is a much easier way.
Just make your link look like this:
/node/add/[CONTENT-TYPE]?destination=[URL-REDIRECT]
Here is an example that I got working:
/node/add/ic-competencies-toolkit-codes?destination=admin/survey-codes
This worked for me:
function mymodule_form_FORM_ID_alter(&$form, $form_state){
$form['actions']['submit']['#submit'][] = 'mymodule_redirect_callback';
}
function mymodule_redirect_callback($form, &$form_state){
$form_state['redirect'] = '_path_';
}
An important point to note is:
$form['actions']['submit']['#submit'][] = 'some_function';
will work, but
$form['#submit'][] = 'some_function';
will not
Alternatively, if you do not wish to write code, try the Rules Module: http://drupal.org/project/rules
Add a new rule and set the "React on Event" to be "After saving new content".
Set the action to be: "System: Page Redirect" and fill the fields out appropriately.
If you wish to get this rule into code, they can be exported down to a module!
You can use hook_form_alter() to do that.
function YOUR_MODULE_form_alter(&$form, &$form_state, $form_id)
{
if ($form_id == "CONTENT_TYPE_node_form") {
$form['#redirect'] = "node";
}
}
Hope this works.
In Drupal 7 this is the correct way:
function mymodule_form_alter(&$form, &$form_state, $form_id) {
if($form_id === 'myform_id'){
$form['#submit'][] = '_mymodule_redirect_callback';
}
function _mymodule_redirect_callback($form, &$form_state) {
$form_state['redirect'] = 'http://www.google.ch';
}
Simplest way to redirect comment form or node form
case 'my_node_form':
$form['#action'] .= '?destination=well-done';
break;
Please note the "." after $form['#action']
You need to append not replace!

Categories