简体   繁体   English

我如何在 JavaScript 中使用 PHP 函数(Gutenberg WordPress)

[英]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.

相关问题 如何在javascript中使用wordpress php函数? - how to use wordpress php functions in javascript? 如何在古腾堡中使用序列化数据? - How can I use serialized data in Gutenberg? 如何将 JavaScript 添加到 Wordpress Gutenberg 自定义块? - How do I add JavaScript to a Wordpress Gutenberg custom block? 如何制作一个可以将任何页面 Gutenberg 转换为 Elementor 和 Elementor 到 Gutenberg 的 Wordpress 插件 - How can I make a Wordpress plugin that can convert any page Gutenberg to Elementor and Elementor to Gutenberg 如何使用php中的javascript函数返回的值? - how can i use returned values from javascript functions in php? WordPress:在functions.php中添加延迟解析JavaScript时未加载古腾堡 - WordPress: Gutenberg not loading when Defer Parsing of JavaScript added in functions.php 如何在Wordpress的functions.php中编写Javascript函数? - How to write a Javascript function inside the functions.php of Wordpress? 如何在JavaScript中使用PHP变量? - How can I use a PHP variable inside of my JavaScript? 如何在JavaScript中使用php函数? - How do I use php functions in javascript? 在检查数据库之后,如何在wordpress functions.php文件中添加javascript - How can I add javascript in wordpress functions.php file after check on Database
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM