在WordPress中添加的通知是不可拒绝的



我正在做我的WordPress插件,我已经根据要求添加了通知,但通知是不可忽略的。

My Code Added:

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>';
}

我已经添加了这段代码,但是当单击关闭按钮时,通知是不允许的。如有任何帮助,不胜感激。

使用admin_noticeshook和适当的标记并从标记中删除

<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标签。现在通知是不允许的。

重要提示:

你的代码每次都会根据你的逻辑显示注意,可能像findtext和replacetext那样添加位置检查是有意义的。

最新更新