Skip to content

Use create_settings_page in admin_init ? #1

Description

@florentsorel

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

}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions