Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I created a plugin that activate my theme programmatically. I want to set default permalink using programming. It does't seems to work with custom post types. it works well with default custom post type.

I tried it on admin_footer hook with 9999 priority but not helped.

The code i used is:

add_action('admin_footer','default_permalink',9999); 
function default_permalink(){ 
  if(get_option('change_permalink')!= 'post_name_permalink') { 
    global $wp_rewrite;
    $wp_rewrite->set_permalink_structure( '/%postname%/' );
    $wp_rewrite->flush_rules(); 
    if(function_exists('flush_rewrite_rules')){ 
      flush_rewrite_rules(true);
    } //Set default permalink to postname end       
    update_option('change_permalink','post_name_permalink'); 
  }
}

Please help.

share|improve this question
add comment

3 Answers 3

Try to do something like this.

<?php
/* 
Plugin Name: My Custom Plugin
Plugin URI: 
Description: 
Author:
Version: 1.0 
Author URI:  
*/

/* Runs when plugin is activated */
register_activation_hook(__FILE__, 'mcp_install'); 

/* Runs on plugin deactivation*/
register_deactivation_hook( __FILE__, 'mcp_remove' );

function mcp_install() {

    //Make sure that .htaccess file is there.
    add_action('init', 'change_permalinks', 20);


    //Activate your theme also.
}

function mcp_remove() {

}

function mcp_change_permalinks() {
    global $wp_rewrite;
    $wp_rewrite->set_permalink_structure('/%postname%/');
    $wp_rewrite->flush_rules();
}

function mcp_custom_init() {
  $labels = array(
    'name' => 'Books',
    'singular_name' => 'Book',
    'add_new' => 'Add New',
    'add_new_item' => 'Add New Book',
    'edit_item' => 'Edit Book',
    'new_item' => 'New Book',
    'all_items' => 'All Books',
    'view_item' => 'View Book',
    'search_items' => 'Search Books',
    'not_found' =>  'No books found',
    'not_found_in_trash' => 'No books found in Trash', 
    'parent_item_colon' => '',
    'menu_name' => 'Books'
  );

  $args = array(
    'labels' => $labels,
    'public' => true,
    'publicly_queryable' => true,
    'show_ui' => true, 
    'show_in_menu' => true, 
    'query_var' => true,
    'rewrite' => array( 'slug' => 'book' ),
    'capability_type' => 'post',
    'has_archive' => true, 
    'hierarchical' => false,
    'menu_position' => null,
    'supports' => array( 'title', 'editor', 'author', 'thumbnail', 'excerpt', 'comments' )
  ); 

  register_post_type( 'book', $args );
}

add_action( 'init', 'mcp_custom_init', 10);
share|improve this answer
    
Actually i tired this but the case is what, when i set permalink with plugin then it works with default post type but its not working for custom post type. it generates 404 error for custom post type pages. –  Shaikh Aezaz May 28 '13 at 3:34
    
Okay I modified the answer and added priorities to the action calls. hope this will help. –  Achintha Samindika May 28 '13 at 4:27
    
Sorry friend but it will not help me anymore. i checked it with other priorities as you seen in my code, i used 9999 priority to call that function last but it also doesn't help me. –  Shaikh Aezaz May 28 '13 at 4:38
add comment

Rewrite rules for custom post types are controlled by their registration arguments (see rewrite in register_post_type() ) and probably shouldn't be modified externally.

Also note that flushing rewrite rules on every page load is extremely bad for performance. That should only be done when they change, such as on activation of your plugin.

share|improve this answer
    
Yes you are right. i want to change permalink on theme activation only. –  Shaikh Aezaz May 28 '13 at 3:36
add comment

do something like this:

function setup_permalinks_by_default() {
    global $wp_rewrite;
    $wp_rewrite->set_permalink_structure('/%postname%/');
    $wp_rewrite->flush_rules();
}
add_action('after_switch_theme', 'setup_permalinks_by_default')

but this - if i'm not mistaken - will not take care of creating the .htaccess file

edit:
missed the part about the custom post type; probably its best to do it at custom post type registration like rarst said;

one other possibility could be to use add_rewrite_rule and add that to above function, more info here:
http://codex.wordpress.org/Rewrite_API/add_rewrite_rule
http://wp.tutsplus.com/tutorials/creative-coding/the-rewrite-api-the-basics/

edit: see milos comment

share|improve this answer
    
after_setup_theme runs on every request, you absolutely do not want to flush rewrite rules on every request. –  Milo May 27 '13 at 16:52
    
oh, my bad, i was under the impression it runs once after theme activation, would after_switch_theme be a better solution? –  ialocin May 27 '13 at 16:57
    
any other suggestions please? –  Shaikh Aezaz May 28 '13 at 3:35
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.