Advertisement
Guest User

Untitled

a guest
Mar 31st, 2020
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.67 KB | None | 0 0
  1. <?php
  2.  
  3. /*******************************
  4.  * Add / show metabox
  5. *******************************/
  6.  
  7. function add_like_metabox() {
  8.  
  9.    $show_on = array('post');
  10.    if(in_array(get_post_type(), $show_on)){
  11.  
  12.       add_meta_box(
  13.          'like_metabox',
  14.          'Likes',
  15.          'render_like_metabox',
  16.          get_post_type(),
  17.          'side',
  18.          'low'
  19.       );
  20.  
  21.    }
  22.  
  23. }
  24.  
  25. add_action('add_meta_boxes', 'add_like_metabox');
  26.  
  27.  
  28. /*******************************
  29.  * Render the metabox
  30. *******************************/
  31.  
  32. function render_like_metabox(){
  33.    global $post;
  34.    $likers = get_post_meta($post->ID, '_likers', true);
  35.    $likes_count = get_post_meta($post->ID, '_likes_count', true);
  36.  
  37.    wp_nonce_field(__FILE__, 'wp_nonce');
  38.    ?>
  39.    <p>
  40.       <label for="likers">IP address array of the likers:</label>
  41.       <textarea name="_likers" id="likers" class="widefat"><?php echo $likers; ?></textarea>
  42.    </p>
  43.    <p>
  44.       <label for="likes_count">Likes count:</label>
  45.       <input type="text" name="_likes_count" id="likes_count" class="widefat" value="<?php echo $likes_count; ?>" />
  46.    </p>
  47.    <?php
  48. }
  49.  
  50.  
  51.  
  52. /*******************************
  53.  * Save the metabox
  54. *******************************/
  55.  
  56. function save_like_metabox(){
  57.    global $post;
  58.  
  59.    if ( defined('DOING_AUTOSAVE') && DOING_AUTOSAVE ) return;
  60.  
  61.    if($_POST && wp_verify_nonce($_POST['wp_nonce'], __FILE__)){
  62.       if(isset($_POST['_likers']) || isset($_POST['_likes_count'])){
  63.          update_post_meta($post->ID, '_likers', $_POST['_likers']);
  64.          update_post_meta($post->ID, '_likes_count', $_POST['_likes_count']);
  65.       }
  66.    }
  67. }
  68.  
  69. add_action('save_post', 'save_like_metabox');
  70. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement