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?
some_option
is not set on every admin page load, it you don't set it up, the condition to redirect will be alwaystrue
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