BuddyDev

Search

[Resolved] set permalink via php

  • Participant
    Level: Guru
    Posts: 885
    Tosin on #45316

    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

  • Keymaster
    (BuddyDev Team)
    Posts: 24212
    Brajesh Singh on #45323

    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
    Brajesh

  • Participant
    Level: Guru
    Posts: 885
    Tosin on #45327

    The 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.

  • Participant
    Level: Guru
    Posts: 885
    Tosin on #45328

    To prevent the performance issue will it be OK to remove $wp_rewrite->flush_rules( true ); in the code

  • Keymaster
    (BuddyDev Team)
    Posts: 24212
    Brajesh Singh on #45329

    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
    Brajesh

  • Participant
    Level: Guru
    Posts: 885
    Tosin on #45330

    Alternatively would it be possible to automatically save and flush permalink settings via cronjob once a week

  • Keymaster
    (BuddyDev Team)
    Posts: 24212
    Brajesh Singh on #45332

    Hi 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

  • Participant
    Level: Guru
    Posts: 885
    Tosin on #45333

    OK thanks Brajesh I’ll do as you say

  • Keymaster
    (BuddyDev Team)
    Posts: 24212
    Brajesh Singh on #45357

    🙂

You must be logged in to reply to this topic.

This topic is: resolved