12

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 Answers 3

4

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

2
  • 1
    Thanks, I noticed that too and verified it works. Finally it's supported!
    – Sean
    Dec 7, 2020 at 17:57
  • FYI, this works for me~
    – Toma
    Sep 25, 2021 at 7:15
4

Using the filter blocks.registerBlockType we can alter block registration settings. We simply need to target the core/image block and modify the default value for the attribute linkDestination which is none by default.

function modifyLinkDestinationDefault(settings, name) {
    if (name !== "core/image") {
        return settings;
    }

    settings.attributes.linkDestination.default = "media";

    return settings;
}

wp.hooks.addFilter(
    "blocks.registerBlockType",
    "my-plugin/modify-linkDestination-default",
    modifyLinkDestinationDefault
);
8
  • 1
    Worked like a charm, thanks a lot! I wrapped your code in a ( function( wp ) { ... } )( window.wp ); block, and enqueued the script using the enqueue_block_editor_assets PHP hook. The enqueued script should have the wp-hooks dependency specified.
    – Sean
    Feb 2, 2019 at 9:37
  • 1
    And, to set the default for galleries, you must modify the core/gallery block and set settings.attributes.linkTo.default. Gutenberg inconsistencies...
    – Sean
    Feb 2, 2019 at 10:08
  • 1
    @Sean You can see here for what is the purpose of wrapping functions in that way. When you use a script dependency enqueuing your own script, in this case wp-hooks as you indicated, you can then safely use the contents of that dependency in your script.
    – Alvaro
    Feb 2, 2019 at 11:54
  • 2
    Update: unfortunately this solution doesn't actually work. The new default is set, but the href property is not - and this is required for the saved image to actually display the link. It seems that I actually have to somehow execute the onSetLinkDestination function to set the href, but this can't be done until an image is actually uploaded (i.e. not possible on the registerBlockType hook). Any ideas for how this might be fixed?
    – Sean
    Feb 6, 2019 at 18:35
  • 1
    I submitted a pull request to implement this in Gutenberg which was later improved by another user... if you need this feature, please poke the developers on those GitHub threads to implement it.
    – Sean
    Jul 28, 2019 at 19:43
0

The correct default attributes for linkTo in core/gallery it's that :

settings.attributes.linkTo.default = 'file';
1
  • Welcome to wpse! Consider adding more explanation about how to implement this code and what it solves, as it stands this cannot be considered a complete answer. Oct 20, 2021 at 7:00

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.