博客支持实况

本文共计 2722 个字,预计阅读时长 10 分钟

我之前一直想给博客支持实况照片,一开始本来是想主题原生适配,但是尝试了几次就放弃了。今天下午的时候偶想到了用插件实现偶然有了灵感。

具体实现方法如下:

创建一个自定义 Gutenberg 区块,使用户能够方便地在区块编辑器中添加支持实况图的区域:

function register_custom_live_photos_block() { wp_register_script( 'custom-live-photos-block', get_template_directory_uri() . '/block.js', array('wp-blocks', 'wp-element', 'wp-editor', 'wp-components', 'wp-i18n'), filemtime(get_template_directory() . '/block.js') ); register_block_type('custom/live-photos-block', array( 'editor_script' => 'custom-live-photos-block', 'render_callback' => 'render_custom_live_photos_block' )); } add_action('init', 'register_custom_live_photos_block'); function render_custom_live_photos_block($attributes) { if (!isset($attributes['photoURL']) || !isset($attributes['videoURL'])) { return ''; } $width = '100%'; $height = '300px'; // Example fixed height, adjust as necessary return sprintf( '<div class="live-photo-wrapper" style="width:%s; height:%s; position:relative;"> <div data-live-photo data-photo-src="%s" data-video-src="%s" style="width:100%%; height:100%%;"></div> </div>', esc_attr($width), esc_attr($height), esc_url($attributes['photoURL']), esc_url($attributes['videoURL']) ); }

插件目录创建一个 JavaScript 文件 block.js,并添加以下代码

(function (blocks, editor, element, components) { var el = element.createElement; var MediaUpload = editor.MediaUpload; var InspectorControls = editor.InspectorControls; var TextControl = components.TextControl; blocks.registerBlockType('custom/live-photos-block', { title: 'Live Photos Block', icon: 'camera', category: 'media', attributes: { photoURL: { type: 'string', default: '' }, videoURL: { type: 'string', default: '' }, width: { type: 'number', default: 400 }, height: { type: 'number', default: 300 } }, edit: function (props) { var attributes = props.attributes; var setAttributes = props.setAttributes; return el( 'div', { className: props.className }, el('p', {}, '选择图片和视频:'), el( MediaUpload, { onSelect: function (media) { setAttributes({ photoURL: media.url }); }, allowedTypes: 'image', render: function (obj) { return el(components.Button, { className: attributes.photoURL ? 'image-button' : 'button button-large', onClick: obj.open }, !attributes.photoURL ? '选择图片' : el('img', { src: attributes.photoURL }) ); } } ), el( MediaUpload, { onSelect: function (media) { setAttributes({ videoURL: media.url }); }, allowedTypes: 'video', render: function (obj) { return el(components.Button, { className: 'button button-large', onClick: obj.open }, '选择视频' ); } } ), el(InspectorControls, {}, el(TextControl, { label: '宽度(px)', value: attributes.width, onChange: function (value) { setAttributes({ width: parseInt(value, 10) || 0 }); } }), el(TextControl, { label: '高度(px)', value: attributes.height, onChange: function (value) { setAttributes({ height: parseInt(value, 10) || 0 }); } }) ) ); }, save: function () { // 后台通过 PHP 渲染,前端保存为空 return null; } }); }( window.wp.blocks, window.wp.editor, window.wp.element, window.wp.components ));

使用方法如下 安装启用插件 在编辑器内找到 LIve Photos Block 按图操作。

点击使用:

选择对应的媒体即可使用。

实况预览:

Live Photo

插件点击下载: