[英]how can i use PHP functions (Gutenberg WordPress) inside JavaScript
sorry for asking a very basic question... i am new at WordPress/Gutenberg, and I am not familiar with PHP. trying to write a custom block with javascript at Gutenberg, but I found most of the functions available only work at PHP. how I can use these functions in javascript.. like.. get_post_meta(), or get_the_tags()抱歉问了一个非常基本的问题......我是 WordPress/Gutenberg 的新手,我不熟悉 PHP。试图在 Gutenberg 编写一个带有 javascript 的自定义块,但我发现大多数可用功能仅适用于 PHP。我如何在 javascript 中使用这些函数.. 像.. get_post_meta(), or get_the_tags()
thank you谢谢
Yes, PHP functions can be used inside Gutenberg blocks by creating a dynamic block which calls a PHP function to render the content.是的,PHP 函数可以在古腾堡块中使用,方法是创建一个动态块,调用 PHP function 来呈现内容。 This enables use of all the available PHP & WordPress functions like
get_post_meta()
etc.这允许使用所有可用的 PHP 和 WordPress 函数,如
get_post_meta()
等。
The Developer documentation has an example block code that shows how this is implemented and is a great place to get started. Developer 文档有一个示例块代码,展示了这是如何实现的,是一个很好的起点。 Also, the
<ServerSideRender>
is a useful component that enables rendering of live PHP inside the block editor.此外,
<ServerSideRender>
是一个有用的组件,可以在块编辑器中呈现实时 PHP。
Reviewing the source code of an existing core block (eg. latest posts) that uses PHP to render may also help you get started with building your own block.查看使用 PHP 渲染的现有核心块(例如最新帖子)的源代码也可以帮助您开始构建自己的块。
There is a workaround since PHP outputs text you can use it to write Javascript code that will be executed on the page load or triggered by an event.有一个解决方法,因为 PHP 输出文本,您可以使用它来编写 Javascript 代码,这些代码将在页面加载时执行或由事件触发。 Just don't forget to surround your js code with script tags
只是不要忘记用脚本标签包围你的 js 代码
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.