wm-settings
wm-settings copied to clipboard
Use create_settings_page in admin_init ?
Hi,
Thx for the great class :). It's help a lot with plugin developpement.
It's possible to use create_settings_page in a admin_init hook ? If I try to use create_settings_page in admin_init, no options are created.
If I try : ($this->roles is empty when the constructor is called in create_settings_page function. And I need admin_init to use get_editable_roles function otherwise a fatal error is throw : Call to undefined function get_editable_roles())
<?php
class EmailPublisherSettings {
private $roles = array();
public function __construct() {
add_action('admin_init', array($this, 'admin_init'));
$page = create_settings_page(
'email_publisher',
__( 'Email Publisher Settings' ),
array(
'title' => __( 'Email Publisher' ),
'parent' => 'options-general.php'
),
array(
'rt_ep_general_setting' => array(
'title' => __( 'General' ),
'description' => __( 'This is my section description.' ),
'fields' => array(
'rt_ep_roles' => array(
'type' => 'multi',
'label' => __( 'Send email to', 'rt_ep' ),
'options' => $this->roles
)
)
),
),
array(
'tabs' => true
)
);
$page->apply_settings( array(
'rt_ep_template_setting' => array(
'title' => __( 'Template' ),
'fields' => array(
'rt_ep_template_subject' => array(
'type' => 'text',
'label' => __( 'Subject' )
),
'rt_ep_template_email_from_name' => array(
'type' => 'text',
'label' => __( 'Email from name' )
),
'rt_ep_template_email_from' => array(
'type' => 'email',
'label' => __( 'Email from' )
),
'rt_ep_template_email_body' => array(
'type' => 'textarea',
'label' => __( 'Email body' ),
)
),
),
) );
}
public function admin_init() {
$this->roles = $this->_getRoles();
var_dump($this->roles);
}
public function _getRoles() {
$roles = array();
foreach(get_editable_roles() as $index => $role) {
$roles[$index] = __($role['name']);
}
return $roles;
}
}
Hi !
Well it could be tricky. The class needs to apply the settings before admin_init. Have you tried during wp_loaded ?
I don't have time to run tests right now, but maybe we could also do with a priority argument here : https://github.com/WebMaestroFr/wm-settings/blob/master/wm-settings.php#L47 ...