wordpress 如何更新savepost挂接和wp远程开机自检中的ACF字段

k7fdbhmy  于 2023-01-20  发布在  WordPress
关注(0)|答案(1)|浏览(118)

如何使用现有的API通过wp_remote_post或其他方法更新post meta或ACF字段?

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);

我不想在目标网站上开发新的API,我想使用现有的(wp/v2/posts/{id})WordPress API。

00jrzges

00jrzges1#

你可以试试这个:

$data = array(
    'acf' => array(
        'full_content' => $full_content
    )
);
$response = wp_remote_post(
    'https://example.com/wp-json/wp/v2/posts/12',
    array(
        'headers' => array(
            'Authorization' => 'Basic ' . base64_encode("$login:$password"),
            'Content-Type' => 'application/json'
        ),
        'body' => json_encode($data)
    )
);

确保目标站点激活了json-rest-API插件,并且您的用户具有编辑帖子的适当权限。

相关问题