Advertisement
mbis

Primary term

Mar 10th, 2020
561
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.95 KB | None | 0 0
  1. <?php
  2. /*
  3. * Plugin Name:       Permalink Manager - Primary Category
  4. * Plugin URI:        http://maciejbis.net/
  5. * Description:       Plugin that allows to keep select primary categories for posts
  6. * Version:           1.0.0
  7. * Author:            Maciej Bis
  8. * Author URI:        http://maciejbis.net/
  9. * License:           GPL-2.0+
  10. * License URI:       http://www.gnu.org/licenses/gpl-2.0.txt
  11. * Domain Path:       /languages
  12. */
  13.  
  14. /**
  15.  * Set primary term
  16.  */
  17. function bis_primary_term($replacement, $replacement_term, $post, $terms, $taxonomy) {
  18.     $bis_primary_terms = get_post_meta($post->ID, 'bis_primary_terms', true);
  19.  
  20.     // Change primary term if selected for specific post
  21.     if(!empty($bis_primary_terms[$taxonomy])) {
  22.         $replacement_term_id = (int) $bis_primary_terms[$taxonomy];
  23.         $replacement_term = get_term($replacement_term_id, $taxonomy);
  24.  
  25.         $replacement = Permalink_Manager_Helper_Functions::get_term_full_slug($replacement_term, $terms, 2, null);
  26.     }
  27.  
  28.     return $replacement;
  29. }
  30. add_filter('permalink_manager_filter_term_slug', 'bis_primary_term', 9, 5);
  31.  
  32. /**
  33.  * Define custom metaboxes
  34.  */
  35. function bis_add_metaboxes() {
  36.     if(!class_exists('Permalink_Manager_Helper_Functions')) { return; }
  37.  
  38.     $post_types = get_post_types( array('public' => true) );
  39.  
  40.   add_meta_box(
  41.     'bis_primary_term',
  42.     __( 'Primary terms', 'bis' ),
  43.     'bis_primary_term_metabox',
  44.     $post_types,
  45.     'side',
  46.     'high'
  47.   );
  48. }
  49. add_action('add_meta_boxes', 'bis_add_metaboxes');
  50.  
  51. function bis_primary_term_metabox($post) {
  52.     wp_nonce_field( '_bis_metabox_nonce', 'bis_metabox_nonce' );
  53.  
  54.     // Get all available taxonomies
  55.     $taxonomies = get_object_taxonomies($post, 'objects');
  56.     $bis_primary_terms = get_post_meta(get_the_ID(), 'bis_primary_terms', true);
  57.  
  58.     foreach($taxonomies as $taxonomy) {
  59.         // Get terms selected for the post
  60.         $terms = get_the_terms($post, $taxonomy->name);
  61.  
  62.         // If there are no terms selected do not display the field
  63.         if(empty($terms)) { continue; }
  64.     ?>
  65.     <p>
  66.         <label for="bis_primary_terms"><?php echo $taxonomy->label; ?></label>
  67.         <select class="widefat" name="bis_primary_terms[<?php echo $taxonomy->name; ?>]">
  68.             <option>---</option>
  69.             <?php
  70.             foreach($terms as $term) {
  71.                 $selected = selected($term->term_id, $bis_primary_terms[$taxonomy->name], false);
  72.                 echo "<option value=\"{$term->term_id}\" {$selected}>{$term->name}</option>";
  73.             }
  74.             ?>
  75.         </select>
  76.     </p>
  77.     <?php
  78.     }
  79. }
  80.  
  81. /**
  82.  * Save custom fields data inside metaboxes
  83.  */
  84. function bis_metaboxes_save( $post_id ) {
  85.     if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) return;
  86.     if ( ! isset( $_POST['bis_metabox_nonce'] ) || ! wp_verify_nonce( $_POST['bis_metabox_nonce'], '_bis_metabox_nonce' ) ) return;
  87.     if ( ! current_user_can( 'edit_post', $post_id ) ) return;
  88.  
  89.     if ( isset( $_POST['bis_primary_terms'] ) ) {
  90.         update_post_meta( $post_id, 'bis_primary_terms', $_POST['bis_primary_terms'] );
  91.     }
  92.  
  93. }
  94. add_action('save_post', 'bis_metaboxes_save');
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement