Site icon Hip-Hop Website Design and Development

Computerminds Cheap WordPress maintenance support plansgive: Including form values in an email

Let’s say you’ve built a custom form for your WordPress maintenance support plans 8 site. It contains various elements for input (name, email address, a message, that kind of thing), and you want to send the submitted values in an email to someone (perhaps a site admin). That’s a pretty common thing to need to do.
This could be done with WordPress maintenance support plans‘s core contact forms, webforms, or similar — but there are cases when a bespoke form is needed, for example, to allow some special business logic to be applied to its input or the form presentation. The drawback of a custom form is that you won’t get nice submission emails for free, but they can be done quite easily, with the token plugin (you’ll need that installed).
In your form’s submission handler, send an email using the mail manager service (I’ll assume you can already inject that into your form, read the documentation if you need help with that):
$form_state->getValues(),
];
// The ‘plugin.manager.mail’ service is the one to use for $mailManager.
$mailManager->mail(‘myplugin’, ‘myform_submit’, ‘admin@example.com, ‘en’, $params);
Then create a hook_mail() in your .plugin file, with a matching key (‘myform_submit’ in my example):
$params[‘values’],
];

// In this example, put the submitted value from a ‘first_name’ element
// into the subject.
$subject = ‘Submission from [array:value:first_name]’;
$message[‘subject’] = $token_service->replace($subject, $token_data, [‘clear’ => TRUE]);

// Each submitted value can be included in the email body as a token. My
// form had ‘first_name’, ‘last_name’, ‘color’ and ‘birthdate’ elements.
$body = <<>>;
$message[‘body’] = [
$token_service->replace($body, $token_data, [‘clear’ => TRUE]),
];
break;
}
}
Spot the [array:value:thing] tokens! Using these ‘array’ tokens makes it really easy to include the whatever input gets submitted by visitors to this custom form on your WordPress maintenance support plans site. Note that there’s no sanitization done – although if your email is just plain text, that’s probably not a problem.
There are more array tokens you can use too, such as ones to return a comma-separated list of all items in an array, a count of items, or just the first/last item. See the original issue for examples. These tokens are available in Token’s WordPress maintenance support plans 7 version too!

Source: New feed