通过 REST API 使用 WPML 获取完整渲染的 Wordpress 页面
我已经为一个项目建立了 Laravel-Wordpress 连接。对于此连接,我使用 Worpdress REST API。 我可以通过 API 从 Laravel 获取带有页眉和页脚的特定页面(以获得统一的外观),其他页面完全来自 Wordpress。最大的好处是,Wordpress 页面可以使用相当用户友好的 Wordpress 后端进行编辑,而其他页面可以在 Laravel 中进行自定义编码。
为了实现这一点,我添加了一些新的 API 端点来检索渲染的页眉/页脚和完整页面:
add_action('rest_api_init', function () {
// add 'rendered_page' member with complete HTML for requested page
register_rest_field(
'page',
'content',
array(
'get_callback' => 'by_render_page_content',
'update_callback' => null,
'schema' => null,
)
);
// add new endpoint for getting header and footer
register_rest_route('eeg/v1', '/headerandfooter', array(
'methods' => 'GET,POST',
'callback' => 'get_page_header_and_footer',
));
});
回调如下所示:(即完整页面)
if (isset($request['language'])) {
do_action('wpml_switch_language', $request['language']);
}
$file = THEME_DIR . '/single.php';
ob_start();
include $file;
return ob_get_clean();
如您所见,我正在检查 language
参数并使用 wpml_switch_language
挂钩设置 WPML 插件的当前语言。 这似乎有“一点点”作用。例如,语言切换器会显示正确的当前语言。
问题是,在主菜单中:所有链接都显示为默认语言。例如,如果当前选择的语言是英语,则链接应类似于 mydomain.com/en/requestedpage
。但所有链接都会转到默认的 mydomain.com/requestedpage
。此外, 参数设置为默认语言,而不是请求的语言。而且 WPML 中的
标签也丢失了。
如果我通过 WordPress 前端(托管在子域中)访问该页面,则一切正常。 所以我认为,我必须在其他地方设置请求的语言,或者必须调用一些 WPML“准备”挂钩或类似的东西,才能使这项工作正常进行。也许 include './single.php';
也不是执行此操作的正确方法。
欢迎任何提示。
I've made a Laravel-Wordpress connection for a project. For this connection, I use the Worpdress REST API.
I'm able to get specific pages from Laravel with the header and footer from Wordpress via API (for a uniform look) and other pages comes completely from Wordpress. Big benefit is, that Wordpress pages can be edited with the pretty user-friendly Wordpress backend while other pages can be custom coded in Laravel.
To achieve this, I have added some new API endpoints to retrieve the rendered header / footer and a complete page:
add_action('rest_api_init', function () {
// add 'rendered_page' member with complete HTML for requested page
register_rest_field(
'page',
'content',
array(
'get_callback' => 'by_render_page_content',
'update_callback' => null,
'schema' => null,
)
);
// add new endpoint for getting header and footer
register_rest_route('eeg/v1', '/headerandfooter', array(
'methods' => 'GET,POST',
'callback' => 'get_page_header_and_footer',
));
});
The callbacks look like this: (i.e. for the complete page)
if (isset($request['language'])) {
do_action('wpml_switch_language', $request['language']);
}
$file = THEME_DIR . '/single.php';
ob_start();
include $file;
return ob_get_clean();
As you can see, I'm checking for a language
parameter and set the current language of the WPML plugin with wpml_switch_language
hook.
This seems to work 'a little bit'. So, for example, the language switcher shows the correct current language.
The problem is, in the main menu: All links are showing to default language. For example if currently selected language is english, the links should look like mydomain.com/en/requestedpage
. But all links go to the default mydomain.com/requestedpage
. Also the <html lang>
parameter is set to the default language, not to the requested. And also the <link hreflang>
tags from WPML are missing.
If I access the page via the Wordpress Frontend (which is hosted at a subdomain), everything is working correctly.
So I think, I have to set the requested language somewhere else or have to call some WPML 'prepare' hooks or something like that, to make this work. Maybe also the include './single.php';
is not the right way to do this.
Any hint is welcome.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我可以自己修好。
解决方案是使用 WPML 创建翻译菜单。
在 WordPress 前端中,菜单中的链接也已本地化,无需额外的翻译菜单。但对于这种使用 REST API 的场景,需要翻译菜单。
I could fix it by myself.
The solution was to create a translated menu with WPML.
In Wordpress frontend, the links in the menus are localized also without an extra translated menu. But for this scenario with the REST API, a translated menu is necessary.