0

I believe i have to add this to my plugin to redirect after plugin activation

as per https://wordpress.stackexchange.com/a/178504/145078

register_activation_hook(__FILE__, 'nht_plugin_activate');
add_action('admin_init', 'nht_plugin_redirect');

function nht_plugin_activate() {
add_option('nht_plugin_do_activation_redirect', true);
}

function nht_plugin_redirect() {
if (get_option('nht_plugin_do_activation_redirect', false)) {
    delete_option('nht_plugin_do_activation_redirect');
    if(!isset($_GET['activate-multi']))
    {
        wp_redirect("edit.php?post_type=headline&page=news-headline");
    }
 }
}

But what is plugin prefix nht ? How can i make my plugin to work redirect after plugin activation what are things to be updates?

5
  • 1
    "nht_" is the prefix. You should replace that with your own. Prefixes are just to make a function name unique to avoid namespace collisions. Since that's the prefix of someone else's plugin, it would be unwise to use it with exactly the same name as the function would already be defined.
    – butlerblog
    Nov 15, 2018 at 21:23
  • @butlerblog oh okay then it does not seems to work for me
    – user145078
    Nov 16, 2018 at 7:52
  • If it worked without changing it, it will work if you change it - there would be no difference. Just make sure you change the function names both where they are defined AND in the hook.
    – butlerblog
    Nov 16, 2018 at 13:02
  • @butlerblog yeah i understand that ..the code does not seems to work
    – user145078
    Nov 16, 2018 at 13:09
  • @butlerblog my mistake , the code was not getting updated that's why it did n't worked ..please make it as answer .thanks :)
    – user145078
    Nov 16, 2018 at 13:23

1 Answer 1

0

In this case, "nht_" is a prefix to avoid naming collisions with similar functions. This is a "best practice" to observe when doing your own development.

So if you're developing something that would be distributed publicly, you should be the habit of applying your own prefix to your function names; and since this particular code snippet is something from another person/plugin, it's definitely a good idea to replace it with your own. So change "nht_" to something that you would use as a prefix:

register_activation_hook(__FILE__, 'my_custom_prefix_plugin_activate');
add_action('admin_init', 'my_custom_prefix_plugin_redirect');

function my_custom_prefix_plugin_activate() {
    add_option('my_custom_prefix_plugin_do_activation_redirect', true);
}

function my_custom_prefix_plugin_redirect() {
    if (get_option('my_custom_prefix_plugin_do_activation_redirect', false)) {
        delete_option('my_custom_prefix_plugin_do_activation_redirect');
        if(!isset($_GET['activate-multi'])) {
            wp_redirect("edit.php?post_type=headline&page=news-headline");
        }
    }
}

I applied "my_custom_prefix_" in your code where appropriate as an example. Note this not only included function names, but also the wp_option that the setting is saved under. But you would want to change it to something that suits you and/or your plugin.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.