Wasteland
Wasteland

Reputation: 5399

Wordpress - Gutenberg - shortcode not rendering

I've switched on Gutenberg and am trying to create a shortcode on a page. This is my code in functions.php

// Enable shortcodes in text areas
add_filter( 'widget_text', 'shortcode_unautop');
add_filter( 'widget_text', 'do_shortcode');

// Enable shortcodes
add_filter( 'the_excerpt', 'shortcode_unautop');
add_filter( 'the_excerpt', 'do_shortcode');


function first_shortcode() { 
	$content = '<h1>This is my first shortcode</h1>';
	return $content;
}
add_action('my_shortcode','first_shortcode');

In the post, I've put [my_shortcode] in the shortcode section, as follows:

enter image description here

But when I display the page, it just renders [my_shortcode]. I'm running Wordpress 4.9.8

Thank you

Upvotes: 0

Views: 2810

Answers (1)

Yogesh Garg
Yogesh Garg

Reputation: 333

you are using wrong function to generate Shortcode

Use add_shortcode to create shortcode rather then add_action

Please use this code..

function first_shortcode() { 
  $content = '<h1>This is my first shortcode</h1>';
  return $content;
}
add_shortcode('my_shortcode', 'first_shortcode');

Upvotes: 5

Related Questions