Skip to main content
Login Join
Snippet · PHP

Disable Image Dragging

Shared by Amit Dholiya · July 4, 2026

19 views
Back to Snippets

Helpful for portfolio, photography, and artwork websites.

Steps

  1. Open functions.php.
  2. Add the code below.
  3. Save the file.

Done — images cannot be dragged by visitors.

function disable_image_dragging() {
    ?>
    <script>
    document.addEventListener('dragstart', function(e) {
        if (e.target.tagName === 'IMG') {
            e.preventDefault();
        }
    });
    </script>
    <?php
}
add_action('wp_footer', 'disable_image_dragging');
Know a different way to do this? Add your approach as a variation so folks can compare them side by side.
Submit a variation

0 comments