Jump to content


Photo
Open

Custom widget - create and add



  • Please log in to reply
1 reply to this topic

#1 icex

icex

    Newbie

  • Members
  • Pip
  • 3 posts

Posted 28 September 2012 - 08:57 AM

Hi, I need to cerate and add a widget for view an extract from the mysql database to my website. In regular use templates inserted code (see you bellow) into function.php, but in pagelines it does not work. I have installed the pagelines-customize and pagelines-pagelines-template-theme. My code widget is trying to insert in your function.php pagelines-customize. In list of widget in wordpress, unfortunately I do not see it.
Can you help me, please? Where is my mistake? Is code correct? Is putting to the file. php correct?

Thank you for your help.

 
class userWidget extends WP_Widget {

function userWidget() {
	 parent::WP_Widget(false, $name = 'userWidget');
}

function widget($args, $instance) {
	 extract($args);
	 $title = apply_filters('widget_title', $instance['title']);
	 echo $before_widget;
	 if ($title)
		 echo $before_title . $title . $after_title;
	
//place for my code ...

	 echo $after_widget;
}

function update($new_instance, $old_instance) {
	 $instance = $old_instance;
	 $instance['title'] = strip_tags($new_instance['title']);
	 return $instance;
}

function form($instance) {
	 $title = esc_attr($instance['title']);
	 ?>
	 <p>
		 <label for="<?php echo $this->get_field_id(&#39;title&#39;); ?>"><?php _e(&#39;Title:&#39;); ?></label>
		 <input id="<?php echo $this->get_field_id(&#39;title&#39;); ?>" name="<?php echo $this->get_field_name(&#39;title&#39;); ?>" type="text" value="<?php echo $title; ?>" />
	 </p>
	 <?php
}

}

add_action(&#39;widgets_init&#39;, create_function(&#39;&#39;, &#39;return register_widget("userWidget");&#39;));


#2 Simon_P

Simon_P

    Messer

  • Administrators
  • 8388607 posts
  • LocationDevon
  • Framework Version:2.0
  • Country: Country Flag

Posted 28 September 2012 - 12:16 PM

The code will have to go in a child themes functions.php, or a custom plugin. The customize plugin runs at 'template_redirect' which is after 'widgets_init'