Can’t seem to get [shortcode]s to work

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.

Read More

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' );

Related posts

1 comment

  1. As Milo indicated:

    functions.php is a file within your current
    theme
    , not the
    functions.php file in wp-includes. There’s nothing wrong with your
    code as-is as long as it’s in a file that’s actually being loaded at
    the correct time.

    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.

Comments are closed.