I cannot figure out how to change the content om my custom widget.
I am creating a dropdown in the Form function that dropdown items is generated from a table. And when i pick one of the dropdown items and hit the save button i want to load a content from my DB and set the widgets content.
if i pick Item 1 in the dropdown i want to query my db with item 1 and update the content of the widgets content.
How do i do this ? i am aware of a ajax call to get the data from the DB but how do i set the data to the widgets content ?
function form($instance){
$select = esc_attr($instance['select']);
?>
<select>
<?php
require_once('../wp-load.php');
global $wpdb;
$query = $wpdb->get_results("SELECT titel FROM wp_layout");
foreach($query as $result){
echo '<option value="' .$result->titel. '">' .$result->titel. '</option>';
}
?>
</select>
function update($new_instance, $old_instance) {
$instance = $old_instance;
$instance['select'] = strip_tags($new_instance['select']);
return $instance;
}
And i need the
function widget()
To make a custom wordpress widget, then first you have to make your custom widget class that will extend the WP_Widget class of wordpress core.
// Creating the widget
class My_Custom_Widget extends WP_Widget {
function __construct() {
parent::__construct(
// Base ID of your widget
'my_custom_widget',
// Widget name will appear in UI
__('My Custom Widget', 'my_custom_widget_domain'),
// Widget description
array( 'description' => __( 'Sample widget', 'my_custom_widget_domain' ), )
);
}
// Creating widget front-end
// This is where the action happens
public function widget( $args, $instance ) {
$title = apply_filters( 'widget_title', $instance['title'] );
// before and after widget arguments are defined by themes
echo $args['before_widget'];
if ( ! empty( $title ) )
echo $args['before_title'] . $title . $args['after_title'];
// This is where you run the code and display the output
echo __( 'Hello, World!', 'my_custom_widget_domain' );
echo $args['after_widget'];
}
// Widget Backend
public function form( $instance ) {
if ( isset( $instance[ 'title' ] ) ) {
$title = $instance[ 'title' ];
}
else {
$title = __( 'New title', 'my_custom_widget_domain' );
}
// Widget admin form
?>
<p>
<label for="<?php echo $this->get_field_id( 'title' ); ?>"><?php _e( 'Title:' ); ?></label>
<input class="widefat" id="<?php echo $this->get_field_id( 'title' ); ?>" name="<?php echo $this->get_field_name( 'title' ); ?>" type="text" value="<?php echo esc_attr( $title ); ?>" />
</p>
<?php
}
// Updating widget replacing old instances with new
public function update( $new_instance, $old_instance ) {
$instance = array();
$instance['title'] = ( ! empty( $new_instance['title'] ) ) ? strip_tags( $new_instance['title'] ) : '';
return $instance;
}
} // Class ends here
And Register your widget:
// Register and load the widget
function custom_load_widget() {
register_widget( 'my_custom_widget' );
}
add_action( 'widgets_init', 'custom_load_widget' );
And now if you want to make ajax calls then just regitsre your ajax calls via hooks explained Wordpress AJAX