I am using [_remote_ip] in the email I am reciveng from contact form 7, is there is a way to show the country name or flag instead of the received IP number?
The main issue started when I activated caching, where the php country locate stopped working, so I have to use the [_remote_ip] in the email to know the location of the contact, but it's not a good solution that I always have to locate the ip manually.
That's why I want to check if there is a way to show that IP as a name or as a flag.
This will add a form tag to Contact form 7 [country] - Add this to your functions.php or make it a plugin. TO use this, you'll need a subscription (free tier available) to https://ipstack.com.
add_action('wpcf7_init', function (){
wpcf7_add_form_tag( 'country' , 'cf7_ip_to_country_form_tag' );
});
function cf7_ip_to_country_form_tag($tag){
// Your API Key for https://ipstack.com is required
$api_key = 'xxxxxxx';
// Get API Response
$url = 'http://api.ipstack.com/' . $_SERVER['REMOTE_ADDR'] . '?access_key=' . $api_key;
$response = wp_remote_get($url);
$body = json_decode(wp_remote_retrieve_body($response));
/* $body = Object returned from ipstack.
* $body->country_name = Full Country Name
* $body->country_code = 2 Letter ISO Abbrev
* set the value based on what you want.
*/
return '<input type="hidden" name="country" value="'.$body->country_name.'">';
}
Related
WooCommerce does not include the customer's name in the To: field of the headers when sending out emails. I'm trying to add them myself with the WooCommerce email recipient filters, but I'm running into a bit of a roadblock.
The code is being triggered as expected, but the messages don't seem to be arriving.
Here's my current code:
function add_customer_name_to_email_header($recipient, $order) {
// Don't run if something else has already modified it and added a name somehow.
if (filter_var($recipient, FILTER_VALIDATE_EMAIL)) {
$first_name = $order->get_billing_first_name();
$last_name = $order->get_billing_last_name();
$recipient_name = trim($first_name . ' ' . $last_name);
$recipient_name = sanitize_text_field($recipient_name);
if (!empty($recipient_name)) {
$recipient = sprintf('"%s" <%s>', htmlspecialchars($recipient_name), $recipient);
}
}
return $recipient;
}
add_filter('woocommerce_email_recipient_customer_note', 'add_customer_name_to_email_header', 10, 2);
add_filter('woocommerce_email_recipient_customer_completed_order', 'add_customer_name_to_email_header', 10, 2);
add_filter('woocommerce_email_recipient_customer_invoice', 'add_customer_name_to_email_header', 10, 2);
add_filter('woocommerce_email_recipient_customer_processing_order', 'add_customer_name_to_email_header', 10, 2);
I've confirmed that it is properly generating the correctly-formatted value for that field, e.g.: "John Doe" <jdoe#example.com>, but messages don't actually arrive once the $recipient value has been modified.
My suspicions are that WooCommerce or WordPress are further sanitizing the recipient value before the email is sent, probably stripping out or modifying characters like < and >, but I don't know for sure. Is there any existing method for this I just haven't found, or a known way around this?
I wanna to add payment to wordpress . Payment system called PayBox. https://paybox.money/docs/ru/pay-in/3.3#tag/Inicializaciya-cherez-brauzer-polzovatelya/paths/~1payment.php/get . I trying to create form and send data with post method to php file and send request to payment with code below.
<?php
$request = [
'pg_merchant_id'=> 111,
'pg_amount' => 25,
'pg_salt' => 'some_random_string',
'pg_order_id'=>'123',
'pg_description' => 'Описание заказа',
'pg_result_url' => 'https://example.com'
];
// $request['pg_testing_mode'] = 1; //add this parameter to request for testing payments
//if you pass any of your parameters, which you want to get back after the payment, then add them. For example:
// $request['client_name'] = 'My Name';
// $request['client_address'] = 'Earth Planet';
//generate a signature and add it to the array
ksort($request); //sort alphabetically
array_unshift($request, 'payment.php');
array_push($request, 'secret_key'); //add your secret key (you can take it in your personal cabinet on paybox system)
$request['pg_sig'] = md5(implode(';', $request));
unset($request[0], $request[1]);
$query = http_build_query($request);
//redirect a customer to payment page
header('Location:http://core.local/payment.php?'.$query);
But problem is . Where can I handle this code? I dont know how to do this in wp. Form created with page builder .
You need to find out with your page builder that creates the form if they are able to handle hooks. See if there's a function you can hook into when the form is processed.
Generally this would go in your functions.php file of your theme (or a child theme if you're using a ready-made theme).
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.
Is there any mail-tags to use?
for instance; for tracking client's ip we can use; [_remote_ip]
First step is to create an API key. To get an API key we have to register in the site IPInfoDB.
Once API key is ready we have to download the file ip2locationlite.class.php from the site IPInfoDB.
Next step is to create our custom plugin.I named it as "country_custom_plugin.php". Its always good to create a custom plugin inside a folder, so that all the required files for the corresponding plugin stays in the folder. Named the folder as "country_custom_plugin"
Move the file "ip2locationlite.class.php" to the folder "country_custom_plugin".
/*Calling the function from contact-form-7 module and passing the result of the function stylus_ip_location_get_cc */
add_filter( 'wpcf7_special_mail_tags', 'country_custom_ip_location', 10, 2 );
/*Function to get location of an user from the ip address*/
function country_custom_ip_location( $output, $name ){
/*including the third party integration to get IP Location*/
include_once('ip2locationlite.class.php');
/*Special tag values are passed in format wpcf7.$name which we convert to _$name*/
$name = preg_replace( '/^wpcf7\./', '_', $name );
/*If location is requested in contact form enter the loop*/
if ( '_custom_ip_location' == $name ) {
$ipLite = new ip2location_lite;
/*Entering the API key value generated*/
$ipLite->setKey('"Enter your API Key Here"');
/*Getting the IP address*/
$ipaddress = preg_replace( '/[^0-9a-f.:, ]/', '', $_SERVER['REMOTE_ADDR'] );
/*Getting the Location*/
$visitorGeolocation = $ipLite->getCity($ipaddress);
if (!empty($visitorGeolocation) && is_array($visitorGeolocation)) {
$output = $visitorGeolocation['regionName'] . ', ' . $visitorGeolocation['countryName'] . ', ' . $visitorGeolocation['countryCode'];
}
}
return $output;
}
Reference.Hope this will help. Please let me know if any issue.
I've created a form with contact form 7, and I need to send two e-mails, one with all the data that I pick up from the form, another one with a "thank you" message and other important information, these informations are statics, all the same for every user that clicks in the submit button.
So my problem is this email, CF7 can send only one type of mail to multiple users, but not two differents mail to two differents users. The second mail needs to use the [your-mail] (the mail that the users write in the form).
I've discovered the on_sent_ok function that allows me to open a page or something else after the submit, but I have no idea how to send this different mail.
It's been a while from your request, but I write here my solution for future needs.
You can reach the goal by following 2 steps:
A) handle the sent mail Contact Form 7 event.
function your_wpcf7_mail_sent_function( $contact_form ) {
$title = $contact_form->title;
$submission = WPCF7_Submission::get_instance();
if ($submission && "New hospital" == $title) {
$posted_data = $submission->get_posted_data();
//$posted_data is an associative array that contains all the form input values,
//so you can use it to proceed with B step
}
}
add_action( 'wpcf7_mail_sent', 'wpcf7_mail_sent_event_triggered' );
If you're not sure on where place this code my suggestion is to add it at the beginning of your theme's function.php file.
B) send a custom mail using wp_mail API.
If you take a look at the docs you'll find that the API is quite easy to understand.
$to = 'sendto#example.com';
$subject = 'The subject';
$body = 'The email body content';
$headers = array('Content-Type: text/html; charset=UTF-8');
wp_mail( $to, $subject, $body, $headers );
Obviously you can customize the body message using or not the $posted_data values according to your needs.