Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- /*
- * Plugin Name: Post Permalinks Fallback
- * Plugin URI: http://maciejbis.net/
- * Description: Plugin that allows to keep old permalinks format (%post_id%/%postname%)
- * 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
- */
- class Bis_Post_Permalinks_Fallback {
- function __construct() {
- add_action('init', array($this, 'init'));
- register_activation_hook(__FILE__, array($this, 'flush_rewrite_rules'));
- register_deactivation_hook(__FILE__, array($this, 'flush_rewrite_rules'));
- }
- function init() {
- add_filter('post_link', array($this, 'filter_post_permalinks'), 999, 2);
- add_action('init', array($this, 'extra_post_permastructs'), 100);
- add_action('admin_init', array($this, 'limit_date_settings'));
- }
- /**
- * Filter post permalinks
- */
- function filter_post_permalinks($url, $element) {
- $limit_date = get_option('bis_permalink_limit_date');
- // Posts permalinks added after the date provided below will not be changed
- if(!empty($limit_date) && strtotime($element->post_date) >= strtotime($limit_date)) {
- return $url;
- }
- if(!empty($element->post_type) && $element->post_type == 'post') {
- $url = sprintf('%s/%d/%s', rtrim(get_home_url()), $element->ID, $element->post_name);
- }
- return user_trailingslashit($url);
- }
- /**
- * Register new additional set of rewrite rule for old post permalinks
- * %post_id%/%postname%
- */
- function extra_post_permastructs() {
- add_permastruct('post-old-format', '%post_id%/%postname%', array('with_front' => false));
- }
- /**
- * Flush rewrite rules during plugin activation
- */
- function flush_rewrite_rules() {
- flush_rewrite_rules();
- }
- /**
- * Display extra field in "Permalinks" admin section
- */
- function limit_date_settings() {
- add_settings_field(
- 'bis_permalink_limit_date',
- 'Old post permalinks limit date',
- array($this, 'limit_date_field'),
- 'permalink',
- 'optional',
- array('label_for' => 'bis_permalink_limit_date')
- );
- if(isset($_POST['bis_permalink_limit_date'])) {
- update_option('bis_permalink_limit_date', sanitize_title_with_dashes($_POST['bis_permalink_limit_date']));
- }
- }
- function limit_date_field($args) {
- $value = get_option('bis_permalink_limit_date', '2019-03-01');
- printf('<input type="text" value="%s" name="bis_permalink_limit_date" id="bis_permalink_limit_date" class="regular-text" />', esc_attr($value));
- }
- }
- new Bis_Post_Permalinks_Fallback();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement