0

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

1
  • Yes a shortcode would work, but the ‘standard’ way would be to create a block for it. Commented Oct 13, 2019 at 11:54

1 Answer 1

0

The 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

2
  • Ok, makes sense. I am doing something similar with a hook and ran into the problem os not quite knowing the wordpress way to access a plugin's classes/models. Is that plugin specific or is there a general way to go about it. So if some plugin has a post type called topic, is there a core way to load its model?
    – Brian
    Commented Oct 14, 2019 at 16:18
  • @Brian The WordPress way of registering a custom post type is by using register_post_type function, so if you want to access posts from a post type registered by a plugin, you can do it through WP_Query by passing the "post_type" => "your post type" as an argument. Commented Oct 14, 2019 at 17:10

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.