Add custom attributes to the DOM using add_filter?

Asked on August 29, 2016 in Customization.

I added some custom properties to the widget’s ‘advanced’ section using ‘layerswp_advanced_component_args’.  The reason is, that
a) I don’t want to subclass/overwrite the affected widgets
b) I will finally put that modification into an extension, so I can use the feature in other themes as well.

Now I want it to get echoed to the DOM using the same technique, but I couldn’t find any documentation or example for this…

Is there a way to add my attributes to the proper node using a filter or action?

tiptronic Power User
Reply
  • Possible Answers to Your Question:

  • 2 Answer(s)
    Answered on August 30, 2016.

    Sure Andy,

    Each widget has a couply of actions in them, for example:

    <?php do_action( 'layers_before_content_widget_inner', $this, $instance ); ?>

    Then you can add a function like this to add a div with the data:

    add_action( 'layers_before_content_widget_inner' , my_widget_data, 10, 3 );
    function my_widget_data( $widget_this, $item_instance = array(), $widget_instance = array()){
    // Some code here
    }
    Reply
    Answered on August 30, 2016.

    Hi Marc,

    thx – but I think it’s unfortunately not that easy since I want to add arbitrary attributes to each element.

    Here’s what I need in the end:

    
    <div id="myid" class="myclass" data-animations="slideInUp">
    
    

    The problem is, that (e.g. on the content.php widget) the template looks like this:

    
    <div id="<?php echo $widget_id; ?>-<?php echo $column_key; ?>" class="<?php echo esc_attr( $classes ); ?> " >
    
    

    …so there’s no room to add other properties to the node (without hacking)..

    To me it seems, the fix is *very* easy:

    There’s already a filter to get the right ‘customclasses’:

    
    $classes = apply_filters( 'layers_content_widget_item_class', $classes, $this, $item_instance );
    
    

    I added this simple line(s) below:

    
    $attributes = apply_filters( 'layers_content_widget_item_attributes', $classes, $this, $item_instance );
    
    ...
    
    <div id="<?php echo $widget_id; ?>-<?php echo $column_key; ?>" class="<?php echo esc_attr( $classes ); ?> " <?php echo $attributes; ?> >
    
    

    Done.

    Unfortunately I must add this to any widget I plan to add custom properties to (read: ‘all’).

    Wouldn’t that make a nice enhancement of the current layerswp-widgets?

    Best

    andy

    I realized that when I had typed my reply. I like your solution, I’ll consider it for the next release it’ll add a lot of customizability to the widgets.

    on August 30, 2016.

    If you need some help, code, ideas, whatever – just let me know… Unfortunately I can’t make pull-requests on GitHub, so it’s on you 😉

    andy

    on August 31, 2016.

    Hi Marc,

    any word on this? With the 1.6 update I had to merge my code again, which is (obviously) pita 😉

    Cheers

    andy

    on December 16, 2016.

    Hi there! Nice stuff, do keep me posted when you post again something like this! 먹튀검증사이트

    on February 8, 2022.

    Pretty good post. I just stumbled upon your blog and wanted to say that I have really enjoyed reading your blog posts. Any way I’ll be subscribing to your feed and I hope you post again soon. Big thanks for the useful info. tablet device price

    on February 16, 2022.

    I am glad you take pride in what you write. This makes you stand way out from many other writers that push poorly written content. Ottawa Rims

    on February 17, 2022.

    비아센터, 정품 비아그라를 온라인에서 판매하고 있는 대한민국 온라인 비아그라 판매 1위 쇼핑몰입니다. 비아센터에서는 가짜 비아그라 판매시 200% 보상 서비스를 진행하고 있으니, 믿고 구입하셔도 됩니다 비아그라 판매

    on February 19, 2022.

    Creative adventure sports that allow people to soar, plunge, twirl, whiz and boing around perilously. This blossary attempts to capture some of the more … sell merchant services

    on February 20, 2022.

    Its like you read my mind! You seem to know so much about this, like you wrote the book in it or something. I think that you could do with a few pics to drive the message home a little bit, but instead of that, this is excellent blog. A fantastic read. I’ll definitely be back.  MyCardStatement.Com

    on March 3, 2022.

    Discover samsung mobile repair in UAE. Authorized Samsung Mobile Repair Center | 20+ Year of Trust Get the best Samsung mobile repair service in Dubai at your doorstep or our axiom care Book your Samsung mobile repair now at affordable price at Axiom care

    on March 10, 2022.

    The a truly great place That i experienced owing to publish it all. The chances of what precisely I want to observe pray during near future you can expect to keep going meant for posting an extremely good place. hue top evening cruise Tours

    on June 13, 2022.

    definitely an interesting read. i usually dont comment. signed Mark Cuban  Shopee

    on June 15, 2022.

    What does it mean to see a snake in a dream ? To see a snake in a dream usually means an enemy. If a person sees a snake in a dream, he has a secret enemy. yuxuda ilan gormek

    on June 19, 2022.

    Watch movies – Watch movies ViP Turkish Dubbed Watch Foreign Movies – Watch movies is the ad-free movie viewing site that comes to mind. sinema izle

    on June 19, 2022.
    Reply