shnjavee
shnjavee

Reputation: 13

How to check if text widget has certain content?

I need to check if Text Widget has certain content?

To be more specific, I need to check if Text Widget content has a specific shortcode that starts with:

[button ...

Any help is much appreciated!

Thank you!

Upvotes: 0

Views: 1226

Answers (4)

flowdee
flowdee

Reputation: 633

You can filter text widgets as well and use the has_shortcode function in order find find out if your shortcode was used.

function my_filter_widget_text( $widget_text, $instance, $widget ) {
    $tag = 'button';

    if ( has_shortcode( $instance['text'], $tag ) ) {
        $widget_text .= '<p>blub</p>';
    }

    return $widget_text;
}
add_filter( 'widget_text', 'my_filter_widget_text', 1, 3 );

Hope this helps.

Upvotes: 1

essmahr
essmahr

Reputation: 676

wordpress's has_shortcode() will search a string for the existence of a specific shortcode. Codex documentation can be found here.

Upvotes: 0

Deveoo
Deveoo

Reputation: 473

First you can use a function like this to export a widget content:

if(!function_exists('get_dynamic_sidebar'){
function get_dynamic_sidebar($index = 1)
{
    $sidebar_contents = "";
    ob_start();
    dynamic_sidebar($index);
    $sidebar_contents = ob_get_clean();
    return $sidebar_contents;
}
}

Than i think this tutorial ken help you to finish your work:

Tutorial

Upvotes: 0

Deveoo
Deveoo

Reputation: 473

WordPress shortcodes do not work within text widgets. Why I have no idea but if you need this functionality, it is quite easy to accomplish, simply write this code below in your functions.php:

add_filter( 'widget_text', 'shortcode_unautop');

add_filter( 'widget_text', 'do_shortcode'); 

your shortcodes should now work in your text widgets!

Upvotes: 1

Related Questions