我试图在Wordpress中创建一个Gutenberg块,网站的用户可以从两个选择中选择值。每次用户从其中一个选择值更改值时,都应该执行对外部源的API调用。当api调用结果在下面返回时,应该显示一个带有3个结果的网格。
为了实现这一点,我尝试了多种解决方案,其中之一是Wordpress文档中讨论的动态块。但是,当我呈现动态块的内容时,内容将呈现在屏幕的左上角。我觉得很奇怪。
此外,我看到一些人讨论了更改块的保存部分中的select值的可能性,并让编辑端的属性对此做出反应并获取结果。我在这方面找不到任何例子,我觉得很奇怪,但考虑到我在使用react方面的背景,这将是我首选的解决方案。
在两个不同的选择组件中更改值后,是否可以使用Gutenberg块并执行API调用并在前端向用户显示结果?
发布于 2022-06-18 11:15:47
是的,使用服务器端呈现,其中PHP处理API请求并返回要呈现给块的结果。考虑到您对“在左上角呈现内容”的描述,我认为您在正确的路径上,但可能缺少了description和<ServerSideRender>组件之间的链接。
下面是一个(非常)基本的例子,说明这一切如何作为一个动态的Gutenberg块一起工作。首先,我使用npx @wordpress/create-block创建了一个基本的静态Gutenberg块来设置这个项目。示例块将从外部API (randomuser.me)请求新用户,然后显示结果。API参数gender和nationality的值存储为块属性,例如:
src/block.json
{
"$schema": "https://schemas.wp.org/trunk/block.json",
"apiVersion": 2,
"name": "create-block/dynamic-rendering",
...
"attributes": {
"gender": {
"type": "string",
"default": "any"
},
"nationality": {
"type": "string",
"default": "AU"
}
}
}编辑器的UI是在edit.js中定义的,以使用户能够选择/更新API所需的块属性的值。还需要<ServerSideRender>组件来使保存的块属性传递给PHP并呈现内容。
注意:当保存的属性的值无效(类型错误)或保存的值与<SelectControl>中的任何选项不匹配时,就会出现一个常见的块验证问题。在block.json中为属性提供一个有效的默认值是一个非常好的主意,特别是对于API请求。
例如:
src/ud.js
import { useBlockProps, InspectorControls } from '@wordpress/block-editor';
import { Panel, PanelBody, Disabled, SelectControl } from '@wordpress/components';
import ServerSideRender from '@wordpress/server-side-render';
...
export default function Edit({ attributes, setAttributes }) {
const { gender, nationality } = attributes;
return (
<div {...useBlockProps()}>
<InspectorControls>
<Panel>
<PanelBody title="Random User API Options">
<SelectControl
label="Gender"
value={gender}
options={[
{ label: 'Any', value: 'any' },
{ label: 'Male', value: 'male' },
{ label: 'Female', value: 'female' },
]}
onChange={(value) => setAttributes({ gender: value })}
/>
<SelectControl
label="Nationality"
value={nationality}
options={[
{ label: 'Australian', value: 'AU' },
{ label: 'Brazilian', value: 'BR' },
{ label: 'Canadian', value: 'CA' },
]}
onChange={(value) => setAttributes({ nationality: value })}
/>
</PanelBody>
</Panel>
</InspectorControls>
<Disabled>
<ServerSideRender
block="create-block/dynamic-rendering"
attributes={attributes}
/>
</Disabled>
</div>
);
}save.js
export default function save() {
// nothing to save, as block is dynamic..
}最后,我们需要添加一个render_callback来启用静态块来动态呈现。PHP函数将在编辑器中呈现<ServerSideRender>中的内容,并将其作为前端的普通标记(没有反应),例如:
dynamic-rendering.php (或plugin-name.php)
<?php
...
/**
* Registers the block using the metadata loaded from the `block.json` file.
* Behind the scenes, it registers also all assets so they can be enqueued
* through the block editor in the corresponding context.
*
* @see https://developer.wordpress.org/reference/functions/register_block_type/
*/
function create_block_dynamic_rendering_block_init()
{
register_block_type(__DIR__ . '/build', array(
// Add a render_callback to handle dynamic rendering of block with PHP
'render_callback' => 'create_block_dynamic_rendering_output'
));
}
add_action('init', 'create_block_dynamic_rendering_block_init');
/**
* Server-side rendering function for the `create-block/dynamic-rendering` block.
* Requests a random user from external API using the values saved in the block attributes
*
* @see https://developer.wordpress.org/apis/handbook/making-http-requests/getting-data-from-an-external-service/
*/
function create_block_dynamic_rendering_output($attributes)
{
$response = wp_remote_get("https://randomuser.me/api/?gender={$attributes['gender']}&nat={$attributes['nationality']}");
$http_code = wp_remote_retrieve_response_code($response);
return $http_code == 200 ? wp_remote_retrieve_body($response) : "Something went wrong..";
}https://stackoverflow.com/questions/72646319
复制相似问题