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
2
Answers
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
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.