Advertisement
firoze

Wordpress custom meta checkbox

Oct 3rd, 2019
136
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.95 KB | None | 0 0
  1. <?php
  2.  
  3. /*    
  4. when specific value is set then it will set that's meta_value
  5.     suppose value="1"
  6.     // here meta_value has been set up 1
  7.    
  8.     like
  9.    
  10.     <input type="checkbox" id="myText" class="regular-text" name="myText" value="1" <?php if(get_post_meta($post->ID, 'myText', true)):?> checked <?php endif;?>>
  11.    
  12.     if meta value suppose in <input >
  13.    
  14.     value not set OR set
  15.    
  16.     like
  17.    
  18.     <input type="checkbox" id="myText" class="regular-text" name="myText"  <?php if(get_post_meta($post->ID, 'myText', true)):?> checked <?php endif;?>>
  19.    
  20.     then value set auto meta_value "on"
  21.    
  22. */
  23.    
  24.    
  25. /**
  26.  * Register meta box(es).
  27.  */
  28. function wpdocs_register_meta_boxes() {
  29.     add_meta_box(
  30.         'meta-box-id',  // uniqueue id
  31.         __( 'Featured Posts','customquery' ), // Title name and textdomain
  32.         'wpdocs_my_display_callback',     // callback function
  33.          array(
  34.             'post'
  35.          )  // post type
  36.         );
  37. }
  38. add_action( 'add_meta_boxes', 'wpdocs_register_meta_boxes' );
  39.  
  40. /**
  41.  * Meta box display callback.
  42.  *
  43.  * @param WP_Post $post Current post object.
  44.  */
  45. function wpdocs_my_display_callback( $post ) {
  46. // Display code/markup goes here. Don't forget to include nonces!
  47. ?>
  48. <label for="myText"><?php _e("Show Your Post As Featured Post","customquery");?><label/>
  49. <input type="checkbox" id="myText" class="regular-text" name="myText" value="1" <?php if(get_post_meta($post->ID, 'myText', true)):?> checked <?php endif;?>>
  50. <?php
  51. }
  52.  
  53. /**
  54.  * Save meta box content.
  55.  *
  56.  * @param int $post_id Post ID
  57.  */
  58. function wpdocs_save_meta_box( $post_id ) {
  59.     // Save logic goes here. Don't forget to include nonce checks!
  60.     if(isset($_POST["myText"])){
  61.         update_post_meta($post_id, 'myText', sanitize_text_field($_POST['myText']) );
  62.     }
  63. }
  64. add_action( 'save_post', 'wpdocs_save_meta_box' );
  65.  
  66.  
  67. // Show / display this data anywehere you need/want
  68. //echo get_post_meta($post->ID, 'myText', true);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement