• Mathazzar

    (@mathazzar)


    Hi there,

    I’m hoping for a way to have WordPress automatically add a couple of specific attributes to all image tags on the site.

    It’s for a photography site and I’d like to gently discourage click+drag and right-click using the oncontextmenu="return false;" and ondragstart="return false;" attributes for all <img> tags.

    My question is…how would I go about doing this? Is there a function I could add to functions.php or a plugin I could use to automatically re-write all image tags to include those attributes? I haven’t found one yet, and the one similar discussion around here I found from a year ago didn’t get any replies.

    Anyone have a possible solution I could try?

Viewing 6 replies - 1 through 6 (of 6 total)
  • nickohrn

    (@nickohrn)

    Do you want every single image on the site or just images in post content? Does it have to be an attribute or would you be OK attaching an event handle with JavaScript?

    Thread Starter Mathazzar

    (@mathazzar)

    Hi Nick!

    Either or is fine as the only images besides post content ones are logo images. As for the method, I’m not terribly familiar with this area of development so I defer to best practise and/or your judgement as far as attributes vs. JS event handlers.

    Whatever is leaner to implement, I suppose!

    Thanks very much for the response, hopefully you can point me in the right direction.

    nickohrn

    (@nickohrn)

    Let me whip something up for you. I’ll post again in 1/2 hour.

    Thread Starter Mathazzar

    (@mathazzar)

    That is very kind of you!

    In case it helps, the site in question is visible at https://marius.photography

    nickohrn

    (@nickohrn)

    That’s a nice site you’ve got there!

    I created the simplest plugin I could (sorry it took more than 1/2 hour – had to take the dog out). It uses jQuery’s event handling to prevent the contextmenu and dragstart events on images. The entirety of the plugin text can be found here: https://gist.github.com/nickohrn/9301743

    I zipped it up as a file you can download and then upload to your site. The zip file is available here: https://nickohrn.com/wp-content/uploads/2014/03/ridiculously-naive-image-protection.zip

    I think this should get you most of the way there. If you don’t mind, I’m going to do a short write up on my blog tomorrow about this and would like to mention you if that is OK.

    Thread Starter Mathazzar

    (@mathazzar)

    Nick,

    Thank you very much, not only for taking the time to do this but also for sharing the gist so I can learn something! Another reminder of why this community is awesome.

    And I couldn’t have titled the plugin better myself ?? needless to say my goal isn’t actual protection so much as a rudimentary discouragement.

    By all means do a write-up, and I’ll look forward to reading it. ??

Viewing 6 replies - 1 through 6 (of 6 total)
  • The topic ‘Adding Image Tag Attributes to All Images on Site’ is closed to new replies.