How to process content in a widget?

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

%%keyword%%

Read More

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!

Related posts

Leave a Reply

2 comments

  1. 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.

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

    // Hook into 'widget_display_callback' filter
    // It allows altering a Widget properties right before it outputs 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!