How update ACF field in savepost hook and wp remote post - wordpress

How I can update post meta or ACF fields by wp_remote_post or other method by using exists API ?
function publish_content($post_id, $post, $update)
{
// If this is just a revision, don't send the email.
if (wp_is_post_revision($post_id)) {
return;
}
$full_content = get_field('full_content', $post_id);
$login = 'username';
$password = 'password';
$response = wp_remote_post(
'https://example.com/wp-json/wp/v2/posts/12',
array(
'headers' => array(
'Authorization' => 'Basic ' . base64_encode("$login:$password")
)
)
);
//I need Update ACF Field Value in example.com site
}
add_action('save_post', 'publish_content', 11, 3);
I don't want to develop new API on the destination site and I want to use the existing (wp/v2/posts/{id}) WordPress API.

Related

How can I update Wordpress plugin with transient by add authorize_token to header

I am trying to update my WordPress plugins automatically using transient.
I have one that works but it adds authorize_token to query params like below code.
$new_files = $this->github_response['zipball_url']; // Get the ZIP
***$package = add_query_arg(array("access_token" => $this->authorize_token), $new_files);***
$slug = current( explode('/', $this->basename ) ); // Create valid slug
$plugin = array( // setup our plugin info
'url' => $this->plugin["PluginURI"],
'slug' => $slug,
'package' => $package,
'new_version' => $this->github_response['tag_name']
);
$transient->response[$this->basename] = (object) $plugin;
but i have received email from github that says i should add authorize_token to header.
I'm trying to find solution by google, but i can't find it.
How can I fix it?
I figured that you will have to add a filter to the http_request. So when wordpress will try any http request it will go through your filter. Then inside that filter you can check the url and if it is your plugin source url, you can then add your accessToken to the header.
this method was in my updater class, and I had setup the class property accessToken and username when initializing the updater.
add_filter( "http_request_args", array( $this, "addHeaders") , 10, 3);
public function addHeaders($parsed_args, $url)
{
if(empty($parsed_args['headers']))
{
$parsed_args['headers'] = [];
}
if(strpos($url, "https://api.github.com/repos/{$this->username}/{$this->repo}") !== FALSE)
{
$parsed_args['headers']['Authorization'] = "token $this->accessToken";
error_log("Adding token for : $url");
}
else
{
error_log("Not adding token for $url");
}
return $parsed_args;
}

Wordpress plugin for course registration

I'm building a WordPress Page for course registration. All I want the plugin to do is send the filled in form details to my email ID and send an email to the user that he/she has successfully registered for the course. I don't need users to signup with username and password.
I've tried my luck with WP Forms but it only seems to have the option to forward the email to me and not the user.
Any suggestion on which plugin I should use?
As #Hughes mentioned, you cant use wpcf7, and just hook on it to insert custom post on every query.
// Hook on wpcf7
add_filter( 'wpcf7_mail_components', 'do_on_cf7_submit', 50, 2 );
function do_on_cf7_submit($mail_params, $form = null) {
// Empty post content
$content = '';
// set post content if field not empty
if ($_POST['field-name'] != '') {
$content .= 'Field Name Label: '.$_POST['field-name'] ;
}
// insert post if content not epmty
if ($content != '') {
insertQueryPost($_POST['email'], $content);
}
// allow cf7 to do his stuff
return $mail_params;
}
// insert custom post type "query", don't forget to setup your custom post type first
function insertQueryPost($title, $content) {
// insted of proper post slug, just make a hashed slug, when setting custom post type, set it to not public and not search-able
$t = time();
$thash = md5($t);
$my_query = array(
'post_title' => wp_strip_all_tags( $title ),
'post_content' => $content,
'post_type' => 'query',
'post_name' => $thash,
'post_status' => 'publish',
'post_author' => 1
);
$data = wp_insert_post( $my_query );
}

Wordpress Create Custom Post on New User Register

In Wordpress I'm Trying to Create Custom Post on New User Register of specific user role "author"
For This I try to figure out this Code in Function.php
add_action( 'user_register', 'wpse_216921_company_cpt', 10, 1 );
function wpse_216921_company_cpt( $user_id )
{
// Get user info
$user_info = get_userdata( $user_id );
$user_roles=$user_info->roles;
if ($user_roles == 'author') {
// Create a new post
$user_post = array(
'post_title' => $user_info->nickname,
'post_type' => 'CustomPost', // <- change to your cpt
);
// Insert the post into the database
$post_id = wp_insert_post( $user_post );
}
}
But Not Success. After I add above code no error working fine but it not triggering automatic and not creating new custom post
I simple want that whenever I add new Author / New Author Register it create one Custom Post with title as same as username. and publish it
The role your are checking with is not correct, $user_info->roles returns an array, not a string. Find the modified code below,
add_action( 'user_register', 'wpse_216921_company_cpt', 10, 1 );
function wpse_216921_company_cpt( $user_id )
{
// Get user info
$user_info = get_userdata( $user_id );
$user_roles = $user_info->roles;
// New code added
$this_user_role = implode(', ', $user_roles );
if ($this_user_role == 'author') {
// Create a new post
$user_post = array(
'post_title' => $user_info->nickname,
'post_status' => 'publish', // <- here is to publish
'post_type' => 'CustomPost', // <- change to your cpt
);
// Insert the post into the database
$post_id = wp_insert_post( $user_post );
}
}
Hope this helps.

Wordpress User Data sent through cURL

I've just integrated the marketing tool Klaviyo with our Wordpress/WooCommerce set up and I'm trying to push user meta data through a cURL API - but failing!
You can find out how the API works here: https://www.klaviyo.com/docs/http-api#people
I'm hoping to add an action so when the user profile saves, it hooks in my function sending the meta data through to Klaviyo.
Can anyone see what I've done wrong please - code below?
Thanks so much in advance.
Linz
<?php
// Hook into the action which saves the User Meta Data (written by LD)
add_action( 'personal_options_update', 'klaviyo_send' );
add_action( 'edit_user_profile_update', 'klaviyo_send' );
function klaviyo_send () {
// Get cURL resource
$curl = curl_init();
// Set some options
curl_setopt_array($curl, array(
CURLOPT_RETURNTRANSFER => 1,
CURLOPT_URL => 'https://a.klaviyo.com/api/identify?data=eyJ0b2tlbiI6ICJoYXFYaXEiLCAicHJvcGVydGllcyI6IHsiJGVtYWlsIjogInRob21hcy5qZWZmZXJzb25AZXhhbXBsZS5jb20iLCAiJGxhc3RfbmFtZSI6ICJKZWZmZXJzb24iLCAiUGxhbiI6ICJUcmlhbCIsICJTaWduIFVwIERhdGUiOiAiMjAxMy0wMS0yNyAxMjoxNzowNiIsICIkZmlyc3RfbmFtZSI6ICJUaG9tYXMifX0=',
CURLOPT_POST => 1,
CURLOPT_POSTFIELDS => array(
'token' => 'haqXiq', //This is the public 'key' in Klaviyo
'$email' => $user_id->email, //This translates the wordpress field into the Klaviyo field
'twitter' => $user_id->twitter,
)
));
// Send the request & save response to $resp
$resp = curl_exec($curl);
// Close request to clear up some resources
curl_close($curl);
}
?>
Try sending data by this way .. change the posted data to url friendly ways like ?key=value&key=value&key=value
//extract data from the post
//set POST variables
$url = 'https://a.klaviyo.com/api/identify?data=eyJ0b2tlbiI6ICJoYXFYaXEiLCAicHJvcGVydGllcyI6IHsiJGVtYWlsIjogInRob21hcy5qZWZmZXJzb25AZXhhbXBsZS5jb20iLCAiJGxhc3RfbmFtZSI6ICJKZWZmZXJzb24iLCAiUGxhbiI6ICJUcmlhbCIsICJTaWduIFVwIERhdGUiOiAiMjAxMy0wMS0yNyAxMjoxNzowNiIsICIkZmlyc3RfbmFtZSI6ICJUaG9tYXMifX0=';
$fields = array(
'token' => 'haqXiq', //This is the public 'key' in Klaviyo
'$email' => $user_id->email, //This translates the wordpress field into the Klaviyo field
'twitter' => $user_id->twitter,
);
//url-ify the data for the POST
foreach($fields as $key=>$value) { $fields_string .= $key.'='.$value.'&'; }
rtrim($fields_string, '&');
//open connection
$ch = curl_init();
//set the url, number of POST vars, POST data
curl_setopt($ch,CURLOPT_URL, $url);
curl_setopt($ch,CURLOPT_POST, count($fields));
curl_setopt($ch,CURLOPT_POSTFIELDS, $fields_string);
//execute post
$result = curl_exec($ch);
//close connection
curl_close($ch);

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