Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

image_default_link_type is ignored by Gutenberg #10173

Closed
Xoliul opened this issue Sep 25, 2018 · 22 comments · Fixed by #25578 or #25582
Closed

image_default_link_type is ignored by Gutenberg #10173

Xoliul opened this issue Sep 25, 2018 · 22 comments · Fixed by #25578 or #25582
Assignees
Labels
Backwards Compatibility Issues or PRs that impact backwards compatability [Block] File Affects the File Block [Block] Gallery Affects the Gallery Block - used to display groups of images [Block] Image Affects the Image Block [Feature] Media Anything that impacts the experience of managing media Good First Issue An issue that's suitable for someone looking to contribute for the first time Needs Dev Ready for, and needs developer efforts [Status] In Progress Tracking issues with work in progress [Type] Enhancement A suggestion for improvement.

Comments

@Xoliul
Copy link

Xoliul commented Sep 25, 2018

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.

@designsimply designsimply added the Needs Testing Needs further testing to be confirmed. label Sep 26, 2018
@TomasValenta
Copy link

Please fix this annoying bug :(

@TomasValenta
Copy link

TomasValenta commented Dec 17, 2018

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

@Xoliul
Copy link
Author

Xoliul commented Jan 3, 2019

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?

@TomasValenta
Copy link

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

@trlanfeng
Copy link

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

@TomasValenta
Copy link

TomasValenta commented Jan 7, 2019

You have a working example three post above :)

@HmCody
Copy link

HmCody commented Jan 7, 2019

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.

@PTArtist
Copy link

PTArtist commented Feb 18, 2019

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
Copy link

PTArtist commented Feb 19, 2019

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.

@HmCody
Copy link

HmCody commented Feb 20, 2019

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.

@SuzuKube
Copy link

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

@iandunn
Copy link
Member

iandunn commented May 31, 2019

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

@iandunn iandunn added [Block] File Affects the File Block [Block] Gallery Affects the Gallery Block - used to display groups of images [Block] Image Affects the Image Block [Type] Enhancement A suggestion for improvement. Good First Issue An issue that's suitable for someone looking to contribute for the first time [Feature] Media Anything that impacts the experience of managing media and removed Needs Testing Needs further testing to be confirmed. labels May 31, 2019
@erfanio
Copy link

erfanio commented Sep 22, 2019

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

@gziolo gziolo added the Needs Dev Ready for, and needs developer efforts label Oct 21, 2019
@Furiouss38
Copy link

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

@noisysocks noisysocks added the Backwards Compatibility Issues or PRs that impact backwards compatability label Apr 23, 2020
@noisysocks
Copy link
Member

noisysocks commented Apr 23, 2020

This is still awaiting development. The block editor needs to respect the existing image_default_link_type filter (see #15865) that WordPress has. The value that is returned by this filter could be passed to the image and gallery blocks via editor settings.

@helloiamgergo
Copy link

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?

@idarek
Copy link

idarek commented Jun 18, 2020

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?

@vigilantspb
Copy link

any news? it's so annoying :(

@web-jst
Copy link

web-jst commented Sep 9, 2020

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.

@goaround
Copy link

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.

@web-jst
Copy link

web-jst commented Sep 23, 2020

Thanks for pointing that out.
I have now experimented with removing the filter. After removing it, the gallery block was still correctly displayed on the website but in the editor I got an "Unexpected or Invalid Content" error. So you can't modify the gallery anymore with the filter deactivated.

@mkaz
Copy link
Member

mkaz commented Sep 24, 2020

We just fixed this in #25578 and #25582, it will go out in Gutenberg 9.1 and you can test then, or if you are setup to test from the repository both are merged in.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Backwards Compatibility Issues or PRs that impact backwards compatability [Block] File Affects the File Block [Block] Gallery Affects the Gallery Block - used to display groups of images [Block] Image Affects the Image Block [Feature] Media Anything that impacts the experience of managing media Good First Issue An issue that's suitable for someone looking to contribute for the first time Needs Dev Ready for, and needs developer efforts [Status] In Progress Tracking issues with work in progress [Type] Enhancement A suggestion for improvement.
Projects
None yet