Media PHP Beginner

Stop Images Linking to the File by Default

Make new images in posts have no link by default, instead of linking to the full-size file or attachment page.

Tested on WordPress 7.1 PHP 8.3 No plugin needed Updated
functions.php 3 lines
add_filter( 'pre_option_image_default_link_type', function (): string {
	return 'none';
} );

Where to add this

Recommended A code snippets plugin Survives theme changes and is easy to switch off.
Child theme At the end of your child theme's functions.php file.
Must-use plugin As a .php file in wp-content/mu-plugins, starting with <?php.

Always add PHP to a staging site first, or have FTP or file manager access ready, so you can undo it if something goes wrong.

On some sites, new images link to their own file, so clicking a picture opens the bare image and takes visitors off your page. This snippet makes “None” the default.

How it works

WordPress remembers an image_default_link_type setting for new images. The pre_option_ filter makes it always 'none'. Editors can still add a link to any image themselves.

Things to watch

  • Images already in posts aren’t changed.
  • For click-to-enlarge, use “Turn On Lightbox for All Images” instead of linking to the file.

How to test it

Add a new Image block and open its link settings. No link should be set.

This website uses cookies to enhance your browsing experience and ensure the site functions properly. By continuing to use this site, you acknowledge and accept our use of cookies.

Accept All Accept Required Only