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.
Try to do something like this.
Rewrite rules for custom post types are controlled by their registration arguments (see
rewrite
inregister_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.
do something like this:
but this – if i’m not mistaken – will not take care of creating the
.htaccess
fileedit:
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