image_default_link_type is ignored by Gutenberg
The setting image_default_link_type from options.php is not used by Gutenberg: mine is set to "file", yet the editor defaults every image or gallery to the "none" link type. I have to set them all manually instead.
Please fix this annoying bug :(
Finally, I found this working replacement! Put this code to the functions.php file:
<?php
add_action( 'init', 'set_link_to_media_by_default' );
function set_link_to_media_by_default() {
$post_type_object = get_post_type_object( 'post' );
$post_type_object->template = array(
array( 'core/gallery', array(
'linkTo' => 'media',
) ),
);
}
You can also specify another attributes too, for example columns:
<?php
add_action( 'init', 'set_link_to_media_by_default' );
function set_link_to_media_by_default() {
$post_type_object = get_post_type_object( 'post' );
$post_type_object->template = array(
array( 'core/gallery', array(
'linkTo' => 'media',
'columns' => 4,
) ),
);
}
Finally, I found this working replacement!
function gallery_template_to_posts() { $post_type_object = get_post_type_object( 'post' ); $post_type_object->template = array( array( 'core/gallery', array( 'linkTo' => 'media', ) ), ); } add_action( 'init', 'gallery_template_to_posts' );
Any reference what file this is changed in?
For example functions.php in the child theme folder ;)
https://codex.wordpress.org/Functions_File_Explained
I found that the 'Link To' default setting is write in the wp-includes/js/dist/block-library(min).js file at Line 3584.
linkDestination: {
type: 'string',
default: 'none'
},
After I change the default to 'media' , every image that I insert into the post , is 'Link To Media File' as default now. But I don't know how to change the default value in the 'function.php' file
You have a working example three post above :)
Apparently, this is a case of "your mileage may vary."
When I added this code to functions.php, a gallery block was automatically added to new posts and there was no change to link to.
Apparently, this is a case of "your mileage may vary."
When I added this code to functions.php, a gallery block was automatically added to new posts and there was no change to link to.
NOTE there are two different suggestions for adding to functions.php via child theme... Did you use the one that adds the set link function: "function set_link_to_media_by_default" or the other one?
I tried creating a new tiny child theme, to define the above "function set_link_to_media_by_default()", and after some confusion, was able to successfully activate my child theme, BUT new galleries still default to link "none". And I find that by using a child theme I lose all my many randomized header files, and have re-establish them as header files (from medial library) ONE at a TIME rather than by selecting multiple filles... lame! Deactivating this useless child theme does restore my headers, glad to say.
NOTE there are two different suggestions for adding to functions.php via child theme... Did you use the one that adds the set link function: "function set_link_to_media_by_default" or the other one?
Sorry for the confusion. The set link function was the one I tried.
I think we should have an option for default Media Link on Gallery. The pictures are ridiculously little O_o !
Also reported on the forums. I was able to reproduce it.
It's been a while since the last update. Is there a timeline when this going to be fixed?
Finally, I found this working replacement! Put this code to the
functions.phpfile:<?php add_action( 'init', 'set_link_to_media_by_default' ); function set_link_to_media_by_default() { $post_type_object = get_post_type_object( 'post' ); $post_type_object->template = array( array( 'core/gallery', array( 'linkTo' => 'media', ) ), ); }You can also specify another attributes too, for example columns:
<?php add_action( 'init', 'set_link_to_media_by_default' ); function set_link_to_media_by_default() { $post_type_object = get_post_type_object( 'post' ); $post_type_object->template = array( array( 'core/gallery', array( 'linkTo' => 'media', 'columns' => 4, ) ), ); }
When I add this code to functions.php, it's like HmCody said. Links still defined to none and a gallery object is added in each new post.
It's annoying to have to set the link for each image and gutenberg not using image_default_link_type
This is still awaiting development. The block editor needs to respect the existing image_default_link_type filter (see https://github.com/WordPress/gutenberg/issues/15865) that WordPress has. The value that is returned by this filter could be passed to the image and gallery blocks via editor settings.
Any updates? This feature (or we shall call it basic functionality...) would be crucial in my project. I need this to work with Media & Text block. I tried @TomasValenta 's solution. I modified "core/gallery" to "core/media-text" but it's no use. I also tried the "core/image" as Media & Text is based on it. Do you have any ideas?
@WordPress: This issue exists for a long time. Is that so difficult to fix such a small bug?
The above solution by adding code to functions.php doesn't work for. Firstly, it adding gallery block to every new post, which is unneeded.
Every other gallery block is still with none by default. Also, how about past-posts? changing them from none to media?
any news? it's so annoying :(
I tried the following, which seems to work.
Like in this example, you can register a Gutenberg JavaScript file customize_block.js in functions.php:
function myguten_enqueue() {
wp_enqueue_script(
'myguten-script',
get_template_directory_uri() . '/customize_block.js',
array( 'wp-blocks', 'wp-dom-ready', 'wp-edit-post' ),
filemtime(get_template_directory_uri() . '/customize_block.js')
);
}
add_action('enqueue_block_editor_assets', 'myguten_enqueue');
Similar to this example, you can now change the default settings of blocks in customize_block.js:
function changeBlockSettings(settings, name) {
if (name == 'core/gallery') {
settings.attributes.linkTo.default = 'media';
}
return settings;
}
wp.hooks.addFilter(
'blocks.registerBlockType',
'yourNamespace',
changeBlockSettings
);
The possible settings of gallery blocks can be found here. In the same way, I was also able to change the settings of other block types.
The problem with this approche is that the settings.attributes.linkTo.default value will not store inside the block markup because it's the default. If you change the default value later or remove the filter, the block will break.