We can have a conditional admin check in the init hook and rest will work fine, without having our settings exposed outside.
/**
* Default options.
*
* Sets up the default options used on the settings page.
*/
function create_options() {
if ( ! is_admin() ) {
return false;
}
// Include settings so that we can run through defaults.
include_once dirname( __FILE__ ) . '/class-admin-settings.php';
$settings = Admin_Settings::get_settings_pages();
foreach ( $settings as $section ) {
if ( ! method_exists( $section, 'get_settings' ) ) {
continue;
}
$subsections = array_unique( array_merge( array( '' ), array_keys( $section->get_sections() ) ) );
foreach ( $subsections as $subsection ) {
foreach ( $section->get_settings( $subsection ) as $value ) {
if ( isset( $value['default'] ) && isset( $value['id'] ) ) {
$autoload = isset( $value['autoload'] ) ? (bool) $value['autoload'] : true;
add_option( $value['id'], $value['default'], '', ( $autoload ? 'yes' : 'no' ) );
}
}
}
}
}
add_action( 'init', 'Analog\Settings\create_options', 20 );
(On a side note - We already have a conditional admin check in the setting saver function since it loads at wp_loaded which is also a global hook, it should have been as well added at init for settings includes in the first place.)
Reason
We have our settings saver function hooked at
wp_loaded
for conditional failure reasons withadmin_menu
hook and sincewp_loaded
gets fired beforeadmin_init
placing our settings include won't work there. https://github.com/mauryaratan/analogwp-templates/blob/25a05fbf1d1ea8a7ae82d210d1e29a969463174d/inc/register-settings.php#L122So we have it included in an early action that is
init
, making it exposed to everywhere. https://github.com/mauryaratan/analogwp-templates/blob/25a05fbf1d1ea8a7ae82d210d1e29a969463174d/inc/register-settings.php#L173Possible Fix
We can have a conditional admin check in the
init
hook and rest will work fine, without having our settings exposed outside.(On a side note - We already have a conditional admin check in the setting saver function since it loads at
wp_loaded
which is also a global hook, it should have been as well added atinit
for settings includes in the first place.)