gutenberg icon indicating copy to clipboard operation
gutenberg copied to clipboard

image_default_link_type is ignored by Gutenberg

Open Xoliul opened this issue 8 years ago • 22 comments

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.

Xoliul avatar Sep 25 '18 18:09 Xoliul

Please fix this annoying bug :(

TomasValenta avatar Dec 16 '18 22:12 TomasValenta

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,
        ) ),
    );
}

TomasValenta avatar Dec 17 '18 22:12 TomasValenta

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?

Xoliul avatar Jan 03 '19 13:01 Xoliul

For example functions.php in the child theme folder ;) https://codex.wordpress.org/Functions_File_Explained

TomasValenta avatar Jan 03 '19 21:01 TomasValenta

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

trlanfeng avatar Jan 07 '19 14:01 trlanfeng

You have a working example three post above :)

TomasValenta avatar Jan 07 '19 17:01 TomasValenta

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.

HmCody avatar Jan 07 '19 22:01 HmCody

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?

PTArtist avatar Feb 18 '19 19:02 PTArtist

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.

PTArtist avatar Feb 19 '19 05:02 PTArtist

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.

HmCody avatar Feb 20 '19 17:02 HmCody

I think we should have an option for default Media Link on Gallery. The pictures are ridiculously little O_o !

SuzuKube avatar Mar 30 '19 06:03 SuzuKube

Also reported on the forums. I was able to reproduce it.

iandunn avatar May 31 '19 00:05 iandunn

It's been a while since the last update. Is there a timeline when this going to be fixed?

erfanio avatar Sep 22 '19 23:09 erfanio

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,
        ) ),
    );
}

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

Furiouss38 avatar Nov 27 '19 14:11 Furiouss38

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.

noisysocks avatar Apr 23 '20 03:04 noisysocks

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?

helloiamgergo avatar May 05 '20 21:05 helloiamgergo

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?

idarek avatar Jun 18 '20 08:06 idarek

any news? it's so annoying :(

vigilantspb avatar Aug 07 '20 21:08 vigilantspb

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.

web-jst avatar Sep 09 '20 23:09 web-jst

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.

goaround avatar Sep 14 '20 13:09 goaround