Skip to content

Instantly share code, notes, and snippets.

@dan-zakirov
Created November 7, 2023 06:02
Show Gist options
  • Save dan-zakirov/6caf185298b5051b8e762b99a3677395 to your computer and use it in GitHub Desktop.
Save dan-zakirov/6caf185298b5051b8e762b99a3677395 to your computer and use it in GitHub Desktop.
Дата создания поста в сайт баре Gutenberg
add_action('enqueue_block_editor_assets', function () {
ob_start();
?>
<script>
window.onload = function () {
const el = wp.element.createElement;
const { registerPlugin } = wp.plugins;
const { PluginSidebar, PluginSidebarMoreMenuItem } = wp.editPost;
const ulStyle = {
padding: '16px',
};
const liStyle = {
listStyleType: 'none',
};
registerPlugin('post-date-sidebar', {
render: function () {
const { getCurrentPost } = wp.data.select('core/editor');
const post = getCurrentPost();
if (post) {
const createdDate = post.date ? new Date(post.date).toLocaleString() : 'Дата не установлена';
const modifiedDate = post.modified ? new Date(post.modified).toLocaleString() : sameDateMessage;
let sameDateMessage = '';
if (createdDate !== 'Дата не установлена' && modifiedDate !== 'Дата не установлена' && createdDate === modifiedDate) {
sameDateMessage = 'нет обновления';
}
return el(PluginSidebar, {
name: 'post-creation-date-sidebar',
icon: 'calendar-alt',
title: 'Дата создания поста',
},
el('ul', { style: ulStyle },
el('li', { style: liStyle }, `Дата создания: ${createdDate}`),
el('li', { style: liStyle }, `Дата обновления: ${sameDateMessage || modifiedDate}`)
)
);
} else {
return null;
}
},
});
// Добавляем элемент в меню "Дополнительно"
registerPlugin('post-creation-date-sidebar-more-menu-item', {
render: function () {
return el(PluginSidebarMoreMenuItem, {
target: 'post-creation-date-sidebar',
}, 'Дата создания поста');
},
});
}
</script>
<?php
wp_add_inline_script('wp-blocks', str_replace(['<script>', '</script>'], '', ob_get_clean()));
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment