To 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.
Related Posts:
- Get the blog page URL set in Options
- How to set permalink structure via functions.php
- Different ways to display title
- Cache Busting using htaccess Rewrite rule?
- How to write .htaccess dynamically? [closed]
- Link won’t show using the_permalink();
- get_query_var() and permalinks
- WordPress custom post type permalink: website.com/post-name/post-id
- Multisite theme path rewrite using .htaccess
- Filter categories used with Custom Structure Permalink
- How to make permalinks update from code?
- Default Permalink Type to Postname
- How to set a page as homepage in stead of the newspages?
- I used single quote to design HTML of my WordPress blog. Now site redirects to index.php everytime than page.php or single.php
- wordpress 404 not found after rewrtie the author link
- Issue On Displaying Pages with Post Name Permalink
- Issue on Getting Custom post type Thumbnail’s URL
- WordPress page/blog incorporated into static website
- Only the latest post shows up on post page?
- How to remove permalinks links presents in each page of my site?
- If I remove Category base from my URLs, how difficult will rollback be?
- What are the permalinks options for “Category” base and removing it?
- How do I open a post in a custom page in wordpress?
- Does single webpage do not need navigation to create a slug in permalink?
- Getting post URL within custom content template
- hide theme files for admin beneath root
- .htaccess and proxy settings for routing a decoupled REST API consuming theme
- Duplicate WordPress site to subdirectory but use same db?
- Mamp pro permalink issues. Pages keep reverting to index.php
- Permalinks problem with custom theme
- Permalink Setting Changes From Post Name to Default After Changing The Page Name
- Configuring static page with add_rewrite_rule gives 404 after navigating to Permalinks admin panel
- How to add custom css file in theme?
- CSS not updating in browser when I change it
- How to remove search bar from a wordpress theme? [closed]
- Get url of thumbnail from the media uploader
- How to: Update live sites theme from version control repo via plugin or library? [closed]
- How to sanitize select box values in post meta?
- Why to check if function doesn’t exists in functions.php?
- Pages: frontpage.php, home.php, posts page etc
- How to add a WYSIWYG text editor to the Category Edit Screen
- Different wordpress 404 template for different post type [duplicate]
- How to choose which template to be used for multiple taxonomy query?
- WordPress Customizer Typography: How to load just the unique Google Fonts?
- Can we add more than one control under a setting in WP theme customizer?
- Widgets panel not displaying in the Theme Customizer
- How to programmatically bring back “excerpts” field in post editor in WP 3.1+
- Get background color for Live Preview with Theme Customization API?
- Add a theme via symlink
- Alterntives to BEM syntax that comply with WordPress coding standards? [closed]
- How to add oEmbed support to my theme?
- WordPress Multisite: Have the same header and footer of main-blog on all sub-blogs
- How to determine which registered sidebar area a custom widget is loaded into
- Difference between is_page and is_page_template
- Including wp-blog-header.php from functions.php remote call?
- Administration Pages Styling
- How to Globally Use wp_localize_script() Ajax URL
- add filter not working when cancatenating variables
- How can i convert the figma design into wordpress [closed]
- A custom theme with support for multiple layouts
- WordPress Theme Development: How to redirect templates the right way?
- Creating Theme Options Page
- How can I let templates choose which stylesheets are enqueued?
- WordPress with broken paging page 3
- What is the best way to organize template parts?
- wp_enqueue_script outside functions.php file?
- How to implement a $_SESSION alternative in WordPress inside a theme without a plugin?
- How can I make my options in an array and store theme in WP options one DB row?
- Differences between developing custom themes for wordpress.com and wordpress.org?
- Easy to develop on a URL that is changed for production?
- How to control on which pages the Aldehyde theme’s main slider is shown?
- How to rewrite a URL to another rewritten URL without changing the address?
- WordPress stylesheet isn’t being added
- How to detect and display a page only for IE?
- Getting author URL outside the loop
- Change image size depending on page
- How to include a mu-plugin as a theme dependency?
- How to use ajax in wordpress and sending the data without refreshing ?
- Adding an external link to product grid list pages for woocommerce
- What and where are the WordPress core-bundled scripts?
- How do I get from search_theme_directories() to the $stylesheet (name) for switch_theme()
- Tabbed theme options query about add_settings_section callback function
- How to know if I am on 1st page
- How to create a robust and logic class naming system in WordPress theme developing?
- I don’t know why categories are showing below post
- WordPress Custom font not found
- How do I control the header space in non-front pages in Twenty Seventeen?
- Target second from last post in loop
- Customizer API way function is_customize_preview() works only in main page?
- Advantages/Disadvantages Using Theme Editor Instead of Pages
- Magnific Popup – Add Caption to Images
- Why do WordPress developers use so many opening and closing PHP tags when developing themes? [duplicate]
- paragraph format in WYSIWYG on a custom theme?
- unable to display the image meta value as background
- editor style css and page template with and without sidebar
- 3 x 3 grid of posts on the home page
- The normal loop with different styles doesn’t work in search.php
- Pass custom css class to add_menu_page
- How to force unlink on attached/inserted images?
- How to create sub-menu in “Allure Real Estate Theme for Placester”?