Home » Wordpress » Update permalinks if not set at the time of plugin activation

Update permalinks if not set at the time of plugin activation

Posted by: admin November 30, 2017 Leave a comment

Questions:

I need permalink to be set different from default after the plugin is activated
I used get_option(‘permalink_structure’) to check if permalink already set or not…

Here is the code block used in plugin activation function , I used class structure

 if (get_option('permalink_structure'))
    {
        $this->setRewriteRules();
        global $wp_rewrite;
        $wp_rewrite->flush_rules(true);
    }

and here is the serRewriteRules function

function setRewriteRules()
{
    add_rewrite_rule('plugin-url/$', '/wp-content/plugins/my-plugin/page.php', 'top');
}

I searched a lot and update my code so many time but still not working for me ….

Answers:

rather than changing permalink dynamically you can use generate link from WP permalinks function

for e.g.

get_the_permalink()
the_permalink()
get_post_type_archive_link()