• I’m trying to learn how to create widgets and I have 2 questions:
    first… where can I put my sidebar widget file so that wordpress will load them. I’d like to keep the widget code out of the function file. I tried putting it in it’s own file under wp-content/plugins/widget-name/widget-name.php but that did not work. I would like to make it available to “all” my themes so I did not want to put it in a widget folder under a specific theme theme-name/widgets/widget-name.php as I had read was possible. Is there somewhere I can place my widget-name.php file top make it available to all of my themes?

    Second question is a frustrating coding issue. Could anyone tells me why the following code:

    <blockquote>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'); ?>"
    				type="text"
    				value="<?php echo $instance['title']; ?>" />
    		</p>
    	 	<?php
    	}</blockquote>

    gives me a “Notice: Undefined index: title” in my widget input box when I first place it in the side bar. but the following code does not:

    <blockquote>function form($instance) {
    		echo '<p>';
    		echo '<label for="' . $this->get_field_id("title") .'">Random Trivia:</label>';
    		echo '<input type="text"';
    		echo 'name="' . $this->get_field_name("title") . '" ';
    		echo 'id="' . $this->get_field_id("title") . '" ';
    		echo 'value="' . $instance["title"] . '" />';
    		echo '</p>';
    	}</blockquote>

    (they seem nearly identical to me)?

    Thanks for any help

Viewing 5 replies - 1 through 5 (of 5 total)
  • Thread Starter kjfrank

    (@kjfrank)

    please ignore the “blockquote” tags that was my stupid posting mistake!

    Does your PHP file include the next header?

    Plugin Name: Plugin-name
    Plugin URI: not required
    Description: A small text
    Author: Your name
    Version: 0.1
    Author URI: not required
    Thread Starter kjfrank

    (@kjfrank)

    japmarcus… I assume you are referring to my first question. If so yes I have included the proper header:
    /*
    Plugin Name: Random Trivia Bits
    Plugin URI: #
    Description: A Widget that will display random pieces of trivia in a blog sidebar
    Author: Keith Frank
    Version: 1.0
    */

    The way I did get it to work and getting the error I spoke about in my second question was just by placing it in my functions.php file which I rather not do.

    I think I’d rather include it in a similar way that plugins are included “wp-content/plugins/widget-name/widget-name.php” but as I stated earlier that doesn’t seem to work.

    Thanks.

    For the second error its is easy. Check if the “var” exits

    if(empty($instance['var'])){ $instance['var'] = '';}

    Will prevent a error.

    To make a widget availble for all templates upload it to

    /wp-content/plugins/

    Thread Starter kjfrank

    (@kjfrank)

    I have already tried placing my widget file in the plugins directory and the widget does not show up in the admin panel under available widgets. I should clarify… I have already created a dashboard plugin and placed it in the plugin directory and that works however my sidebar widget does not show up when placed in the plugins dir.

    as far as checking if the “var” exists, that does not explain why my first function works(shows an empty input “Title” box in the admin panel) yet my second function reports an “Notice: Undefined index: title” in that same box. All the rest of the code remains unchanged. I’m just swapping out the two functions. ???? neither function checks. It seems very odd to me.

    Thanks again for your help.

Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘widget coding issue’ is closed to new replies.