1

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.

1

Using add_action I am able to add a function to the plugin.

// add action to after_record_action    
add_action('after_record_action', 'marian_rick_custom_action', 10, 3);

// add function
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);
}

As stated here by Tarun Mahashwari

  • Please don't forget to accept this as an answer after two days if it solved your original question. – bravokeyl Aug 28 '16 at 11:30
  • 2
    I indented the code correctly, but you should explain how it answers the question, there's a great opportunity to introduce the add_action function and how hooks work, instead you've given copy-pasta which helps nobody else and gives no further understanding – Tom J Nowell Aug 28 '16 at 12:44

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.