Maxim Mercuriev
Maxim Mercuriev

Reputation: 456

Wordpress shortcode is not called

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?

Upvotes: 0

Views: 808

Answers (1)

Tom&#225;s Cot
Tom&#225;s Cot

Reputation: 1013

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.

Upvotes: 2

Related Questions