0

I have this custom post type:

// Register Custom Post Type Marca
function create_banner_cpt() {
    $labels = array(
        'name' => _x( 'Banner', 'Post Type General Name', 'sc-slider' ),
        'singular_name' => _x( 'Banner', 'Post Type Singular Name', 'sc-slider' ),
        'menu_name' => _x( 'Banner', 'Admin Menu text', 'sc-slider' ),
        'name_admin_bar' => _x( 'Banner', 'Add New on Toolbar', 'sc-slider' ),
        'archives' => __( 'Arquivos de banner', 'sc-slider' ),
        'attributes' => __( 'Atributos de banner', 'sc-slider' ),
        'parent_item_colon' => __( 'Slide anterior:', 'sc-slider' ),
        'all_items' => __( 'Todos os slides', 'sc-slider' ),
        'add_new_item' => __( 'Adicionar novo slide', 'sc-slider' ),
        'add_new' => __( 'Adicionar novo', 'sc-slider' ),
        'new_item' => __( 'Novo slide', 'sc-slider' ),
        'edit_item' => __( 'Editar slide', 'sc-slider' ),
        'update_item' => __( 'Atualizar slide', 'sc-slider' ),
        'view_item' => __( 'Ver slide', 'sc-slider' ),
        'view_items' => __( 'Ver slide', 'sc-slider' ),
        'search_items' => __( 'Procurar slide', 'sc-slider' ),
        'not_found' => __( 'Nenhum slide encontrado', 'sc-slider' ),
        'not_found_in_trash' => __( 'Nenhum slide encontrado na lixeira', 'sc-slider' ),
        'featured_image' => __( 'Inserir imagem no slide', 'sc-slider' ),
        'set_featured_image' => __( 'Definir imagem do slide', 'sc-slider' ),
        'remove_featured_image' => __( 'Remover imagem do slide', 'sc-slider' ),
        'use_featured_image' => __( 'Usar como imagem do slide', 'sc-slider' ),
        'insert_into_item' => __( 'Inserir no slide', 'sc-slider' ),
        'uploaded_to_this_item' => __( 'Atualizar no slide', 'sc-slider' ),
        'items_list' => __( 'Lista de slides', 'sc-slider' ),
        'items_list_navigation' => __( 'Navegar na lista de slides', 'sc-slider' ),
        'filter_items_list' => __( 'Filtrar lista de slides', 'sc-slider' ),
    );
    $args = array(
        'label' => __( 'Banner', 'sc-slider' ),
        'description' => __( 'Banner rotativo', 'sc-slider' ),
        'labels' => $labels,
        'menu_icon' => 'dashicons-welcome-write-blog',
        'supports' => array('title', 'editor', 'thumbnail'),
        'taxonomies' => array(),
        'public' => true,
        'show_ui' => true,
        'show_in_menu' => true,
        'menu_position' => 25,
        'show_in_admin_bar' => true,
        'show_in_nav_menus' => false,
        'can_export' => false,
        'has_archive' => false,
        'hierarchical' => false,
        'exclude_from_search' => true,
        'show_in_rest' => true,
        'publicly_queryable' => false,
        'capability_type' => 'post',
    );
    register_post_type( 'sc-slider', $args );
}
add_action( 'init', 'create_banner_cpt', 0 );

I wanna include this option page:

// Settings Page: Configurar banner
class configuraes_Settings_Page {

    public function __construct() {
        add_action( 'admin_menu', array( $this, 'wph_create_settings' ) );
        add_action( 'admin_init', array( $this, 'wph_setup_sections' ) );
        add_action( 'admin_init', array( $this, 'wph_setup_fields' ) );
    }

    public function wph_create_settings() {
        $page_parent = 'edit.php?post_type=sc-slider';
        $page_title = 'Configurações do banner';
        $menu_title = 'Configurar banner';
        $capability = 'manage_banner_options';
        $slug = 'configuraes';
        $callback = array($this, 'wph_settings_content');
        add_submenu_page($page_parent, $page_title, $menu_title, $capability, $slug, $callback);
    }

    public function wph_settings_content() { ?>
        <div class="wrap">
            <h1>Configurações do banner</h1>
            <?php settings_errors(); ?>
            <form method="POST" action="options.php">
                <?php
                    settings_fields( 'configuraes' );
                    do_settings_sections( 'configuraes' );
                    submit_button();
                ?>
            </form>
        </div> <?php
    }

    public function wph_setup_sections() {
        add_settings_section( 'configuraes_section', 'Atenção: As funções a seguir estarão sujeitas a disponibilidade do tema.', array(), 'configuraes' );
    }

    public function wph_setup_fields() {
        $fields = array(
            array(
                'label' => 'Efeito de transição',
                'id' => 'sc_slider_transition',
                'type' => 'radio',
                'section' => 'configuraes_section',
                'options' => array(
                    'fade' => 'fade',
                    'fadeout' => 'fadeout',
                    'scrollHorz' => 'scrollHorz',
                    'none' => 'none',
                ),
            ),
            array(
                'label' => 'Altura do banner',
                'id' => 'sc_slider_height',
                'type' => 'select',
                'section' => 'configuraes_section',
                'options' => array(
                    '400 px' => '400 px',
                    '500 px' => '500 px',
                    '800 px' => '800 px',
                    '100%' => '100%',
                ),
            ),
            array(
                'label' => 'Mostrar paginação',
                'id' => 'sc_slider_pager',
                'type' => 'radio',
                'section' => 'configuraes_section',
                'options' => array(
                    'Sim' => 'Sim',
                    'Não' => 'Não',
                ),
            ),
            array(
                'label' => 'Estilo da paginação',
                'id' => 'sc_slider_pager_template',
                'type' => 'select',
                'section' => 'configuraes_section',
                'options' => array(
                    'Números' => 'Números',
                    'Pontos' => 'Pontos',
                ),
            ),
            array(
                'label' => 'Velocidade das transições',
                'id' => 'sc_slider_speed',
                'type' => 'number',
                'section' => 'configuraes_section',
                'desc' => 'Velocidade em segundos',
            ),
            array(
                'label' => 'Tempo para troca de slide',
                'id' => 'sc_slider_timeout',
                'type' => 'number',
                'section' => 'configuraes_section',
                'desc' => 'Tempo em segundos',
            ),
            array(
                'label' => 'Pausar ao parar o mouse',
                'id' => 'sc_slide_mouse',
                'type' => 'radio',
                'section' => 'configuraes_section',
                'options' => array(
                    'Sim' => 'Sim',
                    'Não' => 'Não',
                ),
            ),
        );
        foreach( $fields as $field ){
            add_settings_field( $field['id'], $field['label'], array( $this, 'wph_field_callback' ), 'configuraes', $field['section'], $field );
            register_setting( 'configuraes', $field['id'] );
        }
    }

    public function wph_field_callback( $field ) {
        $value = get_option( $field['id'] );
        $placeholder = '';
        if ( isset($field['placeholder']) ) {
            $placeholder = $field['placeholder'];
        }
        switch ( $field['type'] ) {
                case 'radio':
                    if( ! empty ( $field['options'] ) && is_array( $field['options'] ) ) {
                        $options_markup = '';
                        $iterator = 0;
                        foreach( $field['options'] as $key => $label ) {
                            $iterator++;
                            if (!is_array($value)) {
                                $value = str_split($value);
                            };
                            $options_markup.= sprintf('<label for="%1$s_%6$s"><input id="%1$s_%6$s" name="%1$s[]" type="%2$s" value="%3$s" %4$s /> %5$s</label><br/>',
                            $field['id'],
                            $field['type'],
                            $key,
                            checked($value[array_search($key, $value, true)], $key, false),
                            $label,
                            $iterator
                            );
                            }
                            printf( '<fieldset>%s</fieldset>',
                            $options_markup
                            );
                    }
                    break;
                case 'select':
                case 'multiselect':
                    if( ! empty ( $field['options'] ) && is_array( $field['options'] ) ) {
                        $attr = '';
                        $options = '';
                        foreach( $field['options'] as $key => $label ) {
                            $selectedvalue = false;
                            if (is_array($value))  {
                                $selectedvalue = array_search($key, $value, true);
                            };
                            $options.= sprintf('<option value="%s" %s>%s</option>',
                                $key,
                                selected($value[$selectedvalue], $key, false),
                                $label
                            );
                        }
                        if( $field['type'] === 'multiselect' ){
                            $attr = ' multiple="multiple" ';
                        }
                        printf( '<select name="%1$s[]" id="%1$s" %2$s>%3$s</select>',
                            $field['id'],
                            $attr,
                            $options
                        );
                    }
                    break;
            default:
                printf( '<input name="%1$s" id="%1$s" type="%2$s" placeholder="%3$s" value="%4$s" />',
                    $field['id'],
                    $field['type'],
                    $placeholder,
                    $value
                );
        }
        if( isset($field['desc']) ) {
            if( $desc = $field['desc'] ) {
                printf( '<p class="description">%s </p>', $desc );
            }
        }
    }
}
new configuraes_Settings_Page();

But this submenu doesn't appears. How can I do this?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.