How to send an email to a mailchimp Automation on wordpress post publish - wordpress

In wordpress, I'm trying to POST a specific email included as a value of a custom field (called customer_email) in each post to a mailchimp automation.
Below is the code I have and it's not working
function email_on_publish( $postid ) {
$api_key = API-KEY;
$workflow_id = WORKFLOW-ID;
$email_id = EMAIL-ID;
$email = get_post_meta( get_the_ID(), 'customer_email', true );
$args = array(
'method' => 'POST',
'headers' => array(
'Authorization' => 'Basic ' . base64_encode( 'user:'. $api_key )
),
'body' => json_encode(array(
'email_address' => $email
))
);
$response = wp_remote_post( 'https://' . substr($api_key,strpos($api_key,'-')+1) . '.api.mailchimp.com/3.0/automations/' . $workflow_id . '/emails/' . $email_id . '/queue/' . md5(strtolower($email)), $args );
$body = json_decode( $response['body'] );
}
add_action( 'pending_to_publish', 'email_on_publish' );
add_action( 'draft_to_publish', 'email_on_publish' );

Update: This has been resolved. There were two issues at hand.
I didn't need "md5(strtolower($email)" at the end of the post url.
Mailchimp requires you to send all prior emails in the workflow to the user before adding them to an automation. This was rendering all of my testing useless until I learned that.
Thanks for figuring this out, self!

Related

update mailchimp email subscription is not working

Below is my code to create or update subscription in Mailchimp.
function mailchimp_ajax_subscription()
{
$data = isset( $_POST['formData'] ) ? $_POST['formData'] : array();
ob_start();
if(count($data) > 0)
{
$api_key = 'XXXXXXXXXXXXXX';
$status = 'unsubscribed'; // subscribed, unsubscribed, cleaned, pending
$args = array(
'method' => 'PUT',
'headers' => array(
'Authorization' => 'Basic ' . base64_encode( 'user:'. $api_key )
),
'body' => json_encode(array(
'email_address' => $data["email"],
'status' => $status,
'tags' => array($data["name"])
))
);
$response = wp_remote_post( 'https://' . substr($api_key,strpos($api_key,'-')+1) . '.api.mailchimp.com/3.0/lists/XXXXX/members/' . md5(strtolower($email)), $args );
$body = json_decode( $response['body'] );
if ( $response['response']['code'] == 200 && $body->status == $status ) {
echo 'The user has been successfully ' . $status . '.';
} else {
echo '<b>' . $response['response']['code'] . $body->title . ':</b> ' . $body->detail;
}
}
wp_die();
}
Using above code, I can create subscription into Mailchimp but If I enter same email to edit the tags/status it gives me error.
Error:
400Member Exists: abc#xyz.com is already a list member. Use PUT to
insert or update list members.
I already used PUT in the code so what is missing?
You can use TAGS API to update the TAGS. Below is the sample code to update the TAG:
$postdata = array(
'apikey' => $api_key,
'email_address' => $userData["email"],
'tags' => array(
array(
'name' => $userData["name"],
'status' => 'active'
),
)
);
$mch_api = curl_init(); // initialize cURL connection
curl_setopt($mch_api, CURLOPT_URL, 'https://' . substr($api_key,strpos($api_key,'-')+1) . '.api.mailchimp.com/3.0/lists/' . $list_id . '/members/' . md5(strtolower($userData['email']))."/tags");
curl_setopt($mch_api, CURLOPT_HTTPHEADER, array('Content-Type: application/json', 'Authorization: Basic '.base64_encode( 'user:'.$api_key )));
curl_setopt($mch_api, CURLOPT_RETURNTRANSFER, true); // return the API response
curl_setopt($mch_api, CURLOPT_TIMEOUT, 10);
curl_setopt($mch_api, CURLOPT_POST, true);
curl_setopt($mch_api, CURLOPT_POSTFIELDS, json_encode($postdata) ); // send data in json
$result = curl_exec($mch_api);

How to properly save a WordPress option containing HTML code?

Here's a screenshot of what the problem looks like:
And the HTML of that part of the WordPress options page looks like this:
So, in the WordPress admin area, I entered a piece of HTML code (to have a clickable link as the output on the frontend).
This was the code I had entered into that text input field on the backend:
<a title="Website Design London" href="../../website-design/">Website Design</a>
And while on the frontend that link is displaying OK, I'm seeing this mess (see screenshot) on the backend.
As far as I can tell the relevant PHP code is this:
$this->text(
'workdone',
esc_html__( 'Work done', 'mytheme' )
);
So, what is the proper way to save an option that contains HTML code?
And how can I fix the mess shown on the screenshot?
I had the same issue, and this code is working for me:
// render service label
public function render_service_label() {
$value = get_option( 'wbk_service_label', '' );
$value = htmlspecialchars( $value );
$html = '<input type="text" id="wbk_service_label" name="wbk_service_label" value="'.$value.'" >';
$html .= '<p class="description">' . __( 'Service label', 'wbk' ) . '</p>';
echo $html;
}
// validate service label
public function validate_service_label( $input ) {
$allowed_tags = array(
//formatting
'strong' => array(),
'em' => array(),
'b' => array(),
'i' => array(),
'br' => array(),
//links
'a' => array(
'href' => array(),
'class' => array()
),
//links
'p' => array(
'class' => array()
)
);
$input = wp_kses( $input, $allowed_tags );
return $input;
}
So, in the dashboard options page I use htmlspecialchars function
In frontend page I use like this:
$label = get_option( 'wbk_service_label', __( 'Select service', 'wbk' ) );

Custom WooCommerce Gateway

I`m trying to develop a custom payment gateway for WooCommerce plugin, but I have a problem with the checkout page. What I want is to insert a form on the checkout final step page that is submitted automatically after 5 seconds.
My code is:
...
add_action('woocommerce_receipt_' . $this->id, array($this, 'receipt_page'));
add_action('woocommerce_api_wc_' . $this->id, array($this, 'handle_callback'));
}
function handle_callback() {
wp_die('handle_callback');
}
function receipt_page( $order )
{
echo "receipt page";
$this->generate_submit_form_elements( $order );
}
The problem is that "receipt_page" action is not triggered.
Thanks!
oh, its because you forgot to set the has_fields flag to true.
//after setting the id and method_title, set the has_fields to true
$this -> id = 'kiwipay';
$this -> method_title = 'KiwiPay';
$this->has_fields = true; // if you want credit card payment fields to show on the users checkout page
then in the process_payment function put this:
// Payload would look something like this.
$payload = array(
"amount" => $order.get_total(),
"reference" => $order->get_order_number(),
"orderid" => $order->id,
"return_url" => $this->get_return_url($order) //return to thank you page.
);
response = wp_remote_post( $environment_url, array(
'method' => 'POST',
'body' => http_build_query( $payload ),
'timeout' => 90,
'sslverify' => false,
) );
// Retrieve the body's response if no errors found
$response_body = wp_remote_retrieve_body( $response );
$response_headers = wp_remote_retrieve_headers( $response );
//use this if you need to redirect the user to the payment page of the bank.
$querystring = http_build_query( $payload );
return array(
'result' => 'success',
'redirect' => $environment_url . '?' . $querystring,
);

Contact Form 7 and Custom post type

I want to use contact form 7 in Wordpress to build a order Form. I want the content of the order Form to be populated with content from a custom post type "trade Show Material" - The post type contains the fields "name" "number" "description" "photo" . The idea will be that each piece can be selected from the form . Can anyone offer the general direction for this? Should I perhaps be using another plugin entirely?
Maybe you can use the wpcf7_form_tag filter hook for this.
If you want to use a custom post type as the options of a dropdown (select) you can add something like the example below in your functions.php:
function dynamic_field_values ( $tag, $unused ) {
if ( $tag['name'] != 'your-field-name' )
return $tag;
$args = array (
'numberposts' => -1,
'post_type' => 'your-custom-post-type',
'orderby' => 'title',
'order' => 'ASC',
);
$custom_posts = get_posts($args);
if ( ! $custom_posts )
return $tag;
foreach ( $custom_posts as $custom_post ) {
$tag['raw_values'][] = $custom_post->post_title;
$tag['values'][] = $custom_post->post_title;
$tag['labels'][] = $custom_post->post_title;
}
return $tag;
}
add_filter( 'wpcf7_form_tag', 'dynamic_field_values', 10, 2);
In your form you can add the field:
[select* your-field-name include_blank]
In the example above the post_title is used in the options of the dropdown. You can add your own fields here (name, number, description, photo).
I do no think the wpcf7_form_tag works in the same way as vicente showed in his great answer before. It may have changed since 2015.
If you read here it explains how you need to use the wpcf7_form_tag: https://contactform7.com/2015/01/10/adding-a-custom-form-tag/
With that in mind along with this other post from Contact Form 7: https://contactform7.com/2015/02/27/using-values-from-a-form-tag/#more-13351
I came up with this code to create a custom dropdown list for a custom post type that I have.
add_action( 'wpcf7_init', 'custom_add_form_tag_customlist' );
function custom_add_form_tag_customlist() {
wpcf7_add_form_tag( array( 'customlist', 'customlist*' ),
'custom_customlist_form_tag_handler', true );
}
function custom_customlist_form_tag_handler( $tag ) {
$tag = new WPCF7_FormTag( $tag );
if ( empty( $tag->name ) ) {
return '';
}
$customlist = '';
$query = new WP_Query(array(
'post_type' => 'CUSTOM POST TYPE HERE',
'post_status' => 'publish',
'posts_per_page' => -1,
'orderby' => 'title',
'order' => 'ASC',
));
while ($query->have_posts()) {
$query->the_post();
$post_title = get_the_title();
$customlist .= sprintf( '<option value="%1$s">%2$s</option>',
esc_html( $post_title ), esc_html( $post_title ) );
}
wp_reset_query();
$customlist = sprintf(
'<select name="%1$s" id="%2$s">%3$s</select>', $tag->name,
$tag->name . '-options',
$customlist );
return $customlist;
}
Then you use the tag in contact form 7 like this.
[customlist your-field-name]
Hopefully this helps someone else who was looking for a way to do this like I was.
You could alter it to get any information you need from the custom post type.
It does not have any validation though.
Clyde Thomas code still works nice, thanks !
In my case I need the data from a plugin instead of a post so I've modified the code removing the WP_query and the while
global $wpdb;
$result = $wpdb->get_results("SELECT title FROM wp_asl_stores ORDER BY title ASC ");
foreach($result as $row) {
$customlist .= sprintf( '<option value="%1$s">%2$s</option>',
esc_html( $row->title ), esc_html( $row->title ) );
}

Send request to other server in wordpress

I have two website in wordpress
Main site
1.)http://www.abc.com
Other Site
2.)http://www.xyz.com
From mainsite I want to send username and password to othersite for this i am using this code
$creds = array();
echo $creds['user_login'] = "sachin";
echo $creds['user_password'] = "pass";
$user = wp_signon( $creds, false );
// echo ABSPATH . WPINC;
if ( is_wp_error($user) ){
echo $user->get_error_message();
if( !class_exists( 'WP_Http' ) )
include_once( ABSPATH . WPINC. '/class-http.php' );
// You would edit the following:
$username = $creds['user_login']; // Twitter login
$password = $creds['user_password']; // Twitter password
$message = "Hey neat, I'm posting with the API";
// Now, the HTTP request:
$api_url = 'http://www.xyz.com';
$body = array( 'status' => $message );
$headers = array( 'Authorization' => 'Basic '.base64_encode("$username:$password") );
$request = new WP_Http;
$result = $request->request( $api_url , array( 'method' => 'POST', 'body' => $body, 'headers' => $headers ) );
// echo"<pre>";print_r($result['body']);
I dont know i am sending wp http request in right way or not but my problem is i want this username and password(i am sending from main site) to other server (http://www.xyz.com)
How I can do that please suggest me.please provide me your valuable suggestion how I can send username and password from one server to other server.
Thanks
To share 2 wordpress installs so that users can access both then this page will let you do that.
http://www.remicorson.com/share-users-database-on-different-wordpress-installs/
I think this is what your trying to do?

Resources