0

Okay, I have created a plugin and now want to provide shortcode to app.
Here is my only file in wp-content/plugins/my-plugin/my-plugin.php

<?php

add_action('init', function() {
    add_shortcode('my-plugin', function() {
        // ... my code
    
        return 'string';
    });
});

I know that plugin is activated and the callback for init is called.
But the shortcode function is never get called.
I add text [my-plugin] to a widget, and it isn't replaced as well.

What do I do wrong? How to correctly register a shortcode?

2
  • which version of PHP do you have? Commented Sep 19, 2014 at 12:09
  • Did you have a look at the Shortcode API in the codex. Also, by default, the text widget does not support shortcodes Commented Sep 19, 2014 at 12:27

1 Answer 1

2

I guess you PHP is at least 5.3, so you can make it work in a widget, you need to add this code.

add_filter('widget_text', 'do_shortcode');

I tested your code and it works.

Sign up to request clarification or add additional context in comments.

1 Comment

Can I place this line in the plugin? I patched theme for now.

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.