Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save dlxsnippets/c5f98fc2e08f579063d3755e4d955968 to your computer and use it in GitHub Desktop.
Save dlxsnippets/c5f98fc2e08f579063d3755e4d955968 to your computer and use it in GitHub Desktop.
Change Image Block Attributes with Default Overrides
<?php
/**
* Plugin Name: DLX Change Image Attributes
* Plugin URI: https://dlxplugins.com
* Description: Change the align and default link settings for an image.
* Version: 1.0.0
* Requires at least: 6.0
* Requires PHP: 7.3
* Author: Ronald Huereca
* Author URI: https://mediaron.com
* License: GPL v2 or later
* License URI: https://www.gnu.org/licenses/gpl-2.0.html
*
* @package DLXChangeImageAttributes
*/
namespace DLXPlugins\DLXChangeImageAttributes;
add_filter( 'block_type_metadata', __NAMESPACE__ . '\modify_image_attributes', 10, 1 );
/**
* Set Image Block defaults.
*
* @param array $metadata {
* An array of arguments.
*
* @type string $name Block name.
* @type array $attributes Block attributes.
* }
*/
function modify_image_attributes( $metadata ) {
// Check the block type.
if ( 'core/image' !== $metadata['name'] ) {
return $metadata;
}
$metadata['attributes']['align']['default'] = 'center';
$metadata['attributes']['linkDestination']['default'] = 'media';
// Return the metadata.
return $metadata;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment