0

I need to parse widget content based on it's ID.

But a widget works like a function that pulls data based on it's arguments, right? So when I pull the widget data, I can only get it's arguments, not the actual output.

How can I parse the widget content based on it's ID?

Example code:

// Update widget rounds automatically
function automatize_games_rounds($instance, $widget, $args){
    // Check if there is a caption
    if (isset($instance['caption']) && !empty($instance['caption'])) {
        // If there is, does it contain the word "round"?
        if (strpos(strtolower($instance['caption']), 'round')) {
            // If yes, it's the kind of widget I'm looking for. Let's get it's ID.
            $id = $args['widget_id'];
            // Now I need to parse the widget output to do some stuff based on it's content, but how can I get the widget output?
        }
    }
    return $instance;
}
add_filter('widget_display_callback','automatize_games_rounds',10,3);
2
  • You mean a specific Widget that exist in a Sidebar?
    – David Lee
    Feb 19, 2017 at 18:42
  • Yes, it exists in a sidebar Feb 19, 2017 at 18:44

2 Answers 2

0

If you have the arguments, you can use the_widget() to output the same widget you are searching.

If you don't have the arguments or they change in someway(they could be grabbed from the database though) you would need to iterate all widget areas and all widgets inside each one to match the ID of the you are searching, and use the_widget().

0

Actually by that way you can't filter every widgets data. WordPress do not provides any native way to filter the final output of any widget. But there is some other way to modify any widget content and they are-

1. Modify the widget’s source files.

Obviously this is not a good method, as your modifications will be lost if WordPress, or the plugin creating the widget is updated.

2. Copy the widget into your own plugin or theme and modify it there.

3. Replace the widget’s original display callback function with a custom function, which then runs the original display callback function, but with filters.

Basically, the widget’s original display callback function is overridden with a new custom function, which runs the widget’s original callback function, but uses output buffering to capture the output, and run it through a filter before displaying it. I got it here Here is the code which can be used in a theme or plugin, which will provide a widget_output filter, along with the widget type and unique widget ID as parameters:

First, we need to replace the widget’s original display callback function with a custom function of own:

function the_dramatist_filter_dynamic_sidebar_params( $sidebar_params ) {

    if ( is_admin() ) {
        return $sidebar_params;
    }

    global $wp_registered_widgets;
    $widget_id = $sidebar_params[0]['widget_id'];

    $wp_registered_widgets[ $widget_id ]['original_callback'] = $wp_registered_widgets[ $widget_id ]['callback'];
    $wp_registered_widgets[ $widget_id ]['callback'] = 'the_dramatist_custom_widget_callback_function';

    return $sidebar_params;

}
add_filter( 'dynamic_sidebar_params', 'the_dramatist_filter_dynamic_sidebar_params' );

Next, we need to create the function referenced in the previous block of code, which will in turn run the widget’s original display callback function, but will capture its output using PHP output buffering. Then, the widget_output filter will be run on the widget’s generated HTML markup, before being output to the page:

function the_dramatist_custom_widget_callback_function() {

    global $wp_registered_widgets;
    $original_callback_params = func_get_args();
    $widget_id = $original_callback_params[0]['widget_id'];

    $original_callback = $wp_registered_widgets[ $widget_id ]['original_callback'];
    $wp_registered_widgets[ $widget_id ]['callback'] = $original_callback;

    $widget_id_base = $wp_registered_widgets[ $widget_id ]['callback'][0]->id_base;

    if ( is_callable( $original_callback ) ) {

        ob_start();
        call_user_func_array( $original_callback, $original_callback_params );
        $widget_output = ob_get_clean();

        echo apply_filters( 'widget_output', $widget_output, $widget_id_base, $widget_id );

    }

}

With that in place, you can do something like this in a plugin or theme, to modify the output of any widget:

function the_dramatist_widget_output_filter( $widget_output, $widget_id_base, $widget_id ) {

    /* To target a specific widget ID: */
    if ( 'target_widget_id' == $widget_id ) {
        // Apply your desired search and replace operations here
    }

    return $widget_output;

}
add_filter( 'widget_output', 'the_dramatist_widget_output_filter', 10, 3 );

Hope the above helps.

Your Answer

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

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