Wordpress Contact form 7 add_filter() not working? - wordpress

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...

Related

How to properly use CF7 hooks

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.

user_register hook doesn't fire

I have designed a plugin and in a part of its functionalities I'm trying to save some data to a custom table in MySQL based on the ID of new registered user in Wordpress registration form. I used user_register hook as follow:
function my_func($user_id){
// some DB Query code based on $user_id
}
add_action('user_register', 'my_func', 10, 1);
I already tried solution in Wordpress user_register hook not executing?
and used profile_update hook as described there
but it couldn't help. I also tried changing priority value and removed it but the problem still exists.
The only thing I want is to have access to ID of the new registered user in registration form.
I appreciate if someone could help me.
I finally could find the solution. Actually in the body of myfunc function in the question I was trying to output the $user_id using javascript alert and for some reason that script doesn't run at that point but my query runs successfully and the way that I used the user_register hook is correct.
I just want to share it in case anybody encountered such a problem.
Thanks.

Using Nonce-field in Custom Meta Box plugin, unable to save data

I have been struggling to create a plugin that would allow my customer to update their contact info. I've managed have the plugin show the fields in the admin-area of the frontpage (only want to show the info there). As I fill in the information & press update post, fields go blank and nothing gets saved.
I believe I've isolated the cause to the nonce-field, tested by having some of the fields in an if-statement where I tested for isset & wp_verify_nonce. The fields disappeared, thus the nonce isn't working correctly. What am I doing wrong?
This is my first Wordpress-site that I'm working on so the solution of using multiple fields might not be the most clever but seemed the most simple to wrap my head around.
As a side question, am I right in assuming that I can echo the contents of a field (for example the 'h3_nimi' by using echo get_post_meta( get_the_ID(), 'h3_nimi', true );?
Hopefully it's ok to use pastebin to avoid cluttering the post as the code is rather long due to multiple fields? Thank you in advance.
http://pastebin.com/fqRW2Yyx
Your nonce is created as tt-tallennus but you are checking against tt_henkilosto so the check will always fail.

wordpress add_action('save_post', 'my_function) not working

I am trying to trigger an even upon saving / updating a post in wordpress... see here:
add_action('save_post', 'generate_location');
function generate_location($post_id) {
echo "hey";
}
the problem is that its not working...
any ideas why? Syntax?
WordPress implements the Post/Redirect/Get pattern to avoid duplicate form submissions, so you're not going to see anything echo'd from a save_post callback.
Instead, you can do a wp_die( 'hey' ) instead, or log something to the database or file system.
I don't know whether you got this working, but I was having the same issue and discovered how to fix it!
in wp-includes/post.php on line 2940 (at the time of writing), this if/else is run whilst saving a post:
if ( !empty($page_template) && 'page' == $data['post_type'] ) {
You will notice that, if there is an error with the template the function stops there and save_post is never called.
In my case, the posts I was trying to save were imported from a pre-existing site. The new site had no page templates at all, so WP was still trying to save the page with the template from before, failing, and thus; save_post was never called.
I added
/* Template Name: Default Template */
to page.php, bulk edit, selected the template and saved. Remove the template name from page.php (as it shows up twice(, and now save_post is triggered every time.
This was the solution in my case anyway. i'm sure it'll affect someone else, somewhere down the line.

WordPress functions.php: how to apply update_option()?

I'm trying to set the default Image Link URL for my WP users so that it doesn't include the url link as a default. I've done some research, and I know the function is in the wp-admin/options.php:
update_option('image_default_link_type','file');
Rather than mess with the core files, I'd like to put this into the functions.php, but never know the proper way to implement stuff like this! This is what I have so far in my functions.php:
<?php
update_option('image_default_link_type','none');
?>
This obviously doesn't work: it needs the proper setup! What is the correct way to implement this in functions.php?
Also: I'd like to know the strategy for figuring out how to implement functions like this in the future by myself? For example, I never know whether or not I'm supposed to use add_filter or do_action, and how I need to pass the parameters. I've yet to find a book or post out there that explains this very well, and can show me by example. Any good leads on this would be awesome too!
Start with the Wordpress codex. Visit the plugin API (which is really what you are doing) that explains Hooks, Actions and Filters. Then see the Action Reference which provides your list of hooks.
Here you will find the hook update_option_OPTIONNAME. Description from codex:
Runs after a WordPress option has been update by the update_option
function. Action function arguments: old option value, new option
value. You must add an action for the specific options that you want
to respond to, such as update_option_foo to respond when option "foo"
has been updated.
Adding code from asker's comment:
function inventory_linkurl_setting() {
update_option('image_default_link_type','none');
}
add_action('admin_init', 'inventory_linkurl_setting'); ?>

Resources