从自定义php插件文件中的按钮点击调用函数



我创建了一个自定义警报插件,这样每当在wordpress中提交评论时,它就会启动插件功能,显示一个警报,通知用户他们的评论已成功发布。

现在,我可以在发布评论后显示警报,但我希望用户能够删除/关闭警报。我在警报中添加了一个(x(按钮,目的是当用户点击它时,警报就会消失。

这就是我在functions.php中所拥有的,当提交评论时会被激发:

add_action('comment_post', 'comment_custom_alert());

这就是我的自定义警报.php中的内容:

function close_custom_alert() {
echo "hello - testing method.";
// code to hide alert div
}
function comment_custom_alert() {
?>
<div class="custom-alert" id="comment_custom_alert">
<div class="alert-success">
<button type="button" onclick="close_custom_alert()" class="close">&times;</a>
<strong>Success!</strong> Your comment has been posted successfully.
</div>
</div>
<?php
}

但当我点击控制台中的按钮时,我看到close_custom_alert没有定义。不确定我可能错过了什么或做错了什么。

我也试过:

<button type="button" onclick="<?php close_custom_alert() ?>" class="close">&times;</a>

但在尝试之后,我会得到"未捕获的语法错误:意外的标识符">

您可以使用一些js来实现这一点:

this.parentNode.parentNode.remove()

将您的html更新为:

<div class="custom-alert" id="comment_custom_alert">
<div class="alert-success">
<button type="button" onclick="this.parentNode.parentNode.remove()" class="close">&times;</button>
<strong>Success!</strong> Your comment has been posted successfully.
</div>
</div>

最新更新