Ad

How To Make Drag And Drop Widget And Put Position Into Database

- 1 answer

How can i make for example Worpdpress Widget. I want a area dragged and dropped and hold its position into the database. Does somebody has tips/ideas? Thanks!

Ad

Answer

Create a new plugin and open init.php file.

add following code(this is an example code for a widget)

<?php
/*
Plugin Name: Example: My User Widget
Description: This plugin provides a simple widget that shows the name of
the logged in user
*/
class My_User_Widget extends WP_Widget {
function My_User_Widget() {
parent::WP_Widget(false,’My User Widget’);
}
function widget($args) {
$user=wp_get_current_user();
if(!isset($user->user_nicename)) {
$message=’Welcome Guest’;
}
else {
$message=”You are logged in as {$user->user_nicename}”;
}
extract($args);
echo $before_widget;
echo “<p>$message</p>”;
echo $after_widget;
}
}
function register_my_user_widget() {
register_widget(‘My_User_Widget’);
}
add_action(‘widgets_init’,’register_my_user_widget’);

widgets_init hook will call register_my_user_widget method. Then My_User_Widget Class will be called. once you activated the widget you can see it in widgets window and drag and drop it where you want.

Ad
source: stackoverflow.com
Ad