php默认确认按钮,php – 如何在WordPress发布按钮中添加确认对话框?

我正在开发一个wordpress插件,它创建了一个带有一些特殊字段的自定义帖子类型“知识库文章”.我希望发布按钮警告用户在提交之前是否缺少字段.我该怎么做呢?是否有适当的钩子来插入此行为?

解决方法:

您可以在wp-admin / js / common.js中使用基本的客户端表单验证脚本,该脚本默认包含在所有管理页面中.

首先,通过在后期编辑屏幕上运行的插件添加操作:

add_action('admin_print_scripts', 'my_validation_script');

function my_validation_script() {

global $post_type;

if(isset($post_type) && $post_type == 'knowledgebase_article') {

wp_enqueue_script('my-validation-script', network_site_url() . '/wp-content/plugins/your-plugin-folder/my-validation-script.js', array('jquery', 'common'));

}

}

接下来,编写小的javascript表单处理程序来实际处理表单提交:

jQuery(document).ready(function(){

//'my_required_field' is the class name of the DIV or TD tag *containing* the required form field

jQuery('.my_required_field').addClass('form-required');

//'post' is the ID of the main form on the new post screen

jQuery('#post').submit(function(){

//the validateForm function lives in wp-admin/js/common.js

if ( !validateForm( jQuery(this)) ) {

alert('Please enter a value');

return false; //stop submitting the form

} else {

//alert('all required fields have some input');

}

});

});

您可以通过访问标签编辑页面,然后创建一个没有名称的新标签来查看此验证器的行为.该字段的容器变为红色警告您问题.

我在我的测试中测试了这个并且它有效.希望它可以帮助你.

标签:php,wordpress,wordpress-plugin

来源: https://codeday.me/bug/20190903/1795848.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值