widget coding issue in multisite

I have a select widget in a multisite installation. The user selects one of several images and adds a link to a page. I hard coded links to the images. It works, but when I changed from my development server to the actual server, of course the domain and url changed and I had to hard-code everything again and lost all the widgets.

That's okay for one site, but won't work once the other sites are in place (I anticipate working with the site on the development server again). First time doing this coding and I can't figure out how to make it dynamic. Have tried a couple of things and they don't work.

The images are in the theme folder and the theme is "rainbow." Here's the code (some options omitted to make this shorter):

// Output the admin options form
    function form($instance) {
        ?>
            <p>
                <label for="<?php echo $this->get_field_id('title'); ?>"><?php _e('Title:'); ?></label>
                <input id="<?php echo $this->get_field_id('title'); ?>" name="<?php echo $this->get_field_name('title'); ?>" value="<?php echo $instance['title']; ?>" type="text" class="widefat" />
            </p>
			<p>
	            <label for="<?php echo $this->get_field_id('image'); ?>">Select image:</label>
	            <select id="<?php echo $this->get_field_id('image'); ?>" name="<?php echo $this->get_field_name('image'); ?>" class="widefat" style="width:100%;">
	                <option value="http://localhost/rainbow/wp-content/themes/rainbow/images/widgets/built-to-last.jpg" <?php selected('http://localhost/rainbow/wp-content/themes/rainbow/images/widgets/built-to-last.jpg', $instance['image']); ?>>Built to Last</option>
					<option value="http://localhost/rainbow/wp-content/themes/rainbow/images/widgets/come-visit.jpg" <?php selected('http://localhost/rainbow/wp-content/themes/rainbow/images/widgets/come-visit.jpg', $instance['image']); ?>>Come and Visit</option>
	            </select>
	        </p>
            <p>
                <label for="<?php echo $this->get_field_id('linkurl'); ?>"><?php _e('Page you are linking to:'); ?></label>
                <input id="<?php echo $this->get_field_id('linkurl'); ?>" name="<?php echo $this->get_field_name('linkurl'); ?>" value="<?php echo $instance['linkurl']; ?>" type="text" class="widefat" />
            </p>
        <?php
    }
  • aecnu

    Greetings flojnel,

    I would like to suggest to avoid all problems of this type in the future to use the same domain at the development server as well as the production server like I do in my personal development/production operation.

    There are a couple of ways to do this.

    One is if you are on a Windows computer to modify the Hosts file pointing the domain to which sever your prefer --> C:\Windows\System32\drivers\etc\hosts

    Two is much more convenient using Firefox and the add on https://addons.mozilla.org/en-US/firefox/addon/hostadmin/ which can switch you between the two.

    Addressing your current issue with the coding in your post, I admit that I will have to have one of our coders take a look at it. This may take a bit longer then a normal ticket but i will see if I can get a coder in here asap to give us some advice or advise us as you have requested regarding your current coding above.

    Cheers, Joe

  • flojnel

    Thanks - I'm hoping someone can help me modify the current code so the domain is dynamic.

    I don't understand the suggestions about modifying the hosts files. Are you saying the code in my files could have hard-coded links to a particular domain ("mydomain") as an example and run on localhost as though localhost were "mydomain"?

    And you could then make changes to the site while the rest of the Internet would see the original site at "mydomain."

    Is that what you're saying?

    Flo

  • aecnu

    Greetings flojnel,

    Though I will certainly try to get a developer in here asap to give us advice/advise us with the code you have given I was considering the way we do our own development.

    Perhaps if I explain the way it works with the big picture you will understand just how brilliant it is.

    Server "A" is your production server and assigned the domain name mydomain.com and for example it is in the US

    Server "B" is your development server bearing the same domain name mydomain.com and for example this server is in your living room in Europe.

    When you type in your web browser mydomain.com the browser is sent by DNS to the server in the United States.

    But what if you could override DNS (more accurately override your local DNS) to send your browser to mydomain.com to the server in your living room in Europe without affecting anything else in the world?

    And when you flip a switch so to speak you can bounce back and forth between the two at your convenience?

    This would give you the capability to develop on the server in your living room in Europe as if you were on the production server in the United States not having to worry about domain names, url paths, or anything else related because both the servers are using the same domain name.

    So you have your switch set to work with your development environment in Europe and when it is all said and done you flip the switch to the server in the US and upload the entire finished development site to the server in the US and it all works flawlessly because the domain name is the same.

    The switch is the plugin I suggested. Think about it - it is brilliant.

    I shall now go ahead and ping the developer as I mentioned I would earlier.

    Cheers, Joe

  • gogger

    Im not 100% sure what you want to do - but if your just trying to make the code generate the URLs on the fly so you can move it from install to install, you could use this in your URLs:

    <?php echo get_template_directory_uri(); ?>

    Example:
    <option value="<?php echo get_template_directory_uri(); ?>/images/widgets/built-to-last.jpg" <?php selected('<?php echo get_template_directory_uri(); ?>/images/widgets/built-to-last.jpg', $instance['image']); ?>>Built to Last</option>

    of course if you changed theme this would break unless the images were in the same folders in another theme...

  • flojnel

    That gets me halfway there. I'd actually tried that with template_directory before and had the same problem. It saves just fine, the new image shows up, but the select is wrong afterwards. It just shows the first item in the list.

    I use notepad++ and I it's not recognizing the php code after "selected" as code:
    <option value="<?php echo get_template_directory_uri(); ?>/images/widgets/built-to-last.jpg" <?php selected('<?php echo get_template_directory_uri(); ?>/images/widgets/built-to-last.jpg', $instance['image']); ?>>Built to Last</option>

    Any ideas?

  • gogger

    So before you put in <?php echo get_template_directory_uri(); ?> all the options show up but its not dynamic - but after you put them in the images show up but only the first item in the list shows up?

    try this - you cant nest <?php > tags in the way i did - sorry:

    <option value="<?php echo get_template_directory_uri(); ?>/images/widgets/built-to-last.jpg" <?php $template_dir = get_template_directory_uri(); selected( echo $template_dir."/images/widgets/built-to-last.jpg", $instance['image']); ?>>Built to Last</option>

Thank NAME, for their help.

Let NAME know exactly why they deserved these points.

Gift a custom amount of points.