WordPress PUBLISH_POST挂钩在第一次发布时未获得特色图像和元数据,但可用于更新标题

时间:2021-05-07 作者:msbodetti

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

2 个回复
最合适的回答,由SO网友:Sally CJ 整理而成

我不知道是古腾堡的编辑还是钩子publish_post

钩子本身可以工作,如果您使用旧的WordPress post编辑器,那么所讨论的问题将not 发生

所以你可以说这是古腾堡/方块编辑器。

为什么不返回元图像和特征图像

因为Gutenberg使用REST APIpublish_post 吊钩已启动(当wp_update_post() 已调用-请参阅source), 帖子的特色图片和其他元数据尚未保存/处理。

如果使用WordPress,如何解决此问题5.6 或更高版本,则对于您要执行的操作,您需要使用wp_after_insert_post hook 它与旧/经典编辑器和古腾堡/块编辑器配合得很好。

摘录自https://make.wordpress.org/core/2020/11/20/new-action-wp_after_insert_post-in-wordpress-5-6/:

WordPress 5.6中的新操作wp\\u after\\u insert\\u post

新操作wp_after_insert_post 已添加到WordPress5.6中,以允许主题和插件开发人员在更新帖子及其术语和元数据后运行自定义代码。

这个save_post 相关操作通常用于此目的,但这些挂钩可以在术语和元数据在经典编辑器之外更新之前触发。(例如,在REST API中,通过块编辑器,在自定义程序中以及创建自动绘图时。)

新操作最多可发送四个参数:

  • $post_id 帖子ID已更新integer.
  • $post 更新形式的完整post对象WP_Post 对象
  • $updated 无论帖子是否已更新boolean.
  • $post_before 更新之前的完整post对象WP_Post 对象对于新帖子,这是null.publish_post 挂钩,即// your code here 以下部分仅在帖子正在发布且尚未发布(帖子状态尚未发布)时运行publish):

    add_action( \'wp_after_insert_post\', \'my_wp_after_insert_post\', 10, 4 );
    // Note: As of writing, the third parameter is actually named $update and not $updated.
    function my_wp_after_insert_post( $post_id, $post, $update, $post_before ) {
        if ( \'publish\' !== $post->post_status                           ||
            ( $post_before && \'publish\' === $post_before->post_status ) ||
            wp_is_post_revision( $post_id )
        ) {
            return;
        }
    
        // your code here
    }
    

SO网友:anton

{$new_status}_{$post->post_type} 挂钩或publish_post 当帖子从一种状态转换为另一种状态时激发。这个钩子触发时,还没有保存任何帖子元(在帖子创建时)。

如果需要使用自定义post meta,保存回调通常附加到save_postsave_post 还将很快开火。

我想最好试试updated_post_meta 钩子,它在更新特定类型的元数据后立即激发。

相关推荐

在PUBLISH_POST操作中检索wp_mail的ACF字段

我有一个名为“Envios”的自定义帖子类型。我在其中创建了一个自定义元框,可以选择用户和帖子。当其中一个自定义帖子类型发布时,它会向所选用户发送一封包含所选帖子内容的电子邮件。要选择帖子,我使用ACF插件和field Post对象。问题是,有时电子邮件发送正确,但在大多数情况下,什么都没有发送。代码如下: // Adds Custom Meta Box function email_delivery_munda_add_custom_box() { $scr