0

I want my plugin to redirect to a page when some conditions are not met after plugin activation. I keep getting To many redirects... error message.

My code is as shown below:

if (! defined('WPINC')) die;
if (! class_exists('AwesomePlugin')) {

    class AwesomePlugin {
        public function __construct(){
            //Register admin menus
            add_action('admin_menu', [$this, add_menu]);

            //Init plugin
            add_action('admin_init', [$this, 'init_plugin']);
        }

        /**
         *
         * Init plugin
         */
        public function init_plugin(){
            if(!isset(get_option("some_option"))){
                wp_redirect("admin.php?page=some-page"); exit; //This gives too many redirects
            }
            wp_redirect('admin.php?page=dashboard'); exit;
        }

        /**
         * Add menu and submenu items 
         */
        public function add_menu(){
            add_menu_page( 
                'Dashboard', 'Dashboard', 'manage_options', 'dashboard', 
                [__CLASS__, 'load_dashboard_view'], 
                plugins_url('logo.png', __FILE__)
            );

            add_submenu_page(
                '', 'Signup page', 'Signup page', 'manage_options', 
                'sign-up', [__CLASS__, 'load_signup_view']
            ); 
        }  

        /**
         * Dashboard page 
         */
        public function load_dashboard_view(){
            include(plugin_dir_path(__FILE__) . 'dashboard.php');
        } 

        /**
         * Signup page
         */
        public function load_signup_view(){
            include(plugin_dir_path(__FILE__) . 'signup.php');
        }           
    } 

    AwesomePlugin()   
}

What am I missing?

EDIT: I understand that no matter the page I'm loading, the init_plugin function keeps firing. As a result, it keeps redirecting the user to the sign-up, thus the many redirect error message. How do I solve this?

  • 2
    You code perform a redirect if some_option is not set on every admin page load, it you don't set it up, the condition to redirect will be always true in every page load. You just need to think about a proper workflow for your purpose, which is what? – cybmeta Sep 29 '17 at 15:09
-1

This will help you want you want to achieve.

// Add option
register_activation_hook( __FILE__, 'me_shakeeb_install' );
function me_shakeeb_install() {

    $result = 'signup'; // Perform your logic here to decide where to redirect
    add_option( 'me_shakeeb_redirect_to', $result );
}

// Check Option
add_action( 'init', 'me_shakeeb_signup_check' );
function me_shakeeb_signup_check() {

    /**
     * Redirect to about page.
     */
    if ( false !== get_option( 'me_shakeeb_redirect_to', false ) ) {
        $page = get_option( 'me_shakeeb_redirect_to', false );
        delete_option( 'me_shakeeb_redirect_to' );
        wp_redirect( admin_url( 'admin.php?page=$page' ) );
        exit;
    }
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.