Advertisement
mbis

Old posts format fallback

Jul 15th, 2022
883
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.63 KB | None | 0 0
  1. <?php
  2. /*
  3. * Plugin Name:       Post Permalinks Fallback
  4. * Plugin URI:        http://maciejbis.net/
  5. * Description:       Plugin that allows to keep old permalinks format (%post_id%/%postname%)
  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. class Bis_Post_Permalinks_Fallback {
  15.     function __construct() {
  16.         add_action('init', array($this, 'init'));
  17.  
  18.         register_activation_hook(__FILE__, array($this, 'flush_rewrite_rules'));
  19.         register_deactivation_hook(__FILE__, array($this, 'flush_rewrite_rules'));
  20.     }
  21.  
  22.     function init() {
  23.         add_filter('post_link', array($this, 'filter_post_permalinks'), 999, 2);
  24.         add_action('init', array($this, 'extra_post_permastructs'), 100);
  25.         add_action('admin_init', array($this, 'limit_date_settings'));
  26.     }
  27.  
  28.     /**
  29.      * Filter post permalinks
  30.      */
  31.     function filter_post_permalinks($url, $element) {
  32.         $limit_date = get_option('bis_permalink_limit_date');
  33.  
  34.         // Posts permalinks added after the date provided below will not be changed
  35.         if(!empty($limit_date) && strtotime($element->post_date) >= strtotime($limit_date)) {
  36.             return $url;
  37.         }
  38.  
  39.         if(!empty($element->post_type) && $element->post_type == 'post') {
  40.             $url = sprintf('%s/%d/%s', rtrim(get_home_url()), $element->ID, $element->post_name);
  41.         }
  42.  
  43.         return user_trailingslashit($url);
  44.     }
  45.  
  46.     /**
  47.      * Register new additional set of rewrite rule for old post permalinks
  48.      * %post_id%/%postname%
  49.      */
  50.     function extra_post_permastructs() {
  51.         add_permastruct('post-old-format', '%post_id%/%postname%', array('with_front' => false));
  52.     }
  53.  
  54.     /**
  55.      * Flush rewrite rules during plugin activation
  56.      */
  57.     function flush_rewrite_rules() {
  58.         flush_rewrite_rules();
  59.     }
  60.  
  61.     /**
  62.      * Display extra field in "Permalinks" admin section
  63.      */
  64.     function limit_date_settings() {
  65.       add_settings_field(
  66.         'bis_permalink_limit_date',
  67.         'Old post permalinks limit date',
  68.         array($this, 'limit_date_field'),
  69.         'permalink',
  70.         'optional',
  71.         array('label_for' => 'bis_permalink_limit_date')
  72.       );
  73.  
  74.         if(isset($_POST['bis_permalink_limit_date'])) {
  75.             update_option('bis_permalink_limit_date', sanitize_title_with_dashes($_POST['bis_permalink_limit_date']));
  76.         }
  77.  
  78.     }
  79.  
  80.     function limit_date_field($args) {
  81.         $value = get_option('bis_permalink_limit_date', '2019-03-01');
  82.  
  83.         printf('<input type="text" value="%s" name="bis_permalink_limit_date" id="bis_permalink_limit_date" class="regular-text" />', esc_attr($value));
  84.     }
  85.  
  86. }
  87. new Bis_Post_Permalinks_Fallback();
  88.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement