• Resolved Bryan Willis

    (@codecandid)


    First off this plugin is incredible for anyone reading this. It’s definitely being overlooked by many on wordpress (I’m guessing because most people don’t understand it). I read the blog post explaining it and I completely agree something like this needs to be added to wordpress core.

    For people who aren’t sure how to use….

    Here are the “Core Widget Types” available for filtering

    • archives
    • calendar
    • categories
    • meta
    • nav_menu
    • pages
    • recent-comments
    • recent-posts
    • rss
    • search
    • tag_cloud
    • text

    You can figure this out by going to your wp-admin/widgets.php. Once there open up your broswer developer tools to view the source.

    In the image above you’ll notice each widget I added to the sidebar. The highlighted one on that screenshot is the search widget and in this case has an id of widget_55-search-12

    So with that information we know the widget ID is search-12 and the widget type is search.

    So now here’s an example:

    If you reference that screenshot again you’ll see that the search widget has an id of widget_59-calendar-7.

    So in this case calendar is our widget type and and calendar-7 is the widget id. Let’s say you are using bootstrap in your theme and want to style the calendar with bootstraps table classes. This is how you’d add them….

    Using the widget type:

    function my_widget_output_filter( $widget_output, $widget_type, $widget_id ) {
        if ( 'calendar' == $widget_type ) {
            $widget_output = str_replace('<table id="wp-calendar', '<table class="table table-responsive table-condensed" id="wp-calendar', $widget_output);
        }
        return $widget_output;
    }
    add_filter( 'widget_output', 'my_widget_output_filter', 10, 3 );

    Using the widget id:

    function my_widget_output_filter( $widget_output, $widget_type, $widget_id ) {
        if ( 'calendar-7' == $widget_id ) {
            $widget_output = str_replace('<table id="wp-calendar', '<table class="table table-responsive table-condensed" id="wp-calendar', $widget_output);
        }
        return $widget_output;
    }
    add_filter( 'widget_output', 'my_widget_output_filter', 10, 3 );

    Hope this helps some people out.

    https://www.ads-software.com/plugins/widget-output-filters/

Viewing 2 replies - 1 through 2 (of 2 total)
  • Thread Starter Bryan Willis

    (@codecandid)

    Philip, I actually have a couple questions as well on the best way of using this if you don’t mind!

    First question: What is the best way to do multiple items at once?

    For example this is the format I have now:

    if ( 'target_widget_type_1' == $widget_type ) {
    
        }
        elseif ( 'target_widget_type_2' == $widget_type ) {
    
        }

    I saw on your blog example though you had it like this:

    if ( 'target_widget_type_1' == $widget_type ) {
    
        }
        if ( 'target_widget_type_2' == $widget_type ) {
    
        }

    There’s also this….

    switch( $widget_type ) {
            case 'target_widget_type_1' :
    
                break;
            case 'target_widget_type_2' :
    
                break;

    Second question: Say I was filtering the search widget, but then I was also filtering a specific widget by the id, does the widget with the id take precedence over the widget category or is it strictly whichever one is added later? I notice that you are using 10 as the priority. If I added a couple filters and I wanted to ensure one was executed later would I be ok adding a later priority here?

    Thanks for your help!

    Plugin Author Philip Newcomer

    (@philipnewcomer)

    Bryan,

    Regarding your first question, functionally all three examples will give the same result, so here it’s really a matter of personal preference and best practice. If you have more than a couple of if statements you’d definitely want to use the switch, as it will be easier to read and maintain.

    Regarding your question on priorities, the only priority that comes into play here is the WordPress filter priority. The filters will be processed in order of weight, so you can change the weight of your callback function when you register each hook in order to change the order in which your filters are run. For any filters that have been registered with the same weight, WordPress will process them in the order in which they were registered.

    You could just put all widget filtering in one function and control the order there in your source code, with one filter. Again, functionally all these will give the same result, so it’s a matter of personal preference.

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘How to use’ is closed to new replies.