WARNING: This article may be obsolete
This post was published in 2021-09-18. Obviously, expired content is less useful to users if it has already pasted its expiration date.
This post was published in 2021-09-18. Obviously, expired content is less useful to users if it has already pasted its expiration date.
This article is categorized as "Garbage" . It should NEVER be appeared in your search engine's results.
注:
2021-10-07更新:该方法(FULL Size的代码)会让文章保存的内容和Gutenbery自然编写保存的内容不一致(align:center插入的位置不一致)。虽然在显示上并无区别,但仍然不推荐这么做。另外两个效果(自动链接到媒体文件、默认Full Size)不受影响,但这两行设置代码本身已经非常常见了,网上随便一搜就有大把教程。
相关文章:
目的:我需要把Wordpress古腾堡编辑器的图片粘贴改成我想要的样式:插入的图片自动居中、自动链接到媒体文件(对大部分lightbox来说都需要)、默认为FULL Size .
你可能已经在搜索引擎上找到类似这样的答案了:
<?php
add_action( 'after_setup_theme', function() {
update_option( 'image_default_align', 'middle' );
update_option( 'image_default_link_type', 'media' );
update_option('image_default_size ', 'full');
});
但很可惜的是,不知道为什么,现在的Wordpress已经不能正确执行第4句话( update_option( 'image_default_align', 'middle' ); )了(至少在Wordpress 5.8这个大版本是这样),图片总是无法自动居中。
后来我找到了一个补充答案:
https://wordpress.org/support/topic/set-default-image-alignment/
所以最终的完整代码如下:
<?php
add_action( 'after_setup_theme', function() {
// update_option( 'image_default_align', 'middle' );
update_option( 'image_default_link_type', 'media' );
update_option('image_default_size ', 'full');
});
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');
Last Modified in 2021-12-14