Images uploaded in customized gallery do not link to media
I am trying to set the default values for uploaded images and galleries to always be full size and link to the media.
As per an answer to this question, I created block variations for the image and gallery blocks in blockvariation.js in my theme’s js folder:
// Gallery block
wp.blocks.registerBlockVariation(
'core/gallery', {
isDefault: true,
attributes: {
linkTo: 'media',
sizeSlug: 'full',
}
}
);
// Image block
wp.blocks.registerBlockVariation(
'core/image', {
isDefault: true,
attributes: {
linkDestination: 'media',
sizeSlug: 'full',
}
}
);
and then enqueued it in the functions.php
function new_gallery() {
wp_enqueue_script('new-gallery', get_template_directory_uri() . '/js/blockvariations.js',
array( 'wp-blocks', 'wp-dom-ready', 'wp-edit-post' )
);
}
add_action( 'enqueue_block_editor_assets', 'new_gallery' );
This works fine for adding/uploading single images and adding images from the media library to a gallery, but when I upload the images directly to the library, only the linkTo attribute of the gallery block is set to “media”. The linkDestination attribute of the images in the library is set to “none”.
How do I get the uploaded images to correctly link to their media?
Leave an answer