WordPress.org

Ready to get started?Download WordPress

Forums

get_options isn't returning a value when used inside sidebar.php (3 posts)

  1. gotlisch
    Member
    Posted 5 months ago #

    Hi there, have come across a strange problem, I'm unable to return and display one of my theme options when I try to call get_options() from within my sidebar.php file.

    I've tried to call the same option from other page templates such as index.php, header.php etc and it works perfectly fine but as soon as I call the option from the sidebar file I get nothing...no idea what's going on here.

    This is the code I'm using...like I said the option is definetly being stored properly cause i'm able to access it perfectly fine from other parts of my theme.

    <div id="sidebar">		     
    
                <?php if ( !function_exists('dynamic_sidebar') || !dynamic_sidebar('Sidebar') ) : ?>        
    
                    <div class="side_box">
    				<? if(get_field('google-maps'))	{?>
    
    				<h3><? echo get_option($shortname.'_google_maps_heading',''); ?></h3>				
    
    				<iframe src="<? echo get_field('google-maps') ?>" width="300" height="250" frameborder="0" style="border:0"></iframe>						
    
    				<?}?>
    
    				</div><!--//side_box-->                
    
                <?php endif; ?>        
    
            </div><!--//sidebar-->

    Thanks in advance for your help!!

    Mikael

  2. Steven Jones
    Member
    Posted 5 months ago #

    It looks like you're expecting $shortname to be populated with some data, but it's going to be empty unless it's a global variable.

    If you have used $shortname in your main script that calls the sidebar then you need to populate it again in sidebar.php

  3. gotlisch
    Member
    Posted 5 months ago #

    Hi Steven, $shortname is a global varible...it's just being used as a prefix to make sure my global varibles don't conflict with any other ones.. this is how it's been setup in the settings.php file:

    <?php
    
    $themename = "Simple Grid";
    $shortname = "simple_grid";
    $settings_list = array(
    	'home_page_heading_ln1',
    	'home_page_heading_ln2',
    	'home_page_sub_heading',
    	'google_maps_heading',
        'custom_logo_url',
        'featured_banner_url',
        'custom_background_color',
        'facebook_link',
        'twitter_link',
        'dribbble_link',
        'google_plus_link',
        'pinterest_link',		'home_page_title',
        'post_hover_background_color'
    );
    
    /*function add_homestead_scripts() {
    //  wp_enqueue_script('homestead-settings1', WP_CONTENT_URL.'/themes/homestead/jquery.js');
    
      wp_enqueue_script('homestead-settings2', WP_CONTENT_URL.'/themes/homestead/jquery-1.4.2.min.js');
      wp_enqueue_script('homestead-settings3', WP_CONTENT_URL.'/themes/homestead/jpicker-1.1.3.min.js');
    
      //wp_enqueue_script('homestead-settings1', WP_CONTENT_URL.'/themes/homestead/css/colorpicker.css');
    }*/
    
    function mytheme_add_admin() {
    
        global $themename, $shortname, $settings_list;
    
        if ( $_GET['page'] == basename(__FILE__) ) {
    
            if ( 'save' == $_REQUEST['action'] ) {
    
                      foreach($settings_list as $value) {
                        //echo '<script type="text/javascript">alert("' . $value . '");</script>';
                        //if($_REQUEST[$value] != "")
                          //update_option($shortname . '_' . $value,$_REQUEST[$value]);
                          update_option($shortname . '_' . $value,mysql_real_escape_string($_REQUEST[$value]));
                      }
    
                      header("Location: themes.php?page=settings.php&saved=true");
                      die;
    
            }
        }
        add_theme_page($themename." Settings", $themename." Settings", 'edit_themes', basename(__FILE__), 'mytheme_admin');
    
    }
    
    function mytheme_admin() {
    
        global $themename, $shortname, $settings_list;
    
        if ( $_REQUEST['saved'] ) echo '<div id="message" class="updated fade"><p><strong>'.$themename.' settings saved.</strong></p></div>';
        if ( $_REQUEST['reset'] ) echo '<div id="message" class="updated fade"><p><strong>'.$themename.' settings reset.</strong></p></div>';
    
    ?>
    <style type="text/css">
    
    table { border: none; }
    td { padding: 5px; }
    .ss_text { width: 350px; }
    
    </style>
    
    <div class="wrap">
    <h2><?php echo $themename; ?> Theme Options</h2>
    
    <form method="post">
    
    <table>
    <tr>
    <td>Home Page Heading ln1:</td>
    <td><input type="text" name="home_page_heading_ln1" class="ss_text" value="<?php echo stripslashes(stripslashes(get_option($shortname.'_home_page_heading_ln1',''))); ?>" /></td></tr>
    <tr>
    
    ....

Reply

You must log in to post.

About this Topic