So I want to BCC all outgoing emails from my wordpress site to two static, hard coded email addresses.
I went to the pluggable.php file and hard coded the BCC headers to the wp_mail() function like this:
function wp_mail( $to, $subject, $message, $headers = ['Bcc: example#mail.com', "bcc: maik#gmail.com"], $attachments = array() ) {
But nothing seems to be happening.
What am I missing?
Please do not edit core WordPress files!
Instead, use the relevant hook like wp_mail in your case.
Here's an example and this code would be added into the theme functions file, or you can add it as a Must Use plugin:
add_filter( 'wp_mail', 'my_wp_mail_args' );
function my_wp_mail_args( $args ) {
// Just replace the email addresses with the correct ones. And note that you
// don't have to add multiple Bcc: entries - just use one Bcc: with one or
// more email addresses separated by a comma - Bcc: <email>, <email>, ...
if ( is_array( $args['headers'] ) ) {
$args['headers'][] = 'Bcc: example#mail.com, maik#gmail.com';
} else {
$args['headers'] .= "Bcc: example#mail.com, maik#gmail.com\r\n";
}
return $args;
}
PS: If you added that as a Must Use plugin, don't forget to add the <?php at the top.
And BTW, just to explain the "nothing seems to be happening", it's because the $headers (fourth parameter) value there can be overridden when the function is called, e.g. wp_mail( 'foo#example.com', 'test', 'test', [ 'From: no-reply#example2.com' ] ) — the $headers is set, hence the default value is not used.
So I hope this answer helps, and keep in mind, never edit any core WordPress files! First, because in many WordPress functions (and templates as well) there are hooks that you can use to modify the function/template output, and secondly, your edits will be gone when WordPress is updated. 🙂
Related
i am a real very newbie in coding and in Wordpress. Trying my first test plugin to understand basics. I am able to define plugin, register it, so I can see it in plugins, activate it.
My later goal is to be able to create custom form, save user-specific data to new DB table, and then enable reading/editing it.
I tried to follow the instructions from gmazzap placed here: https://wordpress.stackexchange.com/questions/113936/simple-form-that-saves-to-database
I just am having following error from WP in time of trying to display preview of new screen having a shortcode [userform] in it:
*Fatal error: Uncaught Error: Call to undefined function submit_button() in /data/web/virtuals/131178/virtual/www/subdom/test/system/wp-content/themes/twentytwentythree-child/functions.php:14
*
My functions.php of my theme now looks like this:
<?php
add_action('init', function() {
add_shortcode('userform', 'print_user_form');
});
function print_user_form() {
echo '<form method="POST">';
wp_nonce_field('user_info', 'user_info_nonce', true, true);
?>
All your form inputs (name, email, phone) goes here.
<?php
submit_button('Send Data');
echo '</form>';
}
add_action('template_redirect', function() {
if ( ( is_single() || is_page() ) &&
isset($_POST['user_info_nonce']) &&
wp_verify_nonce($_POST['user_info_nonce'], 'user_info')
) {
// you should do the validation before save data in db.
// I will not write the validation function, is out of scope of this answer
$pass_validation = validate_user_data($_POST);
if ( $pass_validation ) {
$data = array(
'name' => $_POST['name'],
'email' => $_POST['email'],
'phone' => $_POST['phone'],
);
global $wpdb;
// if you have followed my suggestion to name your table using wordpress prefix
$table_name = $wpdb->prefix . 'my_custom_table';
// next line will insert the data
$wpdb->insert($table_name, $data, '%s');
// if you want to retrieve the ID value for the just inserted row use
$rowid = $wpdb->insert_id;
// after we insert we have to redirect user
// I sugest you to cretae another page and title it "Thank You"
// if you do so:
$redirect_page = get_page_by_title('Thank You') ? : get_queried_object();
// previous line if page titled 'Thank You' is not found set the current page
// as the redirection page. Next line get the url of redirect page:
$redirect_url = get_permalink( $redirect_page );
// now redirect
wp_safe_redirect( $redirect_url );
// and stop php
exit();
}
}
});
Note: I have not got to DB exercise, point of my question is submit_button.
As indicated in the error, the code on line 1 points to non identified function:
submit_button('Send Data');
I understood from other discussions submit_button should be core function of WP, so I should be able to call it "directly", without the need of a definition.
I tried following:
originally had very similar code within the plugin, moved to functions.php
reinstalled core of WordPress version 6.1.1
tried several different Themes (as it looked this worked for other users, I tried "classic" and "twentytwentythree" )
And still no little step further, still having same issue with error described above. What I am doing wrong?
If someone would confirm this is WP core installation issue, I am ready to reinstall WP from scratch, just trying to save some time, if there might be other cause.
Thank you for any suggestions.
I need to send a copy of the order email to a second email adress defined by a custom field.
I have installed the iconic custom account field plugin and created a custom field on the user (I works and I can edit it in admin).
https://iconicwp.com/blog/the-ultimate-guide-to-adding-custom-woocommerce-user-account-fields/
It's named 'iconic-register-email'
I have also implemented this https://wordpress.stackexchange.com/questions/92020/adding-a-second-email-address-to-a-completed-order-in-woocommerce solution to send an extra email. It works with a hard-coded email.
Now I need to combine the solutions and I just can't get it to work. I have appended the send email code at the bottom of the iconic plugin code.
add_filter( 'woocommerce_email_headers', 'mycustom_headers_filter_function', 10, 2);
function mycustom_headers_filter_function( $headers, $object ) {
$sae = 'a-custom-email#gmail.com';
if ($object == 'customer_completed_order') {
$headers .= 'BCC: Name <' . $sae . '>' . "\r\n";
}
return $headers;
}
This code works, but I need to use iconic-register-email field.
Need to fetch the registred email adress on a user, so when that user makes an order an order copy is sent to that adress defined by iconic-register-email.
My knowledge about coding is very limited, please help. Last thing to add before I can go live.
Managed to solve it. Not sure if it's the most elegant solution, but it works.
As it's predefined functions and arrays, this is how I did it
function mycustom_headers_filter_function( $headers, $object ) {
$sae = iconic_get_userdata( get_current_user_id(), 'iconic-register-email' );
if ($object == 'new_order') {
$headers .= 'BCC: <' . $sae . '>' . "\r\n";
}
return $headers;
}
So I'll start out by saying, that I am a bit new to this.
So I have this website I'm currently making. It's a guild website for World of Warcraft, and we want to be able to have new people being able to apply for membership.
Making the contact form is easy enough through plugins, but this is in theory what I wish to make:
A contact form where when filled in, the application form will push a notification to a webhook set in Discord where when new applicants happen, a message in a channel will be made, notifying the leaders about it.
Do I need to create a plugin myself, or is there any plugin that can offer this functionality?
I had the same needs, after sometime i found a way to do it.
Its actually very simple with WPForms.
WPForms has hooks so you can easily track forms submitions with the wpforms_process_complete hook. This hook allows you to track ALL WPForms sumbission. But maybe you'd like to have different forms. If you wish to track only a specific form, you can add the form id to the end of the hook name.
In my case i had many different forms which are being handled in various different ways, so i had to split them. When a form is being created in WPForms, it receives an ID so does the fields of the named form.
In my case after my form was created it had the following id :
The hook function.
As explained on the Discord Webhook page, Webhooks are a low-effort way to post messages to channels in Discord. They do not require a bot user or authentication to use. The endpoint supports both JSON and form data bodies. In my case i went for JSON.
As explained here you just need to use one of the the content file or embeds field. On this example i will just send a message, so i'll be using the content field.
Once the above instructions applied, you should end up with something close to the following function :
if ( ! function_exists( 'discord_form_submission' ) ) :
/**
* This will fire at the very end of a (successful) form entry.
*
* #link https://wpforms.com/developers/wpforms_process_complete/
*
* #param array $fields Sanitized entry field values/properties.
* #param array $entry Original $_POST global.
* #param array $form_data Form data and settings.
* #param int $entry_id Entry ID. Will return 0 if entry storage is disabled or using WPForms Lite.
*/
function discord_form_submission( $fields, $entry, $form_data, $entry_id )
{
// You have to replace this url by your discord webhook.
$endpoint = 'https://discord.com/api/webhooks/{webhook.id}/{webhook.token}';
// This is the content you can put anything you wish.
// In my case i needed the Name, Class, and the Level of the players.
$content = "**Name :** " . $fields[1]['value'] . PHP_EOL;
$content .= "**Class :** " . $fields[2]['value'] . PHP_EOL;
$content .= "**Level :** " . $fields[3]['value'] . PHP_EOL;
// WP has its own tool to send remote POST request, better use it.
wp_remote_post( $endpoint , [
'headers' => [
'Content-Type' => 'application/json; charset=utf-8'
],
'body' => wp_json_encode([ // Same for the JSON encode.
'content' => $content,
]),
'method' => 'POST',
'data_format' => 'body'
]);
}
endif;
This function must be added in the functions.php file of your theme.
Last but not least, with the help of the WP add_action Function you need to hook up with the wpforms_process_complete hook. In my case since i want to only hook up to the form with the id 1862 i have added the id at the end of the hook which gives us the following code :
add_action( 'wpforms_process_complete_1862', 'discord_form_submission', 10, 4 );
This code must be added in the functions.php file of your theme after our newly added function.
How can I add BCC or CC to the woo commerce, new order email?
I think this is the code that is running the new order emails: http://codepad.org/kPTpSIM0
I cant seem to find what I need on Google.
Thank you in advance.
I found this but I do not know where it goes:
add_filter( 'woocommerce_email_headers', 'add_bcc_all_emails', 10, 2);
function add_bcc_all_emails($headers, $object) {
$headers = array();
$headers[] = 'Bcc: Name <me#email.com>';
$headers[] = 'Content-Type: text/html';
return $headers;
}
Well it looks like you have already read this answer as that is the recommended code, though I believe it will add the BCC on all emails and not just new orders.
Instead I would suggest the following:
add_filter( 'woocommerce_email_headers', 'add_bcc_all_emails', 10, 3);
function add_bcc_all_emails( $headers, $email_id, $order ) {
if( 'new_order' == $email_id ){
$headers .= "Bcc: Name <me#email.com>" . "\r\n";
}
return $headers;
}
As for "where the code goes", it should be made into a plugin. Depending on how easily you would like to be able to disable this code in the future you could write it as a regular plugin (disable from Plugins overview) or as a site-specific snippets plugin (permanent until you delete file via FTP).
Found a templorary fix, It's not a BCC or CC, but It seems I can add more than 1 recipient to the email by comma separating the addresses.
would still like to do BCC or CC as well though, if anyone knows how.
In the Wordpress Settings API, creating a new options page usually starts out with
register_setting('sample_options', 'my_option');
add_settings_section('section', 'Sample Options', 'callback1', 'page');
add_settings_field('name', 'Label', 'callback2', 'page', 'section');
In this simplified example, the data gets saved in the option my_option making the value of name accessible through
$option = get_option('my_option');
$name = $option['name']; // Got it
But what if the value of the name field is there not to place a new value but to update an already existing option that's not my_option like for example this_other_option? I guess what I'm really looking for is is it possible for one field to save to multiple options (my_option and this_other_option) while using the Settings API?
I suppose you could use a callback in register_setting. It would look something like the following:
<?php
register_setting('sample_options', 'my_option', 'my_sanitize_callback');
function my_sanitize_callback($value, $option) {
$value = mysql_real_escape_string($value);
update_option('my_other_option', $value);
return $value;
}
?>
You may have to tweak that a bit. I haven't tested it.