web-dev-qa-db-fra.com

Accéder aux options de thème dans les modèles

Je suis nouveau dans le développement WordPress ...

Donc, je dois faire écho $theme_opts['content_width'] dans page.php, index.php, sigle.php, etc ... Dans header.php fonctionne très bien.

<?php echo $theme_opts['content_width']; ?><br>
sidebar will be
<?php echo 12 - $theme_opts['content_width']; ?>

J'écris ce code pour le tester, mais ça ne marche pas.

j'ai essayé de créer $theme_opts global et cela n'a pas fonctionné.

Ce sont mes codes dans: functions.php

include( get_template_directory() . '/includes/activate.php' );
include( get_template_directory() . '/includes/admin/themeoptions/options-page.php' );
include( get_template_directory() . '/includes/admin/themeoptions/init.php' );
include( get_template_directory() . '/includes/admin/process/save-options.php' );

add_action( 'after_switch_theme', 'bt_activate' );
add_action( 'admin_init', 'bt_admin_init' );

alors j'ai activate.php

<?php

function bt_activate() {
    if( version_compare( get_bloginfo( 'version' ), '4.2', '<' ) ) {
        wp_die( __('You must have a minimum version of 4.2 to use this theme.') );
    }

    $theme_opts                 =   get_option( 'bt_opts' );
    if( !$theme_opts ) {
        $opts                   =   array(
            'facebook'          =>      '',
            'Twitter'           =>      '',
            'youtube'           =>      '',
            'logo_type'         =>      1,
            'logo_img'          =>      '',
            'footer'            =>      '',
            'content_width'     =>      8,
        );
        add_option( 'bt_opts', $opts );
    }
}

Et save-options.php

<?php

function bt_save_options() {

    if( !current_user_can( 'moderate_comments' ) ) {

        wp_die( __('You are not allowed to be on this page.') );

    }

    check_admin_referer( 'bt_options_verify' );

    $opts                   =       get_option('bt_opts');
    $opts['Twitter']        =       sanitize_text_field($_POST['bt_inputTwitter']);
    $opts['facebook']       =       sanitize_text_field($_POST['bt_inputFacebook']);
    $opts['youtube']        =       sanitize_text_field($_POST['bt_inputYoutube']);
    $opts['logo_type']      =       absint( $_POST['bt_inputLogoType'] );
    $opts['footer']         =       $_POST['bt_inputFooter'];
    $opts['logo_img']       =       esc_url_raw($_POST['bt_inputLogoImage']);
    $opts['content_width']  =       absint( $_POST['bt_contentWidth'] );

    update_option( 'bt_opts', $opts );
    wp_redirect( admin_url('admin.php?page=bt_theme_opts&status=1') );

}

et c'est init.php

<?php

function bt_admin_init() {

    include( 'enqueue.php' );

    add_action( 'admin_enqueue_scripts', 'bt_admin_enqueue');
    add_action( 'admin_post_bt_save_options', 'bt_save_options' );

}

options-page.php (dans le back-end)

<?php
    function bt_theme_opts_page() {
    $theme_opts                 =       get_option('bt_opts');
?>
    <div class="wrap">
        <?php
            if ( isset( $_GET['status'] ) && $_GET['status'] == 1 ) {
        ?>
            <div class="alert alert-success alert-dismissible" role="alert">
                <button type="button" class="close" data-dismiss="alert" aria-label="Close"><span aria-hidden="true">&times;</span></button>
                <strong><?php _e('Well done!','cssecotheme' ); ?></strong>
                <?php _e('You successfully update your settings.','cssecotheme' ); ?>
            </div>
        <?php
            }
        ?>
        <div class="panel panel-info">
            <div class="panel-heading">
                <h3 class="panel-title"><?php _e('CSSeco Theme Setting', 'cssecotheme' ); ?></h3>
            </div>
            <form method="post" action="admin-post.php">
                <div class="panel-body">
                    <div class="row">
                        <div class="col-sm-6">

                            <input type="hidden" name="action" value="bt_save_options">
                            <?php wp_nonce_field( 'bt_options_verify' ); ?>

                            <div class="adminFormSocialIconsWrapper">
                                <div class="page-header" style="margin-top: 0;">
                                    <h3>
                                        <?php _e('Social Icons','cssecotheme' ); ?>
                                        <small> Twitter, Facebook, Youtube, etc</small>
                                    </h3>
                                </div>
                                <div class="form-group">
                                    <label for="bt_inputTwitter"><?php _e( 'Twitter', 'cssecotheme' ); ?></label>
                                    <div class="input-group">
                                        <span class="input-group-addon" id="addon_Twitter">
                                            <i class="fa fa-Twitter" aria-hidden="true"></i>
                                        </span>
                                        <input type="text" id="bt_inputTwitter" class="form-control" name="bt_inputTwitter" value="<?php echo $theme_opts['Twitter']; ?>" aria-describedby="addon_Twitter">
                                    </div>
                                </div>
                                <div class="form-group">
                                    <label for="bt_inputFacebook"><?php _e( 'Facebook', 'cssecotheme' ); ?></label>
                                    <div class="input-group">
                                        <span class="input-group-addon" id="addon_Twitter">
                                            <i class="fa fa-facebook" aria-hidden="true"></i>
                                        </span>
                                        <input type="text" id="bt_inputFacebook" class="form-control" name="bt_inputFacebook" value="<?php echo $theme_opts['facebook']; ?>">
                                    </div>
                                </div>
                                <div class="form-group">
                                    <label for="bt_inputYoutube"><?php _e( 'Youtube', 'cssecotheme' ); ?></label>
                                    <div class="input-group">
                                        <span class="input-group-addon" id="addon_Twitter">
                                            <i class="fa fa-youtube" aria-hidden="true"></i>
                                        </span>
                                        <input type="text" id="bt_inputYoutube" class="form-control" name="bt_inputYoutube" value="<?php echo $theme_opts['youtube']; ?>">
                                    </div>
                                </div>
                            </div>

                            <div class="adminFormLogoWrapper">
                                <div class="page-header">
                                    <h3>
                                        <?php _e('Logo','cssecotheme' ); ?>
                                        <small><?php _e('Image or Text','cssecotheme' ); ?></small>
                                    </h3>
                                </div>
                                <div class="form-group">
                                    <label for="bt_inputLogoType"><?php _e( 'Logo Type', 'cssecotheme' ); ?></label>
                                    <select id="bt_inputLogoType" class="form-control" name="bt_inputLogoType">
                                        <option value="1"><?php _e( 'Site Name', 'cssecotheme' ); ?></option>
                                        <option value="2" <?php echo $theme_opts['logo_type'] == 2 ? 'SELECTED' : '' ;?>><?php _e( 'Image', 'cssecotheme' ); ?></option>
                                    </select>
                                </div>
                                <div class="input-group">
                                    <input type="text" class="form-control" placeholder="Logo Image" name="bt_inputLogoImage" value="<?php echo $theme_opts['logo_img']; ?>">
                                    <span class="input-group-btn">
                                        <button class="btn btn-default" type="button" id="bt_uploadLogoImgBtn"><?php _e( 'Upload', 'cssecotheme' ); ?></button>
                                    </span>
                                </div>
                            </div>

                            <div class="adminFormFooterWrapper">
                                <div class="page-header">
                                    <h3>
                                        <?php _e('Footer','cssecotheme' ); ?>
                                        <small><?php _e('Add some text between footer widgets and footer menu','cssecotheme' ); ?></small>
                                    </h3>
                                </div>
                                <div class="form-group">
                                    <label for="bt_inputFooter"><?php _e( 'Footer text(HTML Allowed)', 'cssecotheme' ); ?></label>
                                    <textarea id="bt_inputFooter" name="bt_inputFooter" class="form-control"><?php echo stripslashes_deep($theme_opts['footer']); ?></textarea>
                                </div>
                            </div>
                        </div>
                        <div class="col-sm-6">
                            <div class="adminFormLogoWrapper">
                                <div class="page-header"  style="margin-top: 0;">
                                    <h3>
                                        <?php _e('Content width','cssecotheme' ); ?>
                                        <small><?php _e('The difference until 12 it will be the widget area width','cssecotheme' ); ?></small>
                                    </h3>
                                </div>
                                <div class="form-group">
                                    <label for="bt_contentWidth"><?php _e( 'Select width', 'cssecotheme' ); ?></label>
                                    <select id="bt_contentWidth" class="form-control" name="bt_contentWidth">
                                        <option value="1" <?php echo $theme_opts['content_width'] == 1 ? 'SELECTED' : '' ;?>>1</option>
                                        <option value="2" <?php echo $theme_opts['content_width'] == 2 ? 'SELECTED' : '' ;?>>2</option>
                                        <option value="3" <?php echo $theme_opts['content_width'] == 3 ? 'SELECTED' : '' ;?>>3</option>
                                        <option value="4" <?php echo $theme_opts['content_width'] == 4 ? 'SELECTED' : '' ;?>>4</option>
                                        <option value="5" <?php echo $theme_opts['content_width'] == 5 ? 'SELECTED' : '' ;?>>5</option>
                                        <option value="6" <?php echo $theme_opts['content_width'] == 6 ? 'SELECTED' : '' ;?>>6</option>
                                        <option value="7" <?php echo $theme_opts['content_width'] == 7 ? 'SELECTED' : '' ;?>>7</option>
                                        <option value="8" <?php echo $theme_opts['content_width'] == 8 ? 'SELECTED' : '' ;?>>8</option>
                                        <option value="9" <?php echo $theme_opts['content_width'] == 9 ? 'SELECTED' : '' ;?>>9</option>
                                        <option value="10" <?php echo $theme_opts['content_width'] == 10 ? 'SELECTED' : '' ;?>>10</option>
                                        <option value="11" <?php echo $theme_opts['content_width'] == 11 ? 'SELECTED' : '' ;?>>11</option>
                                        <option value="12" <?php echo $theme_opts['content_width'] == 12 ? 'SELECTED' : '' ;?>>12</option>
                                    </select>
                                    <?php echo $theme_opts['content_width']; ?><br>
                                    sidebar will be
                                    <?php echo 12 - $theme_opts['content_width']; ?>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
                <div class="panel-footer text-right">
                    <button class="btn btn-info" type="submit"><?php _e( 'Update', 'cssecotheme' ); ?></button>
                </div>
            </form>
        </div>
    </div>
<?php
    }
?>

d'accord?

et page.php

<?php get_header(); ?>


<?php global $theme_opts; ?>

<?php echo $theme_opts['content_width']; ?><br>

    sidebar will be

<?php echo 12 - $theme_opts['content_width']; ?>



    <div class="row">
        <section class="col-md-9">
            <?php
                if ( have_posts() ) {
                    while( have_posts() ) {
                        the_post();
            ?>
                <article id="post-<?php the_ID(); ?>" <?php post_class( $clase_panel_main_content ); ?>>
                    <header class="panel-heading blog-post-header">
                        <h2 class="panel-title sp-title">
                            <?php
                            // Title
                            the_title();
                            ?>
                        </h2>
                    </header>
                    <div class="row panel-body">
                        <?php
                        // Feature Image
                        if ( has_post_thumbnail() ) {
                            ?>
                            <div class="col-md-12 custom-featured-img-p">
                                <div class="featimg-wrapper">
                                    <?php
                                    the_post_thumbnail('full', array(
                                        'class' => 'img-responsive img-rounded'
                                    ));
                                    ?>
                                </div>
                            </div>
                            <?php
                        }
                        ?>
                        <div class="col-md-12 blog-post-content">
                            <?php
                            // The content
                            the_content();
                            ?>
                        </div>
                    </div>
                    <footer class="panel-footer blog-post-footer">
                        <?php
                        // Paginated posts
                        wp_link_pages(array(
                            'before'           => '<p class="text-center">' . __( 'Pages:' ),
                            'after'            => '</p>',
                        ));
                        ?>
                        <div class="sp-tags">
                            <?php
                            // Tags
                            the_tags();
                            ?>
                        </div>
                    </footer>
                </article>
            <?php
                    }
                }
            ?>
        </section>
        <aside class="col-md-3">
            <?php get_sidebar(); ?>
        </aside>
    </div>

<?php get_footer(); ?>
1
Bobbaru Krueger

La fonction bt_theme_opts_page() gère les options de votre thème. Cette fonction contient cette ligne:

$theme_opts = get_option('bt_opts');

Les options de votre thème sont donc stockées dans bt_opts. Pour le récupérer, utilisez le même code dans votre fichier page.php. Donc vous auriez besoin de:

$theme_opts = get_option('bt_opts');
echo $theme_opts['content_width'].' sidebar will be '.(12 - $theme_opts['content_width']);
1
Jack Johansson