Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- /**
- Plugin Name: Custom Post Field
- Plugin URI:
- Description:
- Author: ABC
- Version: 1.0
- Author URI: 24h.com.vn
- */
- //exit if acessed directly
- if(!defined('ABSPATH')){
- exit();
- }
- //Add new Metabox to all post type
- function add_my_custom_metabox(){
- $types = get_post_types(); //apply for all post_types;
- foreach( $types as $type ) {
- add_meta_box('featured-meta', 'Custom_Post_Field', 'custom_meta_featured', $type, 'side','high');
- //side là bên trái, đổi thành normal để ở dưới, còn high là priority, xuất hiện ở đầu
- }
- }
- add_action('add_meta_boxes','add_my_custom_metabox');
- function custom_meta_featured($post){
- $custom_field_name = get_post_meta($post->ID,'custom_field_name', true);
- //$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ị
- ?>
- <label for="custom-id">My Custom Field Name</label>
- <input type = "text" name="custom-field-name" id="custom-name" value=" <?php echo esc_attr($custom_field_name); ?>"/>
- <?php
- }
- function save_custom_field_value($post_id){ //$post_id là ID của post hiện tại
- $field_name = sanitize_text_field($_POST['custom-field-name']);
- //apply_filters( 'wp_insert_post_data', 'filter_handler' );
- update_post_meta($post_id,'custom_field_name', $field_name);
- }
- add_action('save_post','save_custom_field_value',10,1);
- ///Start Validate Custom Field Whose Post Type Is Post
- add_action('admin_enqueue_scripts-post.php', 'ep_load_jquery_js');
- add_action('admin_enqueue_scripts-post-new.php', 'ep_load_jquery_js');
- function ep_load_jquery_js(){
- global $post;
- if ( $post->post_type == 'post' ) {
- wp_enqueue_script('jquery');
- }
- }
- add_action('admin_head-post.php','ep_publish_admin_hook');
- add_action('admin_head-post-new.php','ep_publish_admin_hook');
- function ep_publish_admin_hook(){
- global $post;
- if ( is_admin() && $post->post_type == 'post' ){
- ?>
- <script language="javascript" type="text/javascript">
- jQuery(document).ready(function(){
- jQuery('#publish').click(function() {
- if(jQuery(this).data("valid")) {
- return true;
- }
- var form_data = jQuery('#post').serializeArray();
- form_data = jQuery.param(form_data);
- var data = {
- action: 'ep_pre_product_submit',
- security: '<?php echo wp_create_nonce('pre_publish_validation'); ?>',
- form_data: form_data
- };
- jQuery.post(ajaxurl, data, function(response) {
- if (response.indexOf('true') > -1 || response == true){
- jQuery("#post").data("valid", true).submit();
- } else {
- alert("please correct the following errors: " + response);
- jQuery("#post").data("valid", false);
- }
- //hide loading icon, return Publish button to normal
- jQuery('#ajax-loading').hide();
- jQuery('#publish').removeClass('button-primary-disabled');
- jQuery('#save-post').removeClass('button-disabled');
- });
- return false;
- });
- });
- </script>
- <?php
- }
- }
- add_action('wp_ajax_ep_pre_product_submit', 'ep_pre_product_submit_func');
- function ep_pre_product_submit_func() {
- //simple Security check
- check_ajax_referer( 'pre_publish_validation', 'security' );
- parse_str( $_POST['form_data'], $vars);
- $value = trim($vars['custom-field-name']);
- if(empty($value)){
- echo 'Custom Field Can Not Empty';
- die();
- }
- echo 'true';
- }
- ///End Validate Custom Field Whose Post Type Is Post
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement