I am coding a WordPress plugin where at first, I am adding Menus to WordPress and than each menu callback first triggers admin_init and than require desired page. Here is my Logic
<?php
class EasySEO
{
public function __construct()
{
$this->wp_easy_seo_register_menu_page();
}
public function wp_easy_seo_register_menu_page()
{
add_menu_page('Easy SEO : General Setting', 'Easy SEO', 'manage_options', 'wp-easy-seo-dashboard', array(
$this,
'wp_easy_seo_general_cb'
));
add_submenu_page('wp-easy-seo-dashboard', 'Easy SEO : General Setting', 'General', 'manage_options', 'wp-easy-seo-dashboard', array(
$this,
'wp_easy_seo_general_cb'
));
add_submenu_page('wp-easy-seo-dashboard', 'Title & Metas - Easy SEO', 'Title & Metas', 'manage_options', 'wp_easy_seo_title', array(
$this,
'wp_easy_seo_title_cb'
));
add_submenu_page('wp-easy-seo-dashboard', 'Verify Domain Ownership - Easy SEO', 'Domain Verification', 'manage_options', 'wp_easy_seo_domain_ownership_verify', array(
$this,
'wp_easy_seo_domain_ownership_verify_cb'
));
add_submenu_page('wp-easy-seo-dashboard', 'Web Traffic Statistics - Easy SEO', 'Traffic Statistics', 'manage_options', 'wp_easy_seo_traffic_statistics', array(
$this,
'wp_easy_seo_traffic_statistics_cb'
));
}
function wp_easy_seo_general_cb()
{
}
function wp_easy_seo_title_cb()
{
add_action('admin_init', function()
{
require 'wp_easy_seo_title.php';
});
}
function wp_easy_seo_domain_ownership_verify_cb()
{
require 'wp_easy_seo_domain_ownership_verify.php';
}
function wp_easy_seo_traffic_statistics_cb()
{
require 'wp_easy_seo_traffic_statistics.php';
}
}
add_action('admin_menu', function()
{
new EasySEO;
});
?>
Admin_init is NOT getting triggered that is why next page is NOT loading. I have tried echo 1;exit; instead to require to check if the code flows to my echo 1; still it fails.
Here is my wp_easy_seo_title page :
<?php
class EasySEO_Options {
public $options;
public function __construct() {
$this->options= get_option('wp_easyseo_homepage_options');
$this->easy_seo_display_options_page();
$this->wp_easy_seo_register_settings_and_field();
}
public function easy_seo_display_options_page() {
?>
<div class='wrap'>
<h2>Title & Metas - Easy SEO</h2>
<form action="options.php" enctype="multipart/form-data" method="POST">
<?php
settings_fields('wp_easyseo_homepage_options');
do_settings_sections('wp_easy_seo_title');
?>
<p class="submit">
<input name='submit' type='submit' class='button-primary' value='Save Changes'/>
</p>
</form>
</div>
<?php
}
public function wp_easy_seo_register_settings_and_field() {
register_setting('wp_easyseo_homepage_options', 'wp_easyseo_homepage_options');
add_settings_section('wp-easyseo-main-settings', 'Title & Metas', array($this, 'wp_easyseo_main_settings_cb'), 'wp_easy_seo_title');
add_settings_field('wp-easy-seo-title', 'Meta Title', array($this, 'wp_easy_seo_title_cb'), 'wp_easy_seo_title', 'wp-easyseo-main-settings');
add_settings_field('wp-easy-seo-description', 'Meta Description', array($this, 'wp_easy_seo_description_cb'), 'wp_easy_seo_title', 'wp-easyseo-main-settings');
}
public function wp_easyseo_main_settings_cb() {
}
/**
* Inputs
*/
public function wp_easy_seo_title_cb() {
echo "<input name='wp_easyseo_homepage_options[wp-easy-seo-title]' value='{$this->options['wp-easy-seo-title']}' type='text'/>";
}
public function wp_easy_seo_description_cb() {
echo "<input name='wp_easyseo_homepage_options[wp-easy-seo-description]' value='{$this->options['wp-easy-seo-description']}' type='text'/>";
}
}
new EasySEO_Options;
?>
Right now, your call to add_action('admin_init'...
is inside of a function wp_easy_seo_title_cb
which does get called until sometime when menus are being rendered. This is after admin_init has already occurred, and so it is registered too late.
Try moving ALL of your calls to add_action
to the Constructor of your class.
I find it good to register all actions in the constructor in order to keep things organized and ensure everything is registered in time.
<?php
class SomePlugin {
public function __construct() {
add_action('admin_init', array($this, 'some_callback'));
add_action('transition_post_status', array($this, 'other_callback'));
// etc
}
public function some_callback() {
// Do something
}
public function other_callback() {
// Do something
}
}
new SomePlugin();
This way, when the file is loaded it immediately registers all of it's action hooks. Not much else should happen in the constructor other than registering hooks, because the constructor is going to run on EVERY page load.