function register_shortcodes(){
add_shortcode( 'my_shortcode', 'my_shortcode' );
}
add_action( 'init', 'register_shortcodes');
function my_shortcode( $atts ) {
return "--my_shortcode--";
}
Adding this code to a plugin I’ve been making (specifically for this) doesn’t do anything. I don’t think the function my_shortcode( $atts )
is even called.
I’ve tried putting “[my_shortcode]
” in posts, comments, template(s).php. return "--my_shortcode--";
never executes.
I’ve also put the above piece of code in functions.php
which gives me error
Fatal error: Call to undefined function add_shortcode() in C:localhost~wp-includesfunctions.php on line 17
line 17 : add_shortcode( 'my_shortcode', 'my_shortcode' );
As Milo indicated:
Addressing your other question from the comments, yes, this code can be wrapped up into a plugin file. Once the plugin has been activated, you’d just need to place your
[my_shortcode]
shortcode within a post/page content area, and it will be executed.