Set default image link target in Gutenberg image block

I want to set the default target of the image block in Gutenberg to “Media File”:

Link target

I’ve found solutions for the classic editor, and one that appears to work for Gutenberg galleries by editing the block’s template, but I was under the impression that the intended way to modify the editor’s behaviour is via JavaScript hooks in the block editor.

Is there a way to set the default target using a JavaScript hook in the block editor?

UPDATE: I made a pull request on the Gutenberg GitHub repository to try to implement the ability to override the default link target with a plugin. The latest version is here. As of writing, this has not yet been merged.

3 s
3

This was (finally) fixed to Gutenberg (and will be applicable for both the image and the gallery blocks); and will add an option to WordPress options’ database table.
this was done in
https://github.com/WordPress/gutenberg/pull/25578 and https://github.com/WordPress/gutenberg/pull/25582

It’ll be added to WordPress 5.6 but it’s already available in the Gutenberg plugin.

To link the image to the file by default, issue the following in your theme or plugin:

add_action( 'after_setup_theme', function() {
    update_option( 'image_default_link_type', 'file' );
});

Replace file with attachment if you want to link to the attachment page for some reason, or none if you don’t want to the image to have a link.

As for javascript: modifying blocks so that they alter the site’s options is possible in Gutenberg although I haven’t gotten it to work yet, check out:
https://github.com/WordPress/gutenberg/issues/20731

Note that if you try do the image_default_link_type equivalent in javascript, don’t use “file” and “post” as the options (as long as this ticket is open) – see https://github.com/WordPress/gutenberg/issues/25587

Leave a Comment