WordPress教程

如何在WordPress发布按钮中添加确认对话框

阿里云

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

您可以在 wp-admin / js / common.js 中使用基本的客户端表单验证脚本,该脚本默认包含在所有管理页面中。首先,通过在后期编辑屏幕上运行的插件添加操作:

也想出现在这里?联系我们
创客主机
  1.     add_action('admin_print_scripts', 'my_validation_script');
  2.     function my_validation_script() {
  3.         global $post_type;
  4.         if(isset($post_type) && $post_type == 'kNowledgebase_article') {
  5.             wp_enqueue_script('my-validation-script', network_site_url() . '/wp-content/plugins/your-plugin-folder/my-validation-script.js', array('jquery', 'common'));
  6.         }
  7.     }

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

  1.     jQuery(document).ready(function(){
  2.         //'my_required_field' is the class name of the DIV or TD tag *containing* the required form field
  3.         jQuery('.my_required_field').addClass('form-required'); 
  4.         //'post' is the ID of the main form on the new post screen
  5.         jQuery('#post').submit(function(){ 
  6.                 //the validateForm function lives in wp-admin/js/common.js
  7.                 if ( !validateForm( jQuery(this)) ) { 
  8.                         alert('Please enter a value'); 
  9.                         return false; //stop submitting the form
  10.                 } else {
  11.                         //alert('all required fields have some input');
  12.                 }
  13.         });
  14.     });

您可以通过访问标签编辑页面,,然后创建一个没有名称的新标签来查看此验证器的行为。该字段的容器变为红色警告您问题。我在我的测试中测试了这个并且它有效,希望它可以帮助你.

如何在 WordPress 发布按钮中添加确认对话框

已有 149 人购买
查看演示升级 VIP立刻购买

收藏
(0)

发表回复

热销模板

Ashade - 作品展示摄影相册WordPress汉化主题
LensNews

本站承接 WordPress / PbootCMS / DedeCMS 等
系统建站、仿站、开发、定制等业务!