Advertisement
Guest User

Untitled

a guest
Jul 28th, 2016
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 3.46 KB | None | 0 0
  1. <?php
  2. /**
  3. Plugin Name: Custom Post Field
  4. Plugin URI:
  5. Description:
  6. Author: ABC
  7. Version: 1.0
  8. Author URI: 24h.com.vn
  9. */
  10.  
  11. //exit if acessed directly
  12. if(!defined('ABSPATH')){
  13.     exit();
  14. }
  15.  
  16. //Add new Metabox to all post type
  17. function add_my_custom_metabox(){
  18.     $types = get_post_types(); //apply for all post_types;
  19.     foreach( $types as $type ) {
  20.         add_meta_box('featured-meta', 'Custom_Post_Field', 'custom_meta_featured', $type, 'side','high');
  21.         //side là bên trái, đổi thành normal để ở dưới, còn high là priority, xuất hiện ở đầu
  22.     }
  23. }
  24.  
  25. add_action('add_meta_boxes','add_my_custom_metabox');
  26.  
  27. function custom_meta_featured($post){
  28.     $custom_field_name = get_post_meta($post->ID,'custom_field_name', true);
  29.     //$post là biến toàn cục có sẵn, tham số thứ 2 là key, tham số thứ 3 nếu true là 1 giá trị, false là mảng giá trị
  30.     ?>
  31.     <label for="custom-id">My Custom Field Name</label>
  32.     <input type = "text" name="custom-field-name" id="custom-name" value=" <?php echo esc_attr($custom_field_name); ?>"/>
  33.     <?php
  34. }
  35.  
  36. function save_custom_field_value($post_id){ //$post_id là ID của post hiện tại
  37.     $field_name = sanitize_text_field($_POST['custom-field-name']);
  38.     //apply_filters( 'wp_insert_post_data', 'filter_handler' );
  39.     update_post_meta($post_id,'custom_field_name', $field_name);
  40. }
  41. add_action('save_post','save_custom_field_value',10,1);
  42.  
  43.  
  44.  
  45. ///Start Validate Custom Field Whose Post Type Is Post
  46. add_action('admin_enqueue_scripts-post.php', 'ep_load_jquery_js');  
  47. add_action('admin_enqueue_scripts-post-new.php', 'ep_load_jquery_js');  
  48. function ep_load_jquery_js(){
  49.     global $post;
  50.     if ( $post->post_type == 'post' ) {
  51.         wp_enqueue_script('jquery');
  52.     }
  53. }
  54.  
  55. add_action('admin_head-post.php','ep_publish_admin_hook');
  56. add_action('admin_head-post-new.php','ep_publish_admin_hook');
  57.  
  58. function ep_publish_admin_hook(){
  59.     global $post;
  60.     if ( is_admin() && $post->post_type == 'post' ){
  61.         ?>
  62.         <script language="javascript" type="text/javascript">
  63.             jQuery(document).ready(function(){
  64.  
  65.                 jQuery('#publish').click(function() {
  66.                     if(jQuery(this).data("valid")) {
  67.                         return true;
  68.                     }
  69.                     var form_data = jQuery('#post').serializeArray();
  70.                     form_data = jQuery.param(form_data);
  71.  
  72.                     var data = {
  73.                         action: 'ep_pre_product_submit',
  74.                         security: '<?php echo wp_create_nonce('pre_publish_validation'); ?>',
  75.                         form_data: form_data
  76.                     };
  77.  
  78.                     jQuery.post(ajaxurl, data, function(response) {
  79.                         if (response.indexOf('true') > -1 || response == true){
  80.                             jQuery("#post").data("valid", true).submit();
  81.                         } else {
  82.                             alert("please correct the following errors: " + response);
  83.                             jQuery("#post").data("valid", false);
  84.                         }
  85.  
  86.                         //hide loading icon, return Publish button to normal
  87.                         jQuery('#ajax-loading').hide();
  88.                         jQuery('#publish').removeClass('button-primary-disabled');
  89.                         jQuery('#save-post').removeClass('button-disabled');
  90.                     });
  91.                     return false;
  92.                 });
  93.             });
  94.         </script>
  95.         <?php
  96.     }
  97. }
  98.  
  99. add_action('wp_ajax_ep_pre_product_submit', 'ep_pre_product_submit_func');
  100. function ep_pre_product_submit_func() {
  101.     //simple Security check
  102.     check_ajax_referer( 'pre_publish_validation', 'security' );
  103.     parse_str( $_POST['form_data'], $vars);
  104.  
  105.     $value = trim($vars['custom-field-name']);
  106.  
  107.     if(empty($value)){
  108.         echo 'Custom Field Can Not Empty';
  109.         die();
  110.     }
  111.  
  112.     echo 'true';
  113. }
  114. ///End Validate Custom Field Whose Post Type Is Post
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement