Advertisement
Guest User

FUll code custom meta tag

a guest
Mar 27th, 2021
115
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 5.48 KB | None | 0 0
  1. <?php
  2.  
  3. function blog_post_fields_meta_box() {
  4.   add_meta_box(
  5.     'blog_fields_meta_box', // $id
  6.     'Blog Settings', // $title
  7.     'show_blog_fields_meta_box', // $callback
  8.     'page', // $screen
  9.     'normal', // $context
  10.     '55' // $priority
  11.   );
  12. }
  13. add_action( 'add_meta_boxes', 'blog_post_fields_meta_box' );
  14.  
  15. function show_blog_fields_meta_box() {
  16.   global $post;
  17.     $meta = get_post_meta( $post->ID, 'your_fields', true ); ?>
  18.  
  19.   <input type="hidden" name="your_meta_box_nonce" value="<?php echo wp_create_nonce( basename(__FILE__) ); ?>">
  20.  
  21.  
  22.     <p>
  23.       <label for="your_fields[blog_format]">Blog Style/Format : </label>
  24.       <select name="your_fields[blog_format]" id="your_fields[blog_format]">
  25.           <option value="blog-default" <?php selected( @$meta['blog_format'], 'blog-default' ); ?>>Default</option>
  26.           <option value="blog-title-top" <?php selected( @$meta['blog_format'], 'blog-title-top' ); ?>>Title Top</option>
  27.           <option value="blog-image-left" <?php selected( @$meta['blog_format'], 'blog-image-left' ); ?>>Image Left</option>
  28.           <option value="blog-gird-with-featured-post" <?php selected( @$meta['blog_format'], 'blog-gird-with-featured-post' ); ?>>Gird With Featured Post</option>
  29.       </select>
  30.     </p>
  31.  
  32.  
  33.     <p>
  34.       <label for="your_fields[select]">Post Format : </label>
  35.       <select name="your_fields[select]" id="your_fields[select]">
  36.           <option value="list-view" <?php selected( @$meta['select'], 'list-view' ); ?>>List</option>
  37.           <option value="grid-view" <?php selected( @$meta['select'], 'grid-view' ); ?>>Grid</option>
  38.       </select>
  39.     </p>
  40.  
  41.     <p>
  42.         <label for="your_fields[show_thumbnail]">Enable Thumbnail :  
  43.             <input type="checkbox" name="your_fields[show_thumbnail]" value="checkbox" <?php if ( @$meta['show_thumbnail'] === 'checkbox' ) echo 'checked'; ?>>
  44.         </label>
  45.     </p>
  46.  
  47.     <p>
  48.         <label for="your_fields[show_category]">Show Category :
  49.             <input type="checkbox" name="your_fields[show_category]" value="checkbox" <?php if ( @$meta['show_category'] === 'checkbox' ) echo 'checked'; ?>>
  50.         </label>
  51.     </p>
  52.  
  53.     <p>
  54.         <label for="your_fields[show_heading]">Enable Heading :
  55.             <input type="checkbox" name="your_fields[show_heading]" value="checkbox" <?php if ( @$meta['show_heading'] === 'checkbox' ) echo 'checked'; ?>>
  56.         </label>
  57.     </p>
  58.  
  59.     <p>
  60.         <label for="your_fields[show_content]">Show Post Content :
  61.             <input id="show-post-content" type="checkbox" name="your_fields[show_content]" value="checkbox" <?php if ( @$meta['show_content'] === 'checkbox' ) echo 'checked'; ?>>
  62.         </label>
  63.  
  64.       <span>-----></span>
  65.  
  66.       <label for="your_fields[excerpt_length]">Excerpt Length :  
  67.       <input type="number" name="your_fields[excerpt_length]" id="your_fields[excerpt_length]" value="<?php echo @$meta['excerpt_length']; ?>">
  68.         </label>
  69.     </p>
  70.  
  71.     <p>
  72.         <label for="your_fields[show_read_more]">Show Read More Button :
  73.             <input type="checkbox" name="your_fields[show_read_more]" value="checkbox" <?php if ( @$meta['show_read_more'] === 'checkbox' ) echo 'checked'; ?>>
  74.         </label>
  75.  
  76.       <span>-----></span>
  77.  
  78.       <label for="your_fields[read_more_text]">Read More Text :
  79.         <input type="text" name="your_fields[read_more_text]" id="your_fields[read_more_text]" value="<?php echo @@$meta['read_more_text']; ?>">
  80.       </label>
  81.     </p>
  82.  
  83.  
  84.     <p>
  85.         <label for="your_fields[show_date]">Show Date :
  86.             <input type="checkbox" name="your_fields[show_date]" value="checkbox" <?php if ( @$meta['show_date'] === 'checkbox' ) echo 'checked'; ?>>
  87.         </label>
  88.  
  89.       <span>-----</span>
  90.  
  91.       <label for="your_fields[show_author_name]">Show Author Name :
  92.         <input type="checkbox" name="your_fields[show_author_name]" value="checkbox" <?php if ( @$meta['show_author_name'] === 'checkbox' ) echo 'checked'; ?>>
  93.       </label>
  94.     </p>
  95.  
  96.  
  97.     <p>
  98.         <label for="your_fields[show_total_comments]">Show Comments :
  99.             <input type="checkbox" name="your_fields[show_total_comments]" value="checkbox" <?php if ( @$meta['show_total_comments'] === 'checkbox' ) echo 'checked'; ?>>
  100.         </label>
  101.     </p>
  102.  
  103.  
  104.     <p>
  105.         <label for="your_fields[define_category]">Define Category :
  106.             <input type="text" name="your_fields[define_category]" id="your_fields[define_category]" value="<?php echo @@$meta['define_category']; ?>">
  107.         </label>
  108.     </p>
  109.    
  110.  
  111.    
  112.  
  113.   <?php
  114. }
  115.  
  116.  
  117. function save_your_fields_meta( $post_id ) {
  118.   // verify nonce
  119.   if ( isset($_POST['your_meta_box_nonce']) && !wp_verify_nonce( $_POST['your_meta_box_nonce'], basename(__FILE__) ) ) {
  120.     return $post_id;
  121.   }
  122.   // check autosave
  123.   if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) {
  124.     return $post_id;
  125.   }
  126.   // check permissions
  127.   if ( isset($_POST['post_type']) && 'page' === $_POST['post_type'] ) {
  128.     if ( !current_user_can( 'edit_page', $post_id ) ) {
  129.       return $post_id;
  130.     } elseif ( !current_user_can( 'edit_post', $post_id ) ) {
  131.       return $post_id;
  132.     }
  133.   }
  134.  
  135.   $old = get_post_meta( $post_id, 'your_fields', true );
  136.   // $new = @$_POST['your_fields'];
  137.  
  138.   if ( isset($_POST['your_fields']) && $_POST['your_fields'] !== $old ) {
  139.     update_post_meta( $post_id, 'your_fields', $_POST['your_fields'] );
  140.   } elseif ( isset($_POST['your_fields']) && '' === $_POST['your_fields'] && $old ) {
  141.     delete_post_meta( $post_id, 'your_fields', $old );
  142.   }
  143. }
  144.  
  145. add_action( 'save_post', 'save_your_fields_meta' );
  146.  
  147.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement