WordPress.org

Forums

Adding Image Tag Attributes to All Images on Site (7 posts)

  1. Mathazzar
    Member
    Posted 1 year ago #

    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?

  2. nickohrn
    Member
    Posted 1 year ago #

    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?

  3. Mathazzar
    Member
    Posted 1 year ago #

    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.

  4. nickohrn
    Member
    Posted 1 year ago #

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

  5. Mathazzar
    Member
    Posted 1 year ago #

    That is very kind of you!

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

  6. nickohrn
    Member
    Posted 1 year ago #

    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: http://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.

  7. Mathazzar
    Member
    Posted 1 year ago #

    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 :P 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. :)

Topic Closed

This topic has been closed to new replies.

About this Topic