我有一个自定义插件,希望它在某些情况下触发在左角出现和消失的通知,如下图所示:
我找到一些代码in the Gutenberg docs here:const MySnackbarNotice = () => (
<Snackbar>
Post published successfully.
</Snackbar>
);
但将此添加到我的管理排队js脚本显然不起作用。谢谢
我有一个自定义插件,希望它在某些情况下触发在左角出现和消失的通知,如下图所示:
我找到一些代码in the Gutenberg docs here:const MySnackbarNotice = () => (
<Snackbar>
Post published successfully.
</Snackbar>
);
但将此添加到我的管理排队js脚本显然不起作用。谢谢
WordPress有一些全局操作可以在这里使用。如果您想在下角添加自己的通知(如屏幕截图),可以这样做:
wp.data.dispatch("core/notices").createNotice(
"success", // Can be one of: success, info, warning, error.
"This is my custom message.", // Text string to display.
{
type: "snackbar",
isDismissible: true, // Whether the user can dismiss the notice.
// Any actions the user can perform.
actions: [
{
url: \'#\',
label: \'View post\',
},
],
}
);
这里最重要的部分是type: "snackbar"
. 您也可以省略snackbar部分,它将出现在内容上方的UI中:以下是WordPress区块编辑器手册的全文:https://developer.wordpress.org/block-editor/tutorials/notices/虽然Andre的回答部分正确,但仅限于Javascript函数,链接教程也没有提供完整的答案。这是一个完整的解决方案,包括正面和背面:
add_action(\'admin_footer-post.php\',\'my_pre_post_update_hook\');
add_action(\'admin_footer-post-new.php\',\'my_pre_post_update_hook\');
function my_pre_post_update_hook(){
global $post;
?>
<script type="text/javascript">
jQuery(document).ready(function() {
// We listen to Ajax calls made via fetch
var temp_fetch = window.fetch;
window.fetch = function() {
return new Promise((resolve, reject) => {
temp_fetch.apply(this, arguments)
.then((response) => {
if( response.url.indexOf("/wp-json/wp/v2/posts") > -1 &&
response.type === \'basic\'
){
var clone = response.clone();
clone.json().then(function (json) {
if( typeof json.code !== \'undefined\' &&
typeof json.code === \'string\' &&
typeof json.message !== \'undefined\' &&
typeof json.message === \'string\'
){
wp.data.dispatch("core/notices").createNotice(
json.code,
json.message,
{
// type: "snackbar", // Optional
id: \'custom_post_site_save\',
isDismissible: true
}
);
// Close default "Post saved" notice
wp.data.dispatch( \'core/notices\' ).removeNotice(\'SAVE_POST_NOTICE_ID\');
// You can see all active notices by this command:
// wp.data.select(\'core/notices\').getNotices();
}
});
}
resolve(response);
})
.catch((error) => {
reject(error);
})
});
};
// If you want to listen to Ajax calls made via jQuery
// jQuery(document).bind("ajaxSend", function(event, request, settings){
// }).bind("ajaxComplete", function(event, request, settings){
// });
});
</script>
<?php
}
add_action( \'pre_post_update\', \'custom_post_site_save\', 10, 2);
function custom_post_site_save($post_id, $post_data) {
if (wp_is_post_revision($post_id)) { // If this is just a revision, don\'t do anything.
return;
} elseif (isset($post_data[\'post_content\']) && strpos($post_data[\'post_content\'], \'myTextString\') !== false) {
$code = \'error\'; // Can be one of: success, info, warning, error.
$response_body = \'My custom message ...\';
$error = new WP_Error($code, $response_body);
wp_die($error, 200);
}
}
我为我的Wordpress主题创建了一个名为project的自定义帖子类型,效果很好。每个项目都有几个自定义字段,包括一组x和y值,它们指示项目应显示在首页的位置(请参阅下面代码的样式部分:我知道它不起作用,但我正在显示我想要在伪代码中实现的目标)。我可以将x值显示为<?php the_field(\'x\'); ?> 和<?php the_field(\'y\'); ?>, 如WordPress的高级自定义字段文档所定义。但是,如何从PHP到JavaScript获取x和y值,以便