I\'ve been struggling to find a solution for this and I don\'t know if it\'s the Gutenberg editor or if it\'s the hook publish_post
.
Hook:
function api_method($post_id) {
// If this is just a revision, don\'t send request
if ( wp_is_post_revision( $post_id ) ) {
return;
}
// Get Post Object
$post = get_post($post_id);
open_log($post);
$postData = array(
\'unit\' => get_post_meta($post_id, \'open_unit\', true),
\'abstract\' => get_post_meta($post_id, \'open_abstract\', true),
\'image\' => get_the_post_thumbnail_url($post, \'full\'),
\'title\' => get_the_title($post),
\'url\' => get_the_permalink($post),
);
open_log($postData);
$auth = base64_encode(get_option(\'open_up_username\') . \':\' . get_option(\'open_up_api_key\'));
$status = get_post_meta($post_id, \'open_active\', true) ? \'active\':\'paused\';
$openUnit = get_post_meta($post_id, \'open_unit\', true);
// $postMeta = get_post_meta($post_id);
// open_log($postMeta);
$responseApi = wp_remote_post(OPEN_REMOTE_POST, array(
\'headers\' => array(\'Authorization\' => \'Basic \' . $auth),
\'body\' => \'unit=\'.$openUnit.\'&status=\'.$status.\'&abstract=\'.get_post_meta($post_id, \'open_abstract\', true).\'&image=\'.get_the_post_thumbnail_url($post_id, \'full\').\'&title=\'.get_the_title($post).\'&url=\'.get_the_permalink($post)
)
);
$response = new WP_REST_Response($responseApi, 200);
$body = wp_remote_retrieve_body($responseApi);
$responseBody = ( ! is_wp_error( $response ) ) ? json_decode( $body, true ) : null;
$unit = isset($responseBody[\'unit\']) ? $responseBody[\'unit\'] : \'\';
open_log($responseBody);
open_log($unit);
$update = update_post_meta($post_id, \'open_unit\', $unit);
open_log($update);
}
I\'m using the post meta, featured image and title to post to a third party API. The API verifies the data and then returns a unit hash which I store in the post meta open_unit
.
I\'m also logging the data and responses in a log.txt file, and I\'m getting this on initial publish:
object(WP_Post)#4421 (24) {
["ID"]=>
int(240)
["post_author"]=>
string(1) "3"
["post_date"]=>
string(19) "2021-05-07 09:57:28"
["post_date_gmt"]=>
string(19) "2021-05-07 07:57:28"
["post_content"]=>
string(0) ""
["post_title"]=>
string(11) "New post v3"
["post_excerpt"]=>
string(0) ""
["post_status"]=>
string(7) "publish"
["comment_status"]=>
string(4) "open"
["ping_status"]=>
string(4) "open"
["post_password"]=>
string(0) ""
["post_name"]=>
string(11) "new-post-v3"
["to_ping"]=>
string(0) ""
["pinged"]=>
string(0) ""
["post_modified"]=>
string(19) "2021-05-07 09:57:28"
["post_modified_gmt"]=>
string(19) "2021-05-07 07:57:28"
["post_content_filtered"]=>
string(0) ""
["post_parent"]=>
int(0)
["guid"]=>
string(31) "https://grace.open-up.it/?p=240"
["menu_order"]=>
int(0)
["post_type"]=>
string(4) "post"
["post_mime_type"]=>
string(0) ""
["comment_count"]=>
string(1) "0"
["filter"]=>
string(3) "raw"
}
array(5) {
["unit"]=>
string(0) ""
["abstract"]=>
string(0) ""
["image"]=>
bool(false)
["title"]=>
string(11) "New post v3"
["url"]=>
string(45) "https://grace.open-up.it/recipes/new-post-v3/"
}
array(2) {
["success"]=>
bool(false)
["message"]=>
string(83) "Open Up - Si è verificato un problema con l\'elaborazione dell\'immagine in evidenza"
}
string(0) ""
int(1256)
The API is returning and saying that the image is invalid which is true because the image url is not showing in the $postData
array.
After I edit the title and save, it logs below:
object(WP_Post)#4420 (24) {
["ID"]=>
int(240)
["post_author"]=>
string(1) "3"
["post_date"]=>
string(19) "2021-05-07 09:57:28"
["post_date_gmt"]=>
string(19) "2021-05-07 07:57:28"
["post_content"]=>
string(0) ""
["post_title"]=>
string(12) "Edit post v3"
["post_excerpt"]=>
string(0) ""
["post_status"]=>
string(7) "publish"
["comment_status"]=>
string(4) "open"
["ping_status"]=>
string(4) "open"
["post_password"]=>
string(0) ""
["post_name"]=>
string(11) "new-post-v3"
["to_ping"]=>
string(0) ""
["pinged"]=>
string(0) ""
["post_modified"]=>
string(19) "2021-05-07 09:57:40"
["post_modified_gmt"]=>
string(19) "2021-05-07 07:57:40"
["post_content_filtered"]=>
string(0) ""
["post_parent"]=>
int(0)
["guid"]=>
string(31) "https://grace.open-up.it/?p=240"
["menu_order"]=>
int(0)
["post_type"]=>
string(4) "post"
["post_mime_type"]=>
string(0) ""
["comment_count"]=>
string(1) "0"
["filter"]=>
string(3) "raw"
}
array(5) {
["unit"]=>
string(0) ""
["abstract"]=>
string(213) "Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum."
["image"]=>
string(79) "https://grace.open-up.it/wp-content/uploads/2020/10/7540312530_37e709d2f4_b.jpg"
["title"]=>
string(12) "Edit post v3"
["url"]=>
string(45) "https://grace.open-up.it/recipes/new-post-v3/"
}
array(2) {
["success"]=>
bool(true)
["unit"]=>
string(36) "e12213d3-058b-40f9-b487-a1776470f37b"
}
string(36) "e12213d3-058b-40f9-b487-a1776470f37b"
bool(true)
I really am confused about how the Wordpress editor handles the state for publishing and confused why it\'s not returning the meta and featured image. I found this user having the same issue(I think) with the same hook and isn\'t resolved either https://stackoverflow.com/questions/35666305/get-featured-image-url-after-publishing
I have also done a recording of this https://share.getcloudapp.com/eDujbqJp
I hope someone can advise or give direction regarding this. Thank you in advance