• Resolved Nick Heurter

    (@nickelanddimenl)


    Hi mate!

    First of all I would like to thank you for this awesome plugin, really cool!

    But I have 1 question. I’m trying to use your plugin to set the settings for the Slick Slider trough the data-attribute ‘data-slick’. (Here you can see how this works: https://kenwheeler.github.io/slick/).

    So I’m adding the following data-attribute to my Post Template core/block: ‘data-slick’. And I insert the following code inside the input field: {“slidesToShow”: 4, “slidesToScroll”: 4}.

    On frontend this results in: data-slick="{"slidesToShow": 4, "slidesToScroll": 4}"

    I tried all different kind of settings, but I can’t get it working.

    Could you please help?

    Thanks already!

    Kind Regards, Nick

    • This topic was modified 2 years, 1 month ago by Nick Heurter.
Viewing 10 replies - 1 through 10 (of 10 total)
  • Plugin Author websevendev

    (@websevendev)

    Do single quotes work?

    {'slidesToShow': 4, 'slidesToScroll': 4}

    Do no quotes work?

    {slidesToShow: 4, slidesToScroll: 4}

    You can’t use normal quotes as they are already encasing the whole attribute value.
    If data-slick="{"slidesToShow": 4, "slidesToScroll": 4}" worked the value of data-slick would be {.

    Thread Starter Nick Heurter

    (@nickelanddimenl)

    @websevendev what a quick response, awesome! You’re faster than lightning!

    But to answer your question: unfortunately not. I’ve tried it, but it doesn’t work. I have created a codepen to show you the non-working and working example.

    As you can see I need to use the following format: data-slick='{"slidesToShow": 4, "slidesToScroll": 4}'.

    Is this possible with your plugin?

    Plugin Author websevendev

    (@websevendev)

    Yeah looks like it’s not possible then, as the attribute values will definitely be wrapped in " as opposed to '.

    Here’s a hack you can use maybe:

    Prepend eval: and use no quotes/single quotes like this: eval:{slidesToShow: 4, slidesToScroll: 4} producing data-slick="eval:{slidesToShow: 4, slidesToScroll: 4}".
    Then add additional JS before initializing the slider:

    $('.slider[data-slick*="eval:"]').each(function() {
      eval('var data = ' + $(this).data('slick').replace('eval:', ''));
      $(this).data('slick', data);
    });
    $('.slider').slick();

    This will evaluate the {slidesToShow: 4, slidesToScroll: 4} as it’s a valid JS object and assign it as the data-slick value. Alternatively you can use a similar method to “fix” the {"slidesToShow": 4, "slidesToScroll": 4} HTML entities with JS as well probably.

    Thread Starter Nick Heurter

    (@nickelanddimenl)

    Thanks a lot mate! This works great!!

    One thing that doesn’t work yet is the following code: eval:{slidesToShow: 4, slidesToScroll: 4, centerMode: true, centerPadding: '60px' }. Because in this situation there are stil quotes around the 60px. But when I try to add the following without the quotations around the 60px, it is also not working: eval:{slidesToShow: 4, slidesToScroll: 4, centerMode: true, centerPadding: 60px }.

    Do you also have a last suggestion how I could work around this? ?? ??

    Plugin Author websevendev

    (@websevendev)

    For me this seems to happen only for dynamic blocks (aka blocks that are rendered server side by PHP not directly with JS). For core/paragraph block eval:{centerPadding:'60px'} works, but adding the same attribute to core/latest-comments for example the quotes are escaped by the esc_attr() function.

    Try adding this line to your child theme’s functions.php:

    remove_filter('afb_sanitize_attribute_value', 'esc_attr');

    Then the value should remain unescaped for dynamic blocks as well.
    Removing it is technically less secure, but the only risks I can think of are authenticated users of your site who can edit posts in the first place but have malicious intent. If you’re the only user of your site then it shouldn’t really matter.

    • This reply was modified 2 years, 1 month ago by websevendev.
    • This reply was modified 2 years, 1 month ago by websevendev.
    Thread Starter Nick Heurter

    (@nickelanddimenl)

    Not all hero’s wear capes (or maybe you do)! But this is truly fantastic mate, it works!! Thanks a lot!! ?? ??

    Thread Starter Nick Heurter

    (@nickelanddimenl)

    One last question. This site has multiple users (so I’m not the only one using the site). What are the possible treats then with this filter:

    remove_filter('afb_sanitize_attribute_value', 'esc_attr');

    Plugin Author websevendev

    (@websevendev)

    Honestly, I don’t think it opens up more possibilities than users with editor permissions already have. Worst case scenario they add some JavaScript that, when you visit the page, will send them your login cookie, granting them admin access.
    They could add a well-crafted attribute that does it using this plugin, but they can just as well add the Custom HTML block to do that. So removing the filter only opens attack vectors when you have previously closed the earlier ones, which I doubt is the case.

    Anyone that has unfiltered_html permissions needs to be trusted basically. It’s like giving someone keys to your house.

    Thread Starter Nick Heurter

    (@nickelanddimenl)

    Thanks for the explanation! ?? And thanks again for your awesome support! I’ll give the plugin 5 stars, for the support and for the working of the plugin. It’s a really cool plugin!

    Thread Starter Nick Heurter

    (@nickelanddimenl)

    The issue is resolved.

Viewing 10 replies - 1 through 10 (of 10 total)
  • The topic ‘Data Attributes JSON Object’ is closed to new replies.