Advertisement
SergeyBiryukov

Category Links To

Dec 9th, 2014
303
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.52 KB | None | 0 0
  1. <?php
  2. /*
  3. Plugin Name: Category Links To
  4. Plugin URI: http://ru.forums.wordpress.org/topic/43423
  5. Description: Allows you to make a WordPress category link to an external URL of your choosing, instead of its WordPress URL.
  6. Author: Sergey Biryukov
  7. Author URI: http://profiles.wordpress.org/sergeybiryukov/
  8. Version: 0.1
  9. */
  10.  
  11. class Category_Links_To {
  12.  
  13.     function __construct() {
  14.         add_filter( 'category_add_form_fields',  array( $this, 'add_field_to_add_category_form' ), 1 );
  15.         add_filter( 'category_edit_form_fields', array( $this, 'add_field_to_edit_category_form' ), 1 );
  16.         add_filter( 'category_link',             array( $this, 'filter_category_link' ), 10, 2 );
  17.  
  18.         add_action( 'created_category',          array( $this, 'save_category_link' ) );
  19.         add_action( 'edited_category',           array( $this, 'save_category_link' ) );
  20.     }
  21.  
  22.     function get_options() {
  23.         return get_option( 'category_links_to', array() );
  24.     }
  25.  
  26.     function save_options( $options ) {
  27.         update_option( 'category_links_to', $options );
  28.     }
  29.  
  30.     function add_field_to_add_category_form() { ?>
  31.         <div class="form-field term-link-wrap">
  32.             <label for="tag-link">Ссылка</label>
  33.             <input name="term-link" id="tag-link" type="text" value="" size="40" />
  34.             <p>Здесь можно указать адрес, на который будет вести ссылка на рубрику.</p>
  35.         </div>
  36.         <?php
  37.     }
  38.  
  39.     function add_field_to_edit_category_form( $term ) {
  40.         $options = $this->get_options();
  41.  
  42.         if ( isset( $options[ $term->term_id ] ) ) {
  43.             $term_link = $options[ $term->term_id ];
  44.         } else {
  45.             $term_link = '';
  46.         }
  47.         ?>
  48.         <tr class="form-field term-link-wrap">
  49.             <th scope="row"><label for="link">Ссылка</label></th>
  50.             <td><input name="term-link" id="link" type="text" value="<?php echo esc_attr( $term_link ); ?>" size="40" />
  51.             <p class="description">Здесь можно указать адрес, на который будет вести ссылка на рубрику.</p></td>
  52.         </tr>
  53.         <?php
  54.     }
  55.  
  56.     function filter_category_link( $term_link, $term_id ) {
  57.         $options = $this->get_options();
  58.  
  59.         if ( isset( $options[ $term_id ] ) ) {
  60.             $term_link = esc_url( $options[ $term_id ] );
  61.         }
  62.  
  63.         return $term_link;
  64.     }
  65.  
  66.     function save_category_link( $term_id ) {
  67.         $options = $this->get_options();
  68.  
  69.         if ( ! empty( $_POST['term-link'] ) ) {
  70.             $options[ $term_id ] = esc_url_raw( $_POST['term-link'] );
  71.         } else {
  72.             unset( $options[ $term_id ] );
  73.         }
  74.  
  75.         $this->save_options( $options );
  76.     }
  77.  
  78. }
  79.  
  80. new Category_Links_To;
  81. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement