Hello dears
I'm writing an admin panel for my new WordPress plugin base on the WordPress Settings APIs. I am defining a field for changing custom post slug from this admin panel. After the custom slug change in some WordPress sites we get 404 errors for that CPT urls. For this reason, i'm using flush_rewrite_rules() function before displaying the slug filed in the admin panel. You can see the codes below. Now I want you to tell me, am I using the flush_rewrite_rules function in the right place?
function display_clink_main_slug(){
// validate and update clink_main_slug value
$clink_main_slug_option = get_option('clink_main_slug');
if( $clink_main_slug_option ){
$clink_main_slug_option = str_replace(' ', '-', $clink_main_slug_option); // Replaces all spaces with hyphens.
$clink_main_slug_option = preg_replace('/[^A-Za-z0-9\-]/', '', $clink_main_slug_option); // Removes special chars.
update_option( 'clink_main_slug', $value = $clink_main_slug_option );
$clink_main_slug_option = get_option('clink_main_slug');
}elseif( empty($clink_main_slug_option ) ){
update_option( 'clink_main_slug', $value = 'clink' );
$clink_main_slug_option = get_option('clink_main_slug');
}
flush_rewrite_rules();
$clink_slug_structure = get_bloginfo('url') . '/' . $clink_main_slug_option . '/link-name';
?>
<?php echo $clink_main_slug_option; ?>
<input type="text" name="clink_main_slug" id="clink_main_slug" value="<?php echo $clink_main_slug_option; ?>" />
<p class="description clink-description"><?php _e('Enter the clink plugin base slug for links. In default it set to <code>clink</code>','aryan-themes'); ?></p>
<p class="description clink-description"><?php printf( __( 'Current links structure is like: <code>%s</code>', 'aryan-themes' ) , $clink_slug_structure ); ?></p>
<?php
}
init
hook), only ever flush them after permalink-related changes. You pass with flying colours. All good.