-
Notifications
You must be signed in to change notification settings - Fork 2
/
wp-change-custom-post-slugs.php
144 lines (122 loc) · 5.01 KB
/
wp-change-custom-post-slugs.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
<?php
/*
Plugin Name: CPT Slug Changer
Plugin URI: http://fahimm.com/plugin/cpt-slug-changer
Description: The plugin allows to can easily change Custom Post Type slug from WordPress admin panel.
Version: 1.0
Author: FahimMurshed
Author URI: http://www.fahimm.com
*/
add_action( 'admin_menu', 'wpccps_th_custom_post_slug_menu' );
function wpccps_th_custom_post_slug_menu() {
add_options_page( __('CPT Slug Changer', 'wpccps_th_custom_slugs' ), __('CPT Slug Changer', 'wpccps_th_custom_slugs' ), 'manage_options', 'th-wp-change-custom-post-slugs', 'wpccps_th_custom_slugs_plugin_options' );
}
add_action( 'admin_init', 'wpccps_th_custom_post_slug_admin_init' );
function wpccps_th_custom_post_slug_admin_init() {
flush_rewrite_rules();
//register_setting( 'wpccps-settings-group', 'th-wp-change-custom-post-slugs-settings', 'wpccps_th_my_settings_validate_and_sanitize' );
register_setting( 'wpccps-settings-group', 'th-wp-change-custom-post-slugs-settings' );
add_filter( 'update_option_th-wp-change-custom-post-slugs-settings', 'wpccps_th_flush_rewreite_rules_after_save', 10, 2);
add_settings_section( 'section-list-of-custom-post-types', __( 'Configure custom slugs', 'wpccps_th_custom_slugs' ), 'wpccps_th_section_1_callback', 'th-wp-change-custom-post-slugs' );
$post_types = get_post_types( array( '_builtin' => false, 'publicly_queryable' => true, 'show_ui' => true ) );
$settings = (array) get_option( 'th-wp-change-custom-post-slugs-settings' );
foreach ($post_types as $key => $post_type) {
add_settings_field(
'field-'.$post_type,
__( $post_type, 'wpccps_th_custom_slugs' ),
'wpccps_th_setting_structure_callback_function',
'th-wp-change-custom-post-slugs',
'section-list-of-custom-post-types' ,
array( 'label_for' => $post_type . '_structure_th',
'post_type' => $post_type,
'settings' => $settings
)
);
}
}
function wpccps_th_flush_rewreite_rules_after_save( $old_value, $new_value )
{
flush_rewrite_rules();
}
function wpccps_th_setting_structure_callback_function($option){
$post_type = $option['post_type'];
$settings = $option['settings'];
$name = $option['label_for'];
$pt_object = get_post_type_object( $post_type );
$slug = $pt_object->rewrite['slug'];
$wiwpccps_th_front = $pt_object->rewrite['with_front'];
$value = '';
if( !empty($settings[$post_type] )){
$value = esc_attr( $settings[$post_type] );
}
$disabled = false;
if ( isset( $pt_object->cptp_permalink_structure ) and $pt_object->cptp_permalink_structure ) {
$disabled = true;
}
global $wp_rewrite;
$front = substr( $wp_rewrite->front, 1 );
if ( $front and $wiwpccps_th_front ) {
$slug = $front . $slug;
}
?>
<p>
<input name="th-wp-change-custom-post-slugs-settings[<?php echo esc_attr( $post_type );?>]" id="<?php echo esc_attr( $name );?>" type="text" class="regular-text code " value="<?php echo esc_attr( $value ) ;?>" <?php disabled( $disabled, true, true );?> />
<br />
<span>Use alphabets only. Leave empty to use default slug.</span>
</p>
<?php
}
/*
* THE ACTUAL PAGE
* */
function wpccps_th_custom_slugs_plugin_options() {
?>
<div class="wrap">
<iframe src="https://fahimm.com/" width="100%" height="250"></iframe>
<hr />
<h2><?php _e('Custom Post Type Slug Changer', 'wpccps_th_custom_slugs'); ?></h2>
<?php
$post_types = get_post_types( array( '_builtin' => false, 'publicly_queryable' => true, 'show_ui' => true ) );
if( count($post_types )){
?>
<form action="options.php" method="POST">
<?php settings_fields('wpccps-settings-group'); ?>
<?php do_settings_sections('th-wp-change-custom-post-slugs'); ?>
<?php submit_button(); ?>
</form>
<?php }else{
echo _e('There is not custom post type registered in the system', 'wpccps_th_custom_slugs');
} ?>
</div>
<?php }
/*
* THE SECTIONS
* Hint: You can omit using add_settings_field() and instead
* directly put the input fields into the sections.
* */
function wpccps_th_section_1_callback() {
_e( 'You can set the custom slug for following post types. After saving go \'Permalink\' page and click on \'Save Changes\'.', 'wpccps_th_custom_slugs' );
}
/*
* INPUT VALIDATION:
* */
function wpccps_th_my_settings_validate_and_sanitize( $input ) {
$settings = (array) get_option( 'th-wp-change-custom-post-slugs-settings' );
// if ( $some_condition == $input['field_1_1'] ) {
// $output['field_1_1'] = $input['field_1_1'];
// } else {
// add_settings_error( 'th-wp-change-custom-post-slugs-settings', 'invalid-field_1_1', 'You have entered an invalid value into Field One.' );
// }
return $input;
}
function wpccps_th_add_custom_rewrite_rule() {
$settings = (array) get_option( 'th-wp-change-custom-post-slugs-settings' );
foreach ($settings as $post_type => $slug) {
if(!empty( $slug)){
$args = get_post_type_object($post_type);
$args->rewrite["slug"] = $slug;
register_post_type($args->name, $args);
}
}
} // end wpccps_th_add_custom_rewrite_rule
add_action('init', 'wpccps_th_add_custom_rewrite_rule');