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
- Child Theme vs Duplicate Theme Renamed
- Is it possible to manipulate the list of page templates?
- When to use esc_url, esc_html, esc_attr, and friends?
- Passing variables through permalink structure
- How to enqueue scripts and styles only when there are needed?
- Unexpected width and srcset attributes for the_post_thumbnail();
- Pushing updates to your premium theme
- The best way to add stylesheets to WordPress
- WordPress custom post type page dysplay 404 error
- How wordpress handle upload images and how to use them in the code
- Change loop order via form or link (jquery, not URL)
- how to create theme based widget that can be drop in sider bar or footer
- CSS in child theme not overriding the parent theme [closed]
- Optimal solution to develop a wordpress theme?
- How to test for MU via functions.php?
- List categories and exclude child categories
- Am I supposed to create a child theme for every theme I use?
- Custom shortcodes not working using __s theme
- Disable wp_enqueue_style for theme on wp-admin
- Custom URL parameters in template files
- How do I remove TinyMCE text format
- wp_post->post_parent object returning 0
- Customizer: Unique identifier that distinguishes which image upload control is uploading an image
- Theme Splash Image within the “Appearance -> Themes” control panel [duplicate]
- Permalinks not working with Vagrant
- Disabling automatic teasers
- Can “Recent Posts” widget be filtered by functions.php?
- attachment.php code or tutorial
- How to filter or remove the “title” attribute from category links
- Change “Thumbnail”, “Medium” and “Large” image sizes using functions.php?
- Remove CPT slug from URL WordPress
- Custom get_the_excerpt() only works on first post
- Permanently activate WordPress theme
- Post Content Displaying Below ALL Shortcodes Content
- WordPress Template Part in iFrame
- Why tags are displayed bellow the content and not inside
- What’s the best way to create a new design for other pages?
- Consequences if using PHP include TEMPLATEPATH?
- Adding a gallery to my first theme
- Headless WordPress + Vue.js on the same server
- not getting API setting saved confirmation message after update on option page
- Is there a way to retrieve the calling template part?
- WP_Query not getting all posts, just tagged posts
- how to hide home nav link in wordpress
- Converting a theme to a child theme in a network
- Calling a WP Plugin Function’s Variable in Theme Template [duplicate]
- Why excerpt hook not working inside ajax function?
- Help with Core Mods
- Local variable name in setup_postdata()
- Child theme menu not appearing in Twenty Seventeen
- how to edit woocommerce checkout page
- How to rewrite a folder in WordPress?
- Setting a fluid content width
- get_the_excerpt() removes all p tags in page!
- How to migrate the menu from the site on my own theme in WordPress?
- Why is including all your files within a theme folder important?
- Change default options on attachment page
- Issue with permalinks for custom post type
- FitVid not working for iframes on particular theme install
- .htaccess not working in WordPress
- Make theme editor to show all theme’s files
- How do I change the tagline input to textarea?
- htaccess strip url
- can’t add EVENT LISTENER to a element
- add_action works outside condition but not inside it
- Jquery Ui Tabs not working
- How to set up a development/staging site to make major changes to the theme then update on the live site?
- What is the point of using the front-page.php template? [closed]