admin管理员组文章数量:1332660
I'm making a custom theme. It's a highly specialized theme to make WordPress into like an application rather than a CMS system or blog. For instance, a Dental Office Scheduling System (with CMS and widget capabilities), as an example.
Because my theme needs pretty URLs to work properly, something I really need is for the .htaccess file to be that default that gets created only when someone sets Permalinks to Custom (and then types in something like %postname%). How do I trigger that in WordPress, programmatically, so that it creates this? I mean, I could probably overwrite the file myself during theme activation, but the better thing would be to use the WordPress API for it.
I'm making a custom theme. It's a highly specialized theme to make WordPress into like an application rather than a CMS system or blog. For instance, a Dental Office Scheduling System (with CMS and widget capabilities), as an example.
Because my theme needs pretty URLs to work properly, something I really need is for the .htaccess file to be that default that gets created only when someone sets Permalinks to Custom (and then types in something like %postname%). How do I trigger that in WordPress, programmatically, so that it creates this? I mean, I could probably overwrite the file myself during theme activation, but the better thing would be to use the WordPress API for it.
Share Improve this question asked Dec 13, 2011 at 5:01 VolomikeVolomike 1,8652 gold badges18 silver badges20 bronze badges 2- "my theme needs pretty URLs to work properly" why? – chrisguitarguy Commented Dec 20, 2011 at 22:40
- @ChristopherDavis because it's an app theme, not a regular theme. I have an MVC framework loaded inside a theme folder that operates heavily on rewrites. – Volomike Commented Jan 10, 2012 at 3:49
3 Answers
Reset to default 9To fully enable permalinks, you also need to ensure that .htaccess is also created. To do that, you need to set an option and flush the rules with a Boolean.
global $wp_rewrite;
//Write the rule
$wp_rewrite->set_permalink_structure('/%postname%/');
//Set the option
update_option( "rewrite_rules", FALSE );
//Flush the rules and tell it to write htaccess
$wp_rewrite->flush_rules( true );
If you use this in a plugin, it needs to be in the init
hook, not the load
hook. If it's in the load
hook, it will throw an error saying $wp_rewrite
is null.
Important: You should also have a conditional so this is only set once. (You can create an option and check if it's set, if not then you run this permalink code and set that option)
I also typically check if it's the admin side and only run it if it is.
function change_permalinks() {
global $wp_rewrite;
$wp_rewrite->set_permalink_structure('/%postname%/');
$wp_rewrite->flush_rules();
}
add_action('init', 'change_permalinks');
You may not need the action hook if you're sticking this in your theme activation function code.
I also found that this only slightly worked. You still have to click the Permalinks settings page for that .htaccess file to be created. So, what to do? Well, I found I could use an IFRAME that loads that page automatically for me from my theme's options panel, and then it would create that .htaccess file for me.
<iframe style="position:absolute;top:-5000px" src="<?= site_url() ?>/wp-admin/options-permalink.php"></iframe>
This is how I use this, to only occur when my theme is activated, and only if there is no pre-existing setting saved (to not break something done before my theme is used).
/*
* 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('/news/%postname%/');
$wp_rewrite->flush_rules(true);
}
add_action('after_switch_theme', 'set_custom_permalinks');
The advantage of this is that it will only run once when the theme is activated, not every time WordPress is loaded by a visit to the site.
本文标签: How Do I Programmatically Force Custom Permalinks with My Theme
版权声明:本文标题:How Do I Programmatically Force Custom Permalinks with My Theme? 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742273900a2444828.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论