在 WordPress 中添加的通知不可拒绝 [英] Notices Added In WordPress Are Not Dismissible
问题描述
我正在开发我的 WordPress 插件,并且我已根据要求添加了通知,但这些通知不可取消.
I am working on my WordPress plugin and I have added the notices according to the requirement but the notices are not dismissible.
我的代码已添加:
if($findtext == ""){
$msg= '<div class="notice notice-error is-dismissible"><p>Please enter find text.</p><button type="button" class="notice-dismiss"><span class="screen-reader-text">Dismiss this notice.</span></button></div>';
} elseif ($replacetext == "") {
$msg= '<div class="notice notice-error is-dismissible"><p>Please enter replace text.</p><button type="button" class="notice-dismiss"><span class="screen-reader-text">Dismiss this notice.</span></button></div>';
} else {
$msg= '<div class="notice notice-error is-dismissible"><p>Please select location.</p><button type="button" class="notice-dismiss"><span class="screen-reader-text">Dismiss this notice.</span></button></div>';
}
我已添加此代码,但单击关闭按钮时通知不可关闭.非常感谢任何帮助.
I have added this code but the notice is not dismissible when clicking on the close button. Any help is much appreciated.
推荐答案
使用 admin_notices
钩子并正确标记和从标记中移除
Using admin_notices
hook and properly markup and removing from markup
<button type="button" class="notice-dismiss"><span class="screen-reader-text">Dismiss this notice.</span></button>
可以解决问题.
重写PHP代码:
add_action( 'admin_notices', function () use ( $findtext, $replacetext ) {
if ( $findtext == "" ) {
?>
<div class="notice notice-error is-dismissible"><p>Please enter find text!</p></div>
<?php
return;
}
if ( $replacetext == "" ) {
?>
<div class="notice notice-error is-dismissible"><p>Please enter replace text.</p></div>
<?php
return;
}
?>
<div class="notice notice-error is-dismissible"><p>Please select location.</p></div>
<?php
} );
WordPress 在引擎盖下附加按钮.不需要手动附加 button
标签.现在可以取消通知.
WordPress appends button under the hood. Don't need to append a button
tag manually. Now notices are dismissible.
重要提示:
您的代码每次都按照您的逻辑显示通知,可能像 findtext 和 replacetext 一样添加位置检查是有意义的.
Your code show notices every time by your logic, probably it makes sense to add location checking analogically as findtext and replacetext did.
这篇关于在 WordPress 中添加的通知不可拒绝的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!