Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

Possible Duplicate:
Is There A Hook To Process The Content Of The Text Widget?

I have created a plugin that uses tokens in the body content to dynamically insert certain content. For example:

%%keyword%%

At page request, that would get replaced with a keyword specified in the incoming URL.

I would like to be able to use these tokens in a widget and have them processed the same, but my plugin only knows about the_content and I haven't been able to figure out, how to tell it to parse the content of widgets as well, before final page render.

How would this be done? I suppose it can be done more easily with shortcodes, but I'd like to stay consistent with my token usage.

Appreciate any suggestions!

EDIT:

Minutes after posting I found this post which has given me what I need:

Is There A Hook To Process The Content Of The Text Widget?

Thanks anyway all!

share|improve this question
add comment

marked as duplicate by toscho Jul 18 '12 at 21:19

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

2 Answers

The following code will allow you to make replacements in all Widgets, on all strings present in their $instance arrays.

// Hook into the 'widget_display_callback' filter
// It allows altering a Widget properties right before output in sidebar
add_filter('widget_display_callback', function($instance, $widget, $args){
    // Recursive functions that applies replacements in all string elements of $instance
    $fnFixArray = function($v) use (&$fnFixArray){
        // Dig deeper if this is an array or object
        if(is_array($v) or is_object($v)){
            // Use pointer here for property to satisfy both array/object in one
            // Otherwise for arrays we need $v[$k1] and $v->{$k1} for objects
            foreach($v as $k1 => &v1){
                // Go recursive on elements / properties
                $v1 = $fnFixArray(v1);
            }
            return $v;
        }
        // Don't alter non-strings or empty ones
        if(!is_string($v) or empty($v)) return $v;
        // We found a string, replace stuff in it and return the altered value
        return str_replace('%REPLACEWHAT%', '%REPLACEWITH%', $v);
    };
    return $fnFixArray($instance);
}, 11, 3); // We need 3 arguments and a below normal priority

It's a bit more advanced but it's WHAT you really need.

And the same thing with array_walk_recursive() which recurses objects also:

// Hook into the 'widget_display_callback' filter
// It allows altering a Widget properties right before output in sidebar
add_filter('widget_display_callback', function($instance, $widget, $args){
    // This digs through arrays and objects all the way to non-iterative level
    array_walk_recursive($instance, function(&$value, $key){
        // Don't alter non-strings or empty ones
        if(!is_string($value) or empty($value)) return;
        // We found a string, replace stuff in it and return the altered value
        $value = str_replace('%REPLACEWHAT%', '%REPLACEWITH%', $value);
    });
    // Return the possible altered $instance array
    return $instance;
}, 11, 3);

Have fun!

share|improve this answer
add comment

The filter you're looking for is widget_text. From the Codex:

widget_text
applied to the widget text of the WordPress Text widget. May also apply to some third party widgets as well.

share|improve this answer
    
yep just found this post: wordpress.stackexchange.com/questions/4336/… thanks! –  Jonathan van Clute Oct 18 '11 at 20:29
add comment

Not the answer you're looking for? Browse other questions tagged or ask your own question.