Hello,
Is it advisable to set permalink structure via php in theme functions.php also which is better among this 3 code
// set permalink function set_permalink(){ global $wp_rewrite; $wp_rewrite->set_permalink_structure('/%postname%/'); } add_action('init', 'set_permalink');
function set_permalink(){ global $wp_rewrite; $wp_rewrite->set_permalink_structure('/%postname%/'); $wp_rewrite->flush_rules(); } add_action('init', 'set_permalink');
/* * Set permlinks on theme activate */ function set_custom_permalinks() { $current_setting = get_option('permalink_structure'); // Abort if already saved to something else if($current_setting) { return } // Save permalinks to a custom setting, force create of rules file global $wp_rewrite; update_option("rewrite_rules", FALSE); $wp_rewrite->set_permalink_structure('/%postname%/'); $wp_rewrite->flush_rules(true); } add_action('after_switch_theme', 'set_custom_permalinks');
Thanks
It’s a bad idea.
1. You should never flush rewrite rules on each request. That will hit the performance badly.
The last one is slightly better but still not recommend. why do you want to automate the permalink structure on theme activation?
It will take only 30 second to update it manually.
If you still want to do it, here is a cleaned version of your last one
function set_custom_permalinks() { if ( get_option( 'permalink_structure' ) ) { return; } global $wp_rewrite; // Save permalinks to a custom setting, force create of rules file $wp_rewrite->set_permalink_structure( '/%postname%/' ); $wp_rewrite->flush_rules( true ); } add_action( 'after_switch_theme', 'set_custom_permalinks' );
Regards
BrajeshThe problem is at times my site pages suddenly lead to 404 error and every time this happens I always have to go and resave permalink settings. Now I’m tired of doing this over and over I’m also worried that my visitors will be seeing 404 error without me knowing quickly in time to resave permalink setting. so I was thinking if there was a way to set the permalink via php to avoid resaving permalink setting too often.
Hi Tosin,
It is fine to use the flush rewrite rule son theme change or some action which done not happen too frequently.
Also, My suggestion for you will be to find out the real culprit. Putting patches like this will lead to a fragile and unstable/unpredictable code.
Regards
BrajeshHi Tosin,
This is doable but these are all steps in wrong direction.You should try to troubleshoot and try to find the root issue. That will save you from a lot of troubles in future.
Regards
Brajesh
You must be logged in to reply to this topic.