Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- /*
- * Plugin Name: Permalink Manager - Primary Category
- * Plugin URI: http://maciejbis.net/
- * Description: Plugin that allows to keep select primary categories for posts
- * Version: 1.0.0
- * Author: Maciej Bis
- * Author URI: http://maciejbis.net/
- * License: GPL-2.0+
- * License URI: http://www.gnu.org/licenses/gpl-2.0.txt
- * Domain Path: /languages
- */
- /**
- * Set primary term
- */
- function bis_primary_term($replacement, $replacement_term, $post, $terms, $taxonomy) {
- $bis_primary_terms = get_post_meta($post->ID, 'bis_primary_terms', true);
- // Change primary term if selected for specific post
- if(!empty($bis_primary_terms[$taxonomy])) {
- $replacement_term_id = (int) $bis_primary_terms[$taxonomy];
- $replacement_term = get_term($replacement_term_id, $taxonomy);
- $replacement = Permalink_Manager_Helper_Functions::get_term_full_slug($replacement_term, $terms, 2, null);
- }
- return $replacement;
- }
- add_filter('permalink_manager_filter_term_slug', 'bis_primary_term', 9, 5);
- /**
- * Define custom metaboxes
- */
- function bis_add_metaboxes() {
- if(!class_exists('Permalink_Manager_Helper_Functions')) { return; }
- $post_types = get_post_types( array('public' => true) );
- add_meta_box(
- 'bis_primary_term',
- __( 'Primary terms', 'bis' ),
- 'bis_primary_term_metabox',
- $post_types,
- 'side',
- 'high'
- );
- }
- add_action('add_meta_boxes', 'bis_add_metaboxes');
- function bis_primary_term_metabox($post) {
- wp_nonce_field( '_bis_metabox_nonce', 'bis_metabox_nonce' );
- // Get all available taxonomies
- $taxonomies = get_object_taxonomies($post, 'objects');
- $bis_primary_terms = get_post_meta(get_the_ID(), 'bis_primary_terms', true);
- foreach($taxonomies as $taxonomy) {
- // Get terms selected for the post
- $terms = get_the_terms($post, $taxonomy->name);
- // If there are no terms selected do not display the field
- if(empty($terms)) { continue; }
- ?>
- <p>
- <label for="bis_primary_terms"><?php echo $taxonomy->label; ?></label>
- <select class="widefat" name="bis_primary_terms[<?php echo $taxonomy->name; ?>]">
- <option>---</option>
- <?php
- foreach($terms as $term) {
- $selected = selected($term->term_id, $bis_primary_terms[$taxonomy->name], false);
- echo "<option value=\"{$term->term_id}\" {$selected}>{$term->name}</option>";
- }
- ?>
- </select>
- </p>
- <?php
- }
- }
- /**
- * Save custom fields data inside metaboxes
- */
- function bis_metaboxes_save( $post_id ) {
- if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) return;
- if ( ! isset( $_POST['bis_metabox_nonce'] ) || ! wp_verify_nonce( $_POST['bis_metabox_nonce'], '_bis_metabox_nonce' ) ) return;
- if ( ! current_user_can( 'edit_post', $post_id ) ) return;
- if ( isset( $_POST['bis_primary_terms'] ) ) {
- update_post_meta( $post_id, 'bis_primary_terms', $_POST['bis_primary_terms'] );
- }
- }
- add_action('save_post', 'bis_metaboxes_save');
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement