1

I'm new to WordPress development...

So i need to echo $theme_opts['content_width'] in page.php, index.php, sigle.php, etc... In header.php works fine.

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

I write this code to test it, but it's not working.

i tried making $theme_opts global and it didn't work.

These are my codes in : 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' );

then i have 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 );
    }
}

And 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') );

}

and this is 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 (in 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
    }
?>

ok?

and 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(); ?>
2
  • can you please post the code for one of the pages it does not work in?
    – inarilo
    Jun 11, 2017 at 11:57
  • i added page.php in the post. thx! Jun 11, 2017 at 14:04

1 Answer 1

1

The bt_theme_opts_page() function handles your theme's options, and in that function there is this line:

$theme_opts = get_option('bt_opts');

So your theme's options are being stored in bt_opts. To retrieve it, use the same code in your page.php file. So you would need:

$theme_opts = get_option('bt_opts');
echo $theme_opts['content_width'].' sidebar will be '.(12 - $theme_opts['content_width']);
2
  • thx a lot man... damn! was so easy Jun 11, 2017 at 15:41
  • @BobbaruKrueger You're welcome. If it solved your problem, i would appreciate if you mark the answer as accepted.
    – Johansson
    Jun 11, 2017 at 15:43

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.