How to properly use CF7 hooks - wordpress

I'm fairly new to Wordpress development so please excuse if my question is dumb.
I'm trying to call a REST API when CF7 is submitted. So I've tried to hook into both wpcf7_before_send_mail and wpcf7_mail_sent but to no avail. Whenever I put this code in my theme's functions.php file; the form doesn't submit and keeps on loading (like it's trying to submit)
For simplicity's sake, I tried to do a simple redirect as you see in the following code but that's not working either. The form works when I remove the following code from functions.php file.
add_action("wpcf7_mail_sent", "wpcf7_do_something_else");
function wpcf7_do_something_else($cf7) {
wp_redirct('https://google.com');
}
Please advise what I am doing wrong. Thank you very much!

It keeps loading because it's an ajax function and you are trying to use 'wp_redirect' in it and that won't work.
The 'wpcf7_mail_sent' hook is working fine, you can test that by using something like that
add_action("wpcf7_mail_sent", "wpcf7_do_something_else");
function wpcf7_do_something_else($cf7) {
var_dump($cf7);exit;
}
And while sending the form monitor the 'network' tab in the dev tools of your browser to see the results and you will find that it returns an object of the submitted data.

Related

Wordpress shortcode does not work

I'm trying to create a simple shortcode function that would work in a link like this:
href="http://www.website.com">[short_code]
Please note, I had to remove the a tag because it wouldn't show. I want to be able to change the anchor text of a link with a single edit across multiple pages/posts.
to accomplish this, this is what I've done:
I've added include 'myshortcodes.php'; to functions.php file and created a file called myshortcodes.php
within that file I have the following code:
<?php
add_shortcode('hw', 'hello');
function hello() {
return 'Click Here For More Info';
}
Now when I put [hw] in href="http://www.website.com">[hw] it does work on my local wp install, however, when I do the same on my live server, it doesn't work, no matter what I do. It returns [hw] instead of 'Click Here For More Info'. Any ideas why? Thanks!
Maybe problems with php interpreter on server side.
Check if your server side doing include right.
Below and above your shortcode function place echo with something, and if it will echoes on your site, then we need to dig deeper.
And if it will not echoes, than the problem is in include function.

Wordpress Contact form 7 add_filter() not working?

Can't seem to use add_filter for Contact Form 7.
Ultimately, I'm trying to use the code here to add custom validations: http://code-tricks.com/contact-form-7-custom-validation-in-wordpress/
But the add_filter calls don't seem to hook in where they are supposed to and nothing happens. Doing a simple ECHO test, I can see that the file is loading but no validation occurs.
Any idea what might cause this?
add_filter('wpcf7_validate_text','cf7_custom_form_validation', 10, 2); // text field
add_filter('wpcf7_validate_text*', 'cf7_custom_form_validation', 10, 2); // Req. text field
any specified cf7_custom_form_validation() function simply does nothing when form submitted. Even if I just have it echo some text or manipulate a variable. Nothing happens. The function doesnt' seem to get called.
Turns out the CF7 core code has been updated and some modifications are necessary to make custom validation work:
More details can be found here:
http://contactform7.com/2015/01/27/contact-form-7-41/
and here:
http://contactform7.com/2015/01/06/contact-form-7
I'll give these a try and mark this as answered if it all works out.
I dont think there is the problem with add_filter in your case.This might be because the incorrect id used for validation. Check the form id in the validation code whether it is correctly used or not.
FYI, the instructions on the following page are currently wrong - https://contactform7.com/2015/03/28/custom-validation/
The following code $tag->name
needs to read $tag[name], since apparently $tag is an array now...

Drupal Webform hook_webform_submission_insert not firing

I am trying to use the hook_webform_submission_insert in my theme template.php file. I have 2 other webform hooks currently running in here and they work just fine. I am trying to get the submission data after it has been submitted. Below is my code.
function acquarius_hook_webform_submission_insert($node, $submission){
var_dump($node);
var_dump($submission);
}
I am sure I am missing something small here but everything I try seems to fail.
You need to replace hook keyword with your theme name. Also add hook functions in module.
YOURTHEMENAME_webform_submission_insert($node, $submission){
// give your code here
}
After you create a new hook, you need to clear the drupal caches.
Go to YOURSITE.com/admin/config/development/performance
And click on "Clear all caches"

Fire an action right after Appearance > Theme Options has been saved

I just started working with Wordpress (v. 3.6.1).
I have OptionTree installed and as it seems it handles the Theme Options page. I want to run my function (in a plugin or wherever else) right after the user saves the changes of this page.
So far I found out that option-tree/includes/ot-settings-api.php generates the form and it sets the form action to options.php (which is a wordpress core file). I was thinking about change the action to my custom php file and handle the save procedure and finally runs my own function. But this solution looks pretty ugly.
I wonder if there's another way to get the job done.
Thanks.
Thanks to #Sheikh Heera link (tutsplus) I could find a solution.
I think this is some kind of hack and I still don't know if it is the best way. Anyway I did this:
Create a file your-theme-settings.php in your theme lib folder.
Let Wordpress knows about your file by adding this code in your theme functions.php:
include_once('lib/your-theme-settings.php');
Add this code to your-theme-settings.php:
function your_theme_register_settings() {
register_setting('option_tree', 'option_tree', 'your_theme_validate_options');
}
function your_theme_validate_options($input) {
// do whatever you have to do with $input.
}
add_action('admin_init', 'your_theme_register_settings');
In step 3, I put 'option_tree' as 1st and 2nd argument of register_settings function, because I noticed that the Option Group and Option Name of OptionTree plugin is option_tree.
I'm not sure if this is the best solution, so I would be glad if you shares your ideas.

Extending Contact Form 7 Wordpress plugin by using hooks

I would like to create a plugin that uses the contact form 7 hook, wpcf7_admin_after_mail. I want to use the plugin to interface with a CRM system. What I have thus far is the following:
//plugin header here
function add_to_CRM( $cf7 )
{
if (isset($cf7->posted_data["your-message"]))
{
full_contact($cf7);
} else {
quick_quote($cf7);
}
return $cf7;
}
add_action('wpcf7_admin_after_mail', 'add_to_CRM');
//other functions here
I can't seem to get this working. I can't even get the hook to work and do something like mail me. Anybody have any idea what I'm doing wrong here. Since I have limited Wordpress experience I might me missing the boat completely with what I'm trying to do here. I've Googled for answers to no end.
EDIT: I ended up adding this to the theme's functions.php file and it works perfectly. Thing is, I want to get it working as a plugin. Any help will be appreciated.
Try delaying the add_action() call, something like;
add_action('init', create_function('',
'add_action("wpcf7_admin_after_mail", "add_to_CRM");'));
This actually registers your CF7 hook once WordPress is ready (which is nearer the time functions.php gets loaded in).

Resources