I'm trying to pass shortcode attribute values directly into my url for a WordPress Remote Post request. Below is my code example below. I am using the following shortcode in my wordpress page
[example-data start="1" count="10"]
but i cannot get the start or the count data to work in the URL. Any help would be appreciated.
function display_example_data( $atts ) {
$value = shortcode_atts( array(
'start' => 1,
'count' => 10,
), $atts );
$url ="https://example.com/api/reviews?token=".$api_text_key."&start=".$value['start']."&count=".$value['count']."";
$response = wp_remote_post( $url, array(
'method' => 'GET',
'timeout' => 45,
'redirection' => 5,
'httpversion' => '1.0',
'blocking' => true,
'headers' => array(),
'cookies' => array()
)
);
<?php }
add_shortcode( 'example-data', 'display_example_data' );
I need to create an API that will render a related post by category filter. I have written the code in my functions.php file but I did not get how can I pass a post id to the arguments?
function related_posts_endpoint( $request_data ) {
$uposts = get_posts(
array(
'post_type' => 'post',
'category__in' => wp_get_post_categories(183),
'posts_per_page' => 5,
'post__not_in' => array(183),
) );
return $uposts;
}
add_action( 'rest_api_init', function () {
register_rest_route( 'sections/v1', '/post/related/', array(
'methods' => 'GET',
'callback' => 'related_posts_endpoint'
) );
} );
I need to pass the id from my current API call. So, I need to pass that id to the related API arguments that I have currently passed as static (180)
Image of Current post API from which I need to render a related API
You can add to your rest route a parameter called post_id, and then access the id from the request_data array.
function related_posts_endpoint( $request_data ) {
$post_id = $request_data['post_id'];
$uposts = get_posts(
array(
'post_type' => 'post',
'category__in' => wp_get_post_categories($post_id),
'posts_per_page' => 5,
'post__not_in' => array($post_id),
)
);
return $uposts;
}
add_action( 'rest_api_init', function () {
register_rest_route( 'sections/v1', '/post/related/(?P<post_id>[\d]+)', array(
'methods' => 'GET',
'callback' => 'related_posts_endpoint'
));
});
You can add the id to the end of your URL call /post/related/183.
Your can get the post id like normal get request. ?key=value and use its ad $request['key'] so Your code should be like this.
function related_posts_endpoint( $request_data ) {
$uposts = get_posts(
array(
'post_type' => 'post',
'category__in' => wp_get_post_categories(183),
'posts_per_page' => 5,
'post__not_in' => array($request_data['post_id']),//your requested post id
)
);
return $uposts;
}
add_action( 'rest_api_init', function () {
register_rest_route( 'sections/v1', '/post/related/', array(
'methods' => 'GET',
'callback' => 'related_posts_endpoint'
));
});
Now your api url should be like this /post/related?post_id=183
try this then let me know the result.
I'm unable to use put http requests in WordPress.
https://codex.wordpress.org/HTTP_API
This link says you can do it through wp_remote_request()? But where to specify that ?
Can anyone help me with this? I need a code.
$body = array(
"status" => "publish"
);
$args = array(
'headers' => array(
'Content-Type' => 'application/json',
),
'body' => json_encode($body),
'method' => 'PUT'
);
$result = wp_remote_request( "http:300/wp-json/wp/post/1", $args );
Like this
$response = wp_remote_request( 'http://test.com/test', [
'method' => 'PUT'
] );
I'm attempting to POST a file through wp_remote_post. Unfortunately, any file stream, or file paths (prepended with CURL style #, or not), passed to this function are simply dropped and removed from the payload.
I found a post on wp-hackers, however, it's extremely hack-ish and error prone. Is there really no way to transmit a file through this function without writing a complete HTTP payload from scratch?
Here's an example code block using CURL style (prepending path with #), if interested:
$body["attachment[{$i}]"] = "#{$attachment}";
$data = array(
'body' => $body,
'headers' => array(
'Authorization' => 'Basic ' . base64_encode( "user:{$apiKey}" )));
$url = "https://api.someservice.net/{$domain}/endpoint";
$response = wp_remote_post( $url, $data );
Thanks!
Post data should be sent in the body as an array. Example passing post data:
$response = wp_remote_post( $url, array(
'method' => 'POST',
'timeout' => 45,
'redirection' => 5,
'httpversion' => '1.0',
'blocking' => true,
'headers' => array(),
'body' => array( 'username' => 'bob', 'password' => '1234xyz' ),
'cookies' => array()
)
);
if ( is_wp_error( $response ) ) {
$error_message = $response->get_error_message();
echo "Something went wrong: $error_message";
} else {
echo 'Response:<pre>';
print_r( $response );
echo '</pre>';
}
In the example above, $response['body'] will contain the actual page content returned by the server.
I am using XMLRPC to do posts to Wordpress. I am having issues posting thumbnails, after debugging wordpress code I see that my issue is caused by the fact that the image is not attached to the post.
I must do this without patching wordpress or using PHP, only iwth XMLRPC.
I can upload an image and get the ID of the image.
Other point that confuses me is how do you attach an image to a post that you did not posted yet because you wait for the image to upload? I am supposed to upload image then post ,then using the image id and the post id do an update on the image metadata?
Edit: the code in wordpress that is problematic is this check
if ( $thumbnail_html = wp_get_attachment_image( $thumbnail_id, 'thumbnail' ) )
and my assumption it is that it fails because the image is Unattached, if i fix that code all is fine but I can't patch the WP of my application users(so this is not a solution)
Yes it is possible to do it, if Wordpress version is 3.5 or greater,when using the code for uploading file/image you can set the post_id.
The flow I used for new posts with featured images is like this:
use the newPost function and post the content without the featured
image and also set publish to false, record the post_id returned by
this
upload the image and set the post_id to the id of the post just
posted, record the image_id
when done edit the post and set the wp_post_thumbnail equal to the
image_id you just uploaded and also set publish to true(if needed)
Important:
The mime type is important, it must be "image/jpg" or "image/png" please see documentation, if mime type is worng like "jpg" attaching will fail.
Tip:
For debugging, if you get a generic error from wordpress and you can't figure out why you can check the wordpress code and even edit it, adding debugging/tracing calls and hopefully you can figure out the cause.
This is an example of a post with category, image and tags. It requires class-IXR.php
https://github.com/WordPress/WordPress/blob/master/wp-includes/class-IXR.php
and mime_content_type function
https://github.com/caiofior/storebaby/blob/master/magmi/plugins/extra/general/socialnotify/wp/mimetype.php
$client = new IXR_Client($url);
$content = array(
'post_status' => 'draft',
'post_type' => 'post',
'post_title' => 'Title',
'post_content' => 'Message',
// categories ids
'terms' => array('category' => array(3))
);
$params = array(0, $username, $password, $content);
$client->query('wp.newPost', $params);
$post_id = $client->getResponse();
$content = array(
'name' => basename('/var/www/sb/img.jpeg'),
'type' => mime_content_type('/var/www/sb/img.jpeg'),
'bits' => new IXR_Base64(file_get_contents('/var/www/sb/img.jpeg')),
true
);
$client->query('metaWeblog.newMediaObject', 1, $username, $password, $content);
$media = $client->getResponse();
$content = array(
'post_status' => 'publish',
// Tags
'mt_keywords' => 'tag1, tag2, tag3',
'wp_post_thumbnail' => $media['id']
);
$client->query('metaWeblog.editPost', $post_id, $username, $password, $content, true);
My version if you want to use only wp.newPost and wp.editPost
include_once( ABSPATH . WPINC . '/class-IXR.php' );
include_once( ABSPATH . WPINC . '/class-wp-http-ixr-client.php' );
$usr = 'username_on_the_server_side';
$pwd = 'password_on_the_server_side';
$xmlrpc = 'server side xmlrpc.php url';
$client = new IXR_Client($xmlrpc);
//////////// IMAGE UPLOAD AND ATTACHMENT POST CREATION ///////////
$img_attach = 'link to the image';
$img_attach_content = array(
'name' => basename($img_attach),
'type' => mime_content_type($img_attach),
'bits' => new IXR_Base64(file_get_contents($img_attach)),
);
$status = $client->query( 'wp.uploadFile','1', $usr, $pwd, $img_attach_content );
$image_returnInfo = $client ->getResponse();
//////////// POST CREATION ///////////
$custom_fields = array(
array( 'key' => 'blabla1', 'value' => 'blabla1_value' ),
array( 'key' => 'blabla12', 'value' => 'blabla1_value2')
);
$post_content = array(
'post_type' => 'post',
'post_status' => 'draft', //for now
'post_title' => 'XMLRPC Test',
'post_author' => 3,
'post_name' => 'XMLRPC Test',
'post_content' => 'XMLRPC Test Content',
'custom_fields' => $custom_fields
);
$res = $client -> query('wp.newPost',1, $usr, $pwd, $post_content);
$postID = $client->getResponse();
if(!$res)
echo 'Something went wrong....';
else {
echo 'The Project Created Successfully('.$res.')<br>Post ID is '.$postID.'<br>';
}
//////////// Image Post Attachment Edit ///////////
$img_attach_content2 = array(
'post_type' => 'attachment',
'post_status' => 'inherit',
'post_title' => $postID,
'post_name' => $postID,
'post_parent' => $postID,
'guid' => $image_returnInfo['url'],
'post_content' => '',
'post_mime_type' => 'image/jpg'
);
$res2 = $client -> query('wp.editPost', 0, $usr, $pwd, $image_returnInfo['id'], $img_attach_content2);
$postIDimg = $client->getResponse();
//////////// POST EDIT ///////////
$post_content2 = array(
'post_status' => 'publish', //publish
'wp_post_thumbnail' => $image_returnInfo['id'],
'custom_fields' => array( 'key' => '_thumbnail_id', 'value' => $image_returnInfo['id'] )
);
$media2= $client->query('wp.editPost',0, $usr, $pwd, $postID, $post_content2);
This is my version, using wp.newPost and wp.editPost, added on WordPress 3.4, that allow the use of custom post types.
require_once("IXR_Library.php.inc");
$title = 'My title';
$body = 'My body';
$category="category1, category2"; // Comma seperated pre existing categories. Ensure that these categories exists in your blog.
$keywords="keyword1, keyword2, keyword3";
$customfields=array('key'=>'Author-bio', 'value'=>'Autor Bio Here'); // Insert your custom values like this in Key, Value format
$title = htmlentities($title,ENT_NOQUOTES,#$encoding);
$keywords = htmlentities($keywords,ENT_NOQUOTES,#$encoding);
$content = array(
'post_title'=>$title,
'post_content'=>$body,
'post_type'=>'some_custom_post_type',
'post_status' => 'draft', // http://codex.wordpress.org/Post_Status
'mt_allow_comments'=>0, // 1 to allow comments
'mt_allow_pings'=>0, // 1 to allow trackbacks
'mt_keywords'=>$keywords,
'categories'=>array($category),
'custom_fields' => array($customfields)
);
// Create the client object
$client = new IXR_Client('http://example.com/xmlrpc.php');
$username = "wp_username";
$password = "wp_username_password";
$params = array(0,$username,$password,$content,true); // Last parameter is 'true' which means post immediately, to save as draft set it as 'false'
if (!$client->query('wp.newPost', $params)) {
die('<br/><strong>Something went wrong - '.$client->getErrorCode().' : '.$client->getErrorMessage().'<br >');
}
else
{
$post_id = $client->getResponse();
echo 'Inserted with id'.$post_id;
$picture = '/full/path/to/pic.jpg';
$content = array(
'name' => basename($picture),
'type' => mime_content_type($picture),
'bits' => new IXR_Base64(file_get_contents($picture)),
true
);
if (!$client->query('metaWeblog.newMediaObject', 1, $username, $password, $content)) {
die('<br/><strong>Something went wrong - newMediaObject'.$client->getErrorCode().' : '.$client->getErrorMessage().'<br >');
}
else
{
$media = $client->getResponse();
$content = array(
'post_status' => 'publish',
'post_thumbnail' => $media['id']
);
if (!$client->query('wp.editPost', 0, $username, $password, $post_id, $content)) {
die('<br/><strong>Something went wrong editPost - '.$client->getErrorCode().' : '.$client->getErrorMessage().'<br >');
}
}
}