【问题标题】:Can't display custom field from Wordpress REST API custom endpoint无法显示来自 Wordpress REST API 自定义端点的自定义字段
【发布时间】:2020-09-01 13:46:21
【问题描述】:

我正在使用 Wordpress Rest API 将内容从 Wordpress 网站导入 PHP 应用程序。 这并不复杂,只是一个包含帖子列表和各个帖子页面的主页。

我在 API 响应中添加了一些字段,特别是用于获取帖子中插入的第一张图片的 url。

这是这部分的代码:

add_action('rest_api_init', function () {
    register_rest_field('post', 'post_images', array(
        'get_callback'    => 'get_first_image',
        'update_callback' => null,
        'schema'          => null
    ));
});

function get_first_image($obj, $name, $request)
{
    $images = get_attached_media('image', $obj['id']);
    $imagesArray = (array) $images;
    reset($imagesArray);
    $firstImageId = current($imagesArray)->ID;
    $imageSrc = wp_get_attachment_image_url($firstImageId);
    return $imageSrc;
}

当我在主页中列出帖子时它工作正常,但在单个帖子页面中该字段为空。我能想出的唯一解释是我为单个帖子提供了这个自定义端点:

function post_by_slug(WP_REST_Request $request)
{
    $postSlug = $request->get_param('post_slug');
    $lang     = $request->get_param('my_lang');
    $myPost   = get_page_by_path($postSlug, OBJECT, 'post');
    $targetPostId   = apply_filters('wpml_object_id', $myPost->ID, 'post',
        false, $lang);
    $targetPost     = get_post($targetPostId);
    $postController = new \WP_REST_Posts_Controller($targetPost->post_type);
    $response       = $postController->prepare_item_for_response($targetPost,
        $request);

    return rest_ensure_response($response);
}

add_action('rest_api_init', function () {
    register_rest_route('pc/v1',
        "/post-slug/(?P<post_slug>\S+)/(?P<my_lang>\w+)", [
            'methods'  => 'GET',
            'callback' => 'post_by_slug',
            'args'     => [
                'post_slug' => 'required',
                'my_lang'   => 'required'
            ]
        ]);
});

在我的应用中,我这样称呼它:

$client = new Client([
    'base_uri' => 'http://example.com/wp-json/pc/v1/',
    'headers' => [
        'Content-Type' => 'application/json',
        "Accept" => "application/json",
    ],
    'verify' => false,
]);

var_dump(json_decode($client->get("post-slug/$slug/$lang")
                             ->getBody()->getContents()));

奇怪的是,直接从浏览器访问同一个端点我可以正确看到所有字段。我错过了什么?

【问题讨论】:

    标签: wordpress guzzle wordpress-rest-api wpml


    【解决方案1】:

    只是回答我自己的问题,因为我发现了是什么导致端点在我的浏览器中正常工作,但在通过 Guzzle 访问时却没有。

    问题是我以管理员身份登录,所以我用来管理网站上多种语言的 WPML 插件设置了这个 cookie:

    wp-wpml_current_admin_language_d41d8cd98f00b204e9800998ecf8427e:"de"
    

    所以问题与这个插件有关,我实际上是在post_by_slug 函数中使用它。 不知何故,像我在这里指定语言是不够的,或者只是用于不同的目的,不确定:

    $targetPostId   = apply_filters('wpml_object_id', $myPost->ID, 'post', false, $lang);
    

    我可以找到两种解决方案:

    1) 使用插件switch_lang() 方法显式设置语言:

    function post_by_slug(WP_REST_Request $request) {
      global $sitepress;
      $postSlug = $request->get_param('post_slug');
      $lang = $request->get_param('my_lang');
      $sitepress->switch_lang($lang);
    

    2) 更改 guzzle GET 请求以将语言作为查询参数传递,这似乎可以自动与插件一起使用:

    $response = $client->get("post-slug/$slug", ['query' => ['lang' => $lang]])->getBody()->getContents();
    

    我意识到这是一个非常具体的问题,但也许对其他人有帮助。

    【讨论】:

      猜你喜欢
      • 2017-04-02
      • 2017-10-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-05-06
      • 1970-01-01
      • 2020-07-23
      相关资源
      最近更新 更多