Skip to content

Instantly share code, notes, and snippets.

@RiodeJaneiroo
Last active January 31, 2023 13:12
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save RiodeJaneiroo/6b73dcb44f22b4499d48df7b661ef635 to your computer and use it in GitHub Desktop.
Save RiodeJaneiroo/6b73dcb44f22b4499d48df7b661ef635 to your computer and use it in GitHub Desktop.
[Wordpress Default link image media] in Gutenberg set default in image link media #wp #wordpress #gutenberg
function setDefaultHrefImage() {
wp_enqueue_script(
'modify-image-link-destination-default',
get_template_directory_uri() . '/js/setDefaultHrefImage.js',
array('wp-hooks')
);
}
add_action('enqueue_block_editor_assets', 'setDefaultHrefImage');
function modifyLinkDestinationDefault(settings, name) {
if (name !== "core/image") {
return settings;
}
settings.attributes.linkDestination.default = "media";
settings.attributes.align.default = "center";
return settings;
}
wp.hooks.addFilter(
"blocks.registerBlockType",
"my-plugin/modify-linkDestination-default",
modifyLinkDestinationDefault
);
@idarek
Copy link

idarek commented Jun 18, 2020

Does JS file need to be modified to match environment?
my-plugin/modify-linkDestination-default

Tried on mine, and saddly not working

@hyperframed
Copy link

Does JS file need to be modified to match environment?
my-plugin/modify-linkDestination-default

Tried on mine, and saddly not working

The code above works, but only on newly inserted images.

@Socrapop
Copy link

Socrapop commented Aug 5, 2020

Thank you for this, it's a huge game changer!
Do someone have any idea on how to apply this default setting into images transformed from a gallery? I add a lot of photographs into my posts, and since adding them one by one is terribly long and anoying, I use a gallery block that I transform into images. But the RegisterBlockType filter isn't working anymore in that case :/
I found this filter blocks-switchtoblocktype-transformedblock but there is no documentation anywhere on how to use it...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment