• Hi guys,

    I have been using the following script to help me out with managing blog slugs without messing categories or permalinks. For example, if I don’t use the script, I get the blog slug to prepend on each URL, like:

    The above I don’t have issues with. I have issues with the following:

    The script below allows me to stop out the blog slug on category pages, etc. It’s working, however, I suddenly cannot change the blog URL in the dashboard. Once it’s published, it’s a done deal. I used to be able to make changes, I’m not sure what changed. Please have a look point me in the right direction or see if you can pick anything up that I can’t pick up.

    
    function sle_slug_posts_add_rewrite_rules( $wp_rewrite ) {
    
        $new_rules = [
            'insights/page/([0-9]{1,})/?$' => 'index.php?post_type=post&paged='. $wp_rewrite->preg_index(1),
            'insights/(.+?)/?$' => 'index.php?post_type=post&name='. $wp_rewrite->preg_index(1),
        ];
        $wp_rewrite->rules = $new_rules + $wp_rewrite->rules;
        return $wp_rewrite->rules;
    
    }
    add_action('generate_rewrite_rules', 'sle_slug_posts_add_rewrite_rules');
    
    function sle_slug_posts_change_blog_links($post_link, $id=0) {
    
        $post = get_post($id);
        if( is_object($post) && $post->post_type == 'post'){
            return home_url('/insights/'. $post->post_name.'/');
        }
        return $post_link;
    
    }
    add_filter('post_link', 'sle_slug_posts_change_blog_links', 1, 3);
    
    • This topic was modified 4 years, 3 months ago by Archie Makuwa.
Viewing 1 replies (of 1 total)
  • Moderator bcworkz

    (@bcworkz)

    Where is WP installed? Domain root or /insights/? I think domain root, or the rewrite rules are not needed. But in that case the category links wouldn’t include /insights/ by default. Unless your home and site URL settings are incorrect. But you say you cannot alter these? That’s weird.

    Is this a single site installation? Or multisite? Site and home URLs are hidden on multisite, but still accessible through semi-hidden /wp-admin/options.php. So there should always be a way to edit these, assuming you have Administrator (or Super Administrator for multisite) role.

    As a work around, the values can be edited directly through the phpMyAdmin app. Note that directly editing the DB this way is very risky. If you are not absolutely sure what you are doing, you should make a backup of the related table before making changes.

    After making changes related to permalinks and URLs, always visit the permalinks settings screen to ensure the rewrite rules are up to date. No need to change or save anything, just load the settings screen.

Viewing 1 replies (of 1 total)
  • The topic ‘Function to rewrite blog page slugs and catgo’ is closed to new replies.