简体   繁体   中英

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()

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. This enables use of all the available PHP & WordPress functions like get_post_meta() etc.

The Developer documentation has an example block code that shows how this is implemented and is a great place to get started. Also, the <ServerSideRender> is a useful component that enables rendering of live PHP inside the block editor.

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.

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. Just don't forget to surround your js code with script tags

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM