Marian Rick Marian Rick - 3 months ago 12
PHP Question

How to hook inside a function in wordpress

I got a plugin (kind of sign up form), that offers developers some actions/hooks to add their own stuff. Inside the plugin the function is called like this:

// Allow devs to hook in
do_action( 'after_record_action', $result, $data, $format );


I guess that
$data
is an array storing the form data. After a visitor uses the signup form I want to send a mail containing
$data
using
wp_mail()


How can I execute the following script using
after_record_action
? Do I need to add this inside my
functions.php
?

// get data from $data[] array
$data['email'] = $email;
$data['key'] = $key;

// use $data to create a personalized mail
$to = $email;
$subject = "Wordpress Test";
$content = "Hi, this us your key:" . $key . "Enjoy using it!";

// send mail using wp_mail
$status = wp_mail($to, $subject, $content);


I appreciate any help in combining these, as I am not too experienced using php.

Answer

Add following code into your currently active theme's functions.php file:

add_action('after_record_action', 'marian_rick_custom_action', 10, 3);
function marian_rick_custom_action ($result, $data, $format){
// get data from $data[] array
$email = $data['email'];
$key = $data['key'];

// use $data to create a personalized mail
$to = $email;
$subject = "Wordpress Test";
$content = "Hi, this us your key:" . $key . "Enjoy using it!";

// send mail using wp_mail
$status = wp_mail($to, $subject, $content);
}

if you are interested, how all this really works, check the official docs here

Comments