• Hi, im trying to override the HTML output of the AVH extended categories plugin widget in my sidebar.

    basically i want to wrap the entire output of the categories list in a HTML5 tag so that i can create a collapse/expand effect as the categories list is very long over 300 categories. so i need to somehow insert/wrap the output of the AVH plugin.

    i need some example code as the guide in the Other Notes section isnt enough help for what im trying to do i dont think. im new to wordpress so far only messing with child themes and removing post meta data that kind of stuff. but dealing with filters and filter functions is a little confusing for me right now. all i need is an example of the filters and filter functions etc and what i need to insert in the widget logic field inside the AVH plugin widget area please.

    thanks in advance for reading this.

    i also posted this on stackoverflow if more details are required about what im trying to achieve.
    just search for this as i cant seem to add the link here
    “override-wordpress-plugin-sidebar-widget-html-output”

    https://wordpress.org/plugins/widget-logic/

Viewing 5 replies - 1 through 5 (of 5 total)
  • a good place to start is to create a functions.php file in a test theme and add the code for the first example filter.

    add_filter('widget_content', 'basic_widget_content_filter');
    function basic_widget_content_filter($content='')
    {   return $content."<PRE>THIS APPEARS AFTER EVERY WIDGET</PRE>";
    }

    and play with that. However if you simply want every widget in a siderbar to output in a specific way you can probably do that more economically with the sidebar config in the theme.

    http://codex.wordpress.org/Function_Reference/register_sidebar

    allows you to specify a ‘before_widget’ and ‘after_widget’ if you don’t like the default HTML used

    Thread Starter iamn00b

    (@iamn00b)

    Hi thanks for your reply.
    after i posted i carried on and used the example code provided in Other Notes section as a starting point. below is what i ended up with and trying out. basically this code kind of worked i.e it did wrap widgets with the <details> tag, however it wrapped ALL widgets in sidebar and even my top nav menu widget too which is in header area. so thats why i tried the $widget_id to target specific widget areas e.g id='extended-categories-3', which is the id of the specific avh extended categories widget area. unfortunately even when i added the $widget_id=’extended-categories-3′ it still wraps ALL widget areas and doesnt target only that specific widget with id=’extended-categories-3. is there some mistake in the code?

    `add_filter(‘widget_content’, ‘details_wrapper’);

    function details_wrapper($content=”, $widget_id=’extended-categories-3′){

    return “<details><summary>$content</summary></details>”;

    }

    the value in the function definition sets a default value for the function, which is the bit between the {curly braces} afterwards. So drop that and just have

    function details_wrapper($content, $widget_id)
    {    … your code here
    }

    and you’ll have to put in ‘if’ code blocks like this

    if ($widget_id=='extended-categories-3') { echo '<details><summary>' }

    to target specific widgets.

    HTH

    Thread Starter iamn00b

    (@iamn00b)

    Hi thanks again for your reply.

    I was very excited to try the code you posted, but im not sure if im interpreting what you suggested correctly. below is what im putting in functions.php but its not working. is this what the code should look like?

    add_filter('widget_content', 'details_wrapper');
    
    function details_wrapper($content, $widget_id){
    
    	 if ($widget_id=='extended-categories-3') {
    
             echo '<details><summary>'
    
             }

    firstly, i checked for syntax examples of a php if statement and there is a ; missing at end of echo line of code. i tried without and it causes Parse error: syntax error, unexpected ‘}’, expecting ‘,’ or ‘;’ so i tried with the ; and the site loads without parse error but all widgets dont display (all sidebar widgets have dissapeared) so obviously that filter is doing something now since adding the if (widget_id==… code.

    my original code so far is the closest ive come to getting it to almost work.

    theres still two problems:
    1. the details tag is wrapping all widgets instead of specific targeted id widgets.

    2. the details tag arrow icon for expand/collapse is showing but doesnt seem to expand/collapse the widgets.

    they all stay expanded and dont collapse when you click on the details tag arrow.
    so does that mean i should be wrapping the details tag around some HTML element WITHIN the widgets output rather than wrapping the WHOLE widgets code. i.e details tag only wrap around <details>summary><H3></H3></summary><details> for example?

    sorry I made a very basic error in my suggestion. DON’T ECHO what you want displayed, you have to RETURN it.

    the function is provided with the variable $content, which is about to be ECHO’d. this function is your chance to change that value and return it.

    {   …
       return "<mytag>".$content."</mytag>";
    }

    Sorry about that – this will have led you astray and made things quite frustrating!

    You seem to be getting along fine apart from that, so good luck!

Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘'widget_content' filter’ is closed to new replies.