theme development - How to set permalink structure via functions.php

I'm setting up a Wordpress Network and wanted all new sites to have the same permalink structure (i.e. "%year

I'm setting up a Wordpress Network and wanted all new sites to have the same permalink structure (i.e. "/%year%/%monthnum%/%postname%/"). I'm wondering if this is possible to do via hooks or hacks in functions.php, without having to rely on users to choose that structure.

I'm setting up a Wordpress Network and wanted all new sites to have the same permalink structure (i.e. "/%year%/%monthnum%/%postname%/"). I'm wondering if this is possible to do via hooks or hacks in functions.php, without having to rely on users to choose that structure.

Share Improve this question asked Oct 15, 2011 at 21:19 Tomas ButelerTomas Buteler 3,8622 gold badges28 silver badges29 bronze badges
Add a comment  | 

3 Answers 3

Reset to default 16

You can set the permalink structure by calling on the set_permalink_structure() method of the global $wp_rewrite object.

add_action( 'init', function() {
    global $wp_rewrite;
    $wp_rewrite->set_permalink_structure( '/%year%/%monthnum%/%postname%/' );
} );

Here's a PHP < 5.3 version of the code in case you're getting errors.

function reset_permalinks() {
    global $wp_rewrite;
    $wp_rewrite->set_permalink_structure( '/%year%/%monthnum%/%postname%/' );
}
add_action( 'init', 'reset_permalinks' );

Previous Answer is not working. I have gotten a pure solution. Can use Use this code. It will work 100%. Thanks

/**
 * Rewrite set up, when theme activate i mean
 */
if (isset($_GET['activated']) && is_admin()) {
    global $wp_rewrite;
    $wp_rewrite->set_permalink_structure('/%postname%/');
    $wp_rewrite->flush_rules();
}

/**
* Redirect to Permalink setting Page.
* Otherwise Redirect rule will not work Properly.
*/
function redirect_to_permalink() {

    wp_redirect('options-permalink.php');
}
add_action( 'after_switch_theme', 'redirect_to_permalink' );
function setPermaLink(){
    $ps = '/%category%/%postname%/';
    $permalink_structure = sanitize_option( 'permalink_structure', $ps);
    $blog_prefix = '/blog';
    $prefix = '/index.php';

    if ( ! empty( $permalink_structure ) ) {
        $permalink_structure = preg_replace( '#/+#', '/', '/' . str_replace( '#', '', $permalink_structure ) );
        if ( $prefix && $blog_prefix ) {
            $permalink_structure = $prefix . preg_replace( '#^/?index\.php#', '', $permalink_structure );
        } else {
            $permalink_structure = $blog_prefix . $permalink_structure;
        }
    }

    $wp_rewrite->set_permalink_structure( $permalink_structure );
    flush_rewrite_rules();
}

setPermaLink();

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745164016a4614535.html

相关推荐

  • theme development - How to set permalink structure via functions.php

    I'm setting up a Wordpress Network and wanted all new sites to have the same permalink structure (i.e. "%year

    2小时前
    20

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信