I am trying to execute code in a block such that I can display the title and description of a forum topic.
I noticed that the Custom HTML block doesn't seem to allow me to execute PHP.
What is the standard way to do what I am trying to do? Maybe I have to create a custom shortcode?
thanks, Brian
I am trying to execute code in a block such that I can display the title and description of a forum topic.
I noticed that the Custom HTML block doesn't seem to allow me to execute PHP.
What is the standard way to do what I am trying to do? Maybe I have to create a custom shortcode?
thanks, Brian
Share Improve this question asked Oct 13, 2019 at 11:33 BrianBrian 3372 silver badges11 bronze badges 1- Yes a shortcode would work, but the ‘standard’ way would be to create a block for it. – Jacob Peattie Commented Oct 13, 2019 at 11:54
1 Answer
Reset to default 0The easy way would be to create a Shortcode in the functions.php file of your theme and then use it in Gutenberg.
Gutenberg has a built-in block to add shortcodes.
Basically, you will add the following code to your functions.php file,
function get_forum_post_information( $atts ) {
// The PHP Code that you want to execute to get the Title and Description.
return your_stuff;
}
add_shortcode( "get_forum_post_information", "get_forum_post_information" );
Once you are done creating your shortcode, you can simply go to Gutenberg, click the + button to add a new Block, search for Shortcode, then select it.
Gutenberg will put in a block that can be used to run that shortcode. Then simply enter the shortcode in that block as below,
[get_forum_post_information]
Note: You can also pass function parameters through the shortcode if you would like and then access them through the $atts array in the function. A shortcode always returns something to be echoed.
Read more about Shortcodes on the WordPress Codex
Read about the Gutenberg Shortcode Block