WebMaestroFr / wm-settings

Based on the WordPress Settings API, this class generates options pages. It supports all basic input types, selects and all, but also media uploads, which is quite neat.
http://webmaestro.fr/wordpress-settings-api-options-pages/
21 stars 2 forks source link

Use create_settings_page in admin_init ? #1

Open florentsorel opened 9 years ago

florentsorel commented 9 years ago

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;
    }

}
WebMaestroFr commented 9 years ago

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 ...