Support » Plugin: Gutenberg » Set default image alignment

  • Is there some way to make the image block default to center-aligning images?

    I have tried overriding the block in JS using the following code and properly enqueued, but the result is I get two alignment buttons in the editor, which cannot be correct?

    // Modify settings for Core blocks
    wp.hooks.addFilter( 'blocks.registerBlockType',
      'my/change_alignment', ( settings, name ) => {
    
      switch( name ) {
        case 'core/image':
        return lodash.assign( {}, settings, {
          supports: lodash.assign( {}, settings.supports, {
            align: [ 'center']
        } ),
      } );
    }
    
    return settings;
    
    });

    Any help would be appreciated.

Viewing 9 replies - 1 through 9 (of 9 total)
  • I have the same problem. I can get other blocks to align center by default but the core/image block doesn’t seem to work with the same codes that work with the other blocks.

    Thread Starter primitivenet

    (@primitivenet)

    Does anyone have any ideas here? This seems like it should be a standard feature that can be overridden. Am I missing something?

    According to this:

    https://writenowdesign.com/blog/wordpress/wordpress-how-to/change-wordpress-default-image-alignment-link-type/

    Try this:

    add_action( 'after_setup_theme', 'wnd_default_image_settings' );
    
    function wnd_default_image_settings() {
    	update_option( 'image_default_align', 'left' );
    	update_option( 'image_default_link_type', 'none' );
    	update_option( 'image_default_size', 'large' );
    }

    (Adding another comment to get updates on this thread.)

    I’ve been looking for this answer for a while too – tho’ i’m trying to make images align right. I have found that @mastababa ‘s code works for the default_link_type and default_size but i’m not seeing the image align work. (admittedly, they get the code from a post dated 2014, so not a complete surprise that all components remained unchanged)

    I found a more current post here (2021), providing a little bit easier way to access the defaults:
    https://blogambitious.com/automate-image-formatting-wordpress/
    (and cursed myself for not remembering the options page. Doh!)

    When i visit the options page, i can see that I have successfully changed the default setting for image_default_align to “right”, Yet when i make a post, that right alignment isn’t being applied as the default setting. The link type and the size are the two that I MOST wanted fixed, so i’m happy for now, but i will continue to hunt down the alignment answer.

    Same – the 2014 article and the blogambitious article do not work. I can see that the wp-options say center and full-size but it has no effect on adding an image to the block editor. It still defaults to left, large.

    Hi,

    I think it works doing that way.

    // changing default gutenberg image block alignment to "center"
    function change_default_gutenberg_image_block_options (){
      $block_type = WP_Block_Type_Registry::get_instance()->get_registered( "core/image" );
      $block_type->attributes['align']['default'] = 'center';
    }
    add_action( 'init', 'change_default_gutenberg_image_block_options');

    Hope it will help, let me know.

    @etiennesamson Thanks. That worked for me. I changed the align default to ‘right’ for my needs and it’s working.

    I was curious about what attributes were there to be poked at and found this page here, seemed pretty handy so i thought i would share.
    https://design.oit.ncsu.edu/docs/gutenberg/block-attributes/#block-coreimage

    J A

    Hi @jageo.

    Thank you for that page 🙂
    It is very usefull, just missing the available possibilities for each attributes.

Viewing 9 replies - 1 through 9 (of 9 total)
  • You must be logged in to reply to this topic.