Wordpress get post ID from Widget - wordpress

I am trying to get the post ID outside the loop from a widget, but I get some another page ID instead the current post ID.
My question similar to wordpress get post id from side widget, but the accepted answer here (add add_action("widgets_init", "your_init");) does not help.
I have tried:
$post_id = get_the_id();
// or
$post_id = $GLOBALS['post']->ID;
// or
global $wp_query;
$post_id = $wp_query->post->ID;
// or
global $post;
$post_id = $post->ID;
All return some page ID, not the current post ID.
Here is my post link http://ori.co.il/roofdagan1/?p=1492
My widget code:
<?php
/*
Plugin Name: custom-post-field
Description: custom-post-field
*/
// Creating the widget
class cpf_widget extends WP_Widget
{
function __construct()
{
parent::__construct(
// Base ID of your widget
'cpf_widget',
// Widget name will appear in UI
__('CPF Widget', 'cpf_widget_domain'),
// Widget description
array( 'description' => __( 'Custom Post Field Widget', 'cpf_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'];
$post_id = $GLOBALS['post']->ID;
$resoult_ = "Post Id : ". $post_id;
// This is where you run the code and display the output
echo __( $resoult_, 'cpf_widget_domain' );
echo $args['after_widget'];
}
// Widget Backend
public function form( $instance )
{
if ( isset( $instance[ 'title' ] ) )
{
$title = $instance[ 'title' ];
}
else
{
$title = __( 'New title', 'cpf_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 cpf_widget ends here
// Register and load the widget
function cpf_load_widget()
{
register_widget( 'cpf_widget' );
}
add_action( 'widgets_init', 'cpf_load_widget' );
?>

You can use get_queried_object() to retrieve the currently-queried object:
$current_post = get_queried_object();
$post_id = $current_post ? $current_post->ID : null;

Related

Using a widget to add cart shortcode in WooCommerce Archive product pages

I created widget to add [woocommerce_cart] to my archive product sidebar
Number of product per row is set to 3.
After submit add to cart the number of product per row change to 2 !
Any guidance on that?
Here is my archive product link:
Here is the widget code:
// Creating the widget basket
class wpb_widget extends WP_Widget {
function __construct() {
parent::__construct(
// Base ID of your widget
'wpb_widget',
// Widget name will appear in UI
__('ابزارک سبد خرید', 'wpb_widget_domain'),
// Widget description
array( 'description' => __( 'سبد خرید اختصاصی ', 'wpb_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 widget arguments are defined by themes
echo $args['before_widget'];
// This is where you run the code and display the output
?>
<div class="header__card-icon" >
<a href="<?php echo wc_get_cart_url(); ?>"title="<?php is_rtl() ? _e( 'نمایش سبد خرید' ) : _e( 'View your shopping cart' ); ?>">
<span class="demo-icon icon-basket-1">  </span>
<?php // if ( ! empty( $title ) ) echo $args['before_title'] . $title . $args['after_title']; ?>
</a>
<a class="cart-contents" id="mini-cart-count" href="<?php echo wc_get_cart_url(); ?>" title="<?php is_rtl() ? _e( 'نمایش سبد خرید' ) : _e( 'View your shopping cart'); ?>" ><?php if(WC()->cart->get_cart_contents_count()>0){ echo sprintf( WC()->cart->get_cart_contents_count() );} ?> </a>
</div>
<!--header__card-icon-->
<h3 class="widget-title"> ســبــد خــریــد </h3>
<?php
echo do_shortcode('[woocommerce_cart]');
// after widget arguments are defined by themes
echo $args['after_widget'];
}
// Widget Backend
public function form( $instance ) {
if ( isset( $instance[ 'title' ] ) ) {
$title = $instance[ 'title' ];
}
else {
$title = __( '', 'wpb_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 wpb_widget ends here
// Register and load the widget
function wpb_load_widget() {
register_widget( 'wpb_widget' );
}
add_action( 'widgets_init', 'wpb_load_widget' );

does more widgets in main page causes bad loading on wordpress?

I'm using a specific widget that coded myself and use this widget on main page almost 18 times for displaying different category posts. does it cause bad loading in main page or not?! if I use normal coding instead of the widget is that better or not different?! You can check my website loading http://akhbartop.com/ In my system and some friends loading of the main page is not good. I want to know more widget in main page related to loading or not?! What do you suggest instead using a widget?!
this is my codes in widget
<?php
// Creating the widget
class wpb_box extends WP_Widget {
function __construct() {
parent::__construct(
// Base ID of your widget
'wpb_box',
// Widget name will appear in UI
__('ابزارک اختصاصی باکس مطالب خبرخوان', 'bigtheme'),
// Widget description
array( 'description' => __( 'ابزارک نمایش باکس مطالب از سایت های مختلف توسط آدرس خوراک یا فید سایت', 'bigtheme' ), )
);
add_action('save_post', array( $this, 'delete_query_caches') );
}
// Creating widget front-end
// This is where the action happens
/**
* Delete transients
*/
function delete_query_caches( $post_id ){
if( !isset( $_POST['post_type'] ) || $_POST['post_type'] !== 'post' ) return;
$categories = wp_get_post_terms( $post_id, 'category' );
if( $categories && ! is_WP_Error( $categories ) ) {
foreach( $categories as $cat ) {
delete_transient('post'.$cat->term_id.'_query');
}
}
}
public function widget( $args, $instance ) {
$name = apply_filters( 'widget_title', $instance['name'] );
$category = apply_filters( 'widget_title', $instance['category'] );
$id = apply_filters( 'widget_title', $instance['id'] );
$link = apply_filters( 'widget_title', $instance['link'] );
$display = apply_filters( 'widget_title', $instance['display'] );
$color = apply_filters( 'widget_title', $instance['color'] );
// This is where you run the code and display the output
?>
<div class="col-sm-6 col-xs-12 padding5">
<div class="article">
<div class="title">
<h3><?php echo $name ?></h3>
<div class="clear"></div>
<div class="line"></div>
</div>
<?php
if ( false === ( $slider = get_transient( 'post'.$id.'_query' ) ) ) {
$slider = new WP_Query(array(
'post_status' =>'publish',
'post_type' =>'post',
'cat' =>''.$id.'',
'posts_per_page' =>'9'
));
// Make a new query cache for 1 week
set_transient( 'post'.$id.'_query', $slider, 168 * HOUR_IN_SECONDS );
}
if( !$slider->have_posts() ) return; ?>
<ul>
<?php while( $slider->have_posts() ) : $slider->the_post(); ?>
<li>
<?php the_title(); ?>
<div style="display:<?php echo $display;?>" class="tooltiptext hidden-xs"><?php the_excerpt(); ?></div>
</li>
<?php endwhile; $slider->rewind_posts(); ?>
</ul>
<?php wp_reset_postdata(); ?>
<div class="list">مشاهده آرشیو کامل<img src="<?php bloginfo('template_url'); ?>/images/rss.png" width="18" height="18" alt="خوراک سایت" ></div>
</div>
</div>
<?php
echo $args['after_widget'];
}
public function form( $instance ) {
$name = ( isset( $instance[ 'name' ] ) ) ? $instance[ 'name' ] : '';
$category = ( isset( $instance[ 'category' ] ) ) ? $instance[ 'category' ] : '';
$link = ( isset( $instance[ 'link' ] ) ) ? $instance[ 'link' ] : '';
$color = ( isset( $instance[ 'color' ] ) ) ? $instance[ 'color' ] : '';
$id = ( isset( $instance[ 'id' ] ) ) ? $instance[ 'id' ] : '';
$display = ( isset( $instance[ 'display' ] ) ) ? $instance[ 'display' ] : '';
?>
<p>
<label for="<?php echo $this->get_field_id( 'color' ); ?>"><?php _e( 'رنگ باکس مطالب:' ); ?></label>
<input class="widefat" id="<?php echo $this->get_field_id( 'color' ); ?>" name="<?php echo $this->get_field_name( 'color' ); ?>" type="text" value="<?php echo esc_attr( $color ); ?>" placeholder="مثال : #CCC , #dd3333 , black , blue" />
</p>
<p>
<label for="<?php echo $this->get_field_id( 'name' ); ?>"><?php _e( 'عنوان باکس:' ); ?></label>
<input class="widefat" id="<?php echo $this->get_field_id( 'name' ); ?>" name="<?php echo $this->get_field_name( 'name' ); ?>" type="text" value="<?php echo esc_attr( $name ); ?>" />
</p>
<p>
<label for="<?php echo $this->get_field_id( 'id' ); ?>"><?php _e( 'آی دی دسته بندی' ); ?></label>
<input class="widefat" id="<?php echo $this->get_field_id( 'id' ); ?>" name="<?php echo $this->get_field_name( 'id' ); ?>" type="text" value="<?php echo esc_attr( $id ); ?>" />
</p>
<select id="<?php echo $this->get_field_id('category'); ?>" name="<?php echo $this->get_field_name('category'); ?>" class="widefat" style="width:100%;">
<?php foreach(get_terms('category','parent=0&hide_empty=0') as $term) { ?>
<option <?php selected( $instance['category'], $term->term_id ); ?> value="<?php echo $term->term_id; ?>"><?php echo $term->name; ?></option>
<?php } ?>
</select>
<p>
<label for="<?php echo $this->get_field_id( 'link' ); ?>"><?php _e( 'لینک آرشیو' ); ?></label>
<input class="widefat" id="<?php echo $this->get_field_id( 'link' ); ?>" name="<?php echo $this->get_field_name( 'link' ); ?>" type="text" value="<?php echo esc_attr( $link ); ?>" />
</p>
<p>
<label><?php _e( 'نمایش توضیحات مطالب' ); ?></label>
<select class="widefat" id="<?php echo $this->get_field_id( 'display' ); ?>" name="<?php echo $this->get_field_name( 'display' ); ?>">
<option <?php selected( $instance['display'], 'block'); ?> value="block">بله</option>
<option <?php selected( $instance['display'], 'none'); ?> value="none">خیر</option>
</select>
</p>
<?php
}
// Updating widget replacing old instances with new
public function update( $new_instance, $old_instance ) {
$instance = array();
$instance['name'] = ( ! empty( $new_instance['name'] ) ) ? strip_tags( $new_instance['name'] ) : '';
$instance['category'] = ( ! empty( $new_instance['category'] ) ) ? strip_tags( $new_instance['category'] ) : '';
$instance['link'] = ( ! empty( $new_instance['link'] ) ) ? strip_tags( $new_instance['link'] ) : '';
$instance['link2'] = ( ! empty( $new_instance['link2'] ) ) ? strip_tags( $new_instance['link2'] ) : '';
$instance['id'] = ( ! empty( $new_instance['id'] ) ) ? strip_tags( $new_instance['id'] ) : '';
$instance['link3'] = ( ! empty( $new_instance['link3'] ) ) ? strip_tags( $new_instance['link3'] ) : '';
$instance['link4'] = ( ! empty( $new_instance['link4'] ) ) ? strip_tags( $new_instance['link4'] ) : '';
$instance['link5'] = ( ! empty( $new_instance['link5'] ) ) ? strip_tags( $new_instance['link5'] ) : '';
$instance['color'] = ( ! empty( $new_instance['color'] ) ) ? strip_tags( $new_instance['color'] ) : '';
$instance['display'] = ( ! empty( $new_instance['display'] ) ) ? strip_tags( $new_instance['display'] ) : '';
$instance['source'] = ( ! empty( $new_instance['source'] ) ) ? strip_tags( $new_instance['source'] ) : '';
$instance['time'] = ( ! empty( $new_instance['time'] ) ) ? strip_tags( $new_instance['time'] ) : '';
return $instance;
}
} // Class wpb_box ends here
// Register and load the widget
function wpb_box() {
register_widget( 'wpb_box' );
}
add_action( 'widgets_init', 'wpb_box' );
?>
i did some changes and put transient api codes in widget but still has problem with widget updates!!!when posts publish widgets won't update!!
If you're using too many queries, caching the queries might be helpful.
Let's say your custom widget looks like this:
<?php
/**
* My widget class
*/
class dw_myWidget extends WP_Widget {
function __construct() {
// Instantiate the parent object
parent::__construct( false, __('My widget') );
}
function widget( $args, $instance ) {
$title = apply_filters( 'title', $instance['title'] );
$link1 = apply_filters( 'category', $instance['category'] ); // category id
$portfolio = new WP_Query(array(
'post_status' =>'publish',
'post_type' =>'post',
'cat' =>''.$link1.'',
'posts_per_page' =>'9'
));
// And the rest of the code
}
function update( $new_instance, $old_instance ) {
// update stuff
}
function form( $instance ) {
// widget form stuff
}
}
function dw_MyWidget_register() {
register_widget( 'dw_myWidget' );
}
add_action( 'widgets_init', 'dw_MyWidget_register' );
We cache the query using Wordpress's transient key, but since we don't know how many widgets will be out there we must include a unique thing in our transient API, like the $link1 variable which represents the category id:
function widget( $args, $instance ) {
$title = apply_filters( 'title', $instance['title'] );
$link1 = apply_filters( 'category', $instance['category'] ); // category id
if ( false === ( $portfolio = get_transient( 'post'.$link1.'_query' ) ) ) {
$portfolio = new WP_Query(array(
'post_status' =>'publish',
'post_type' =>'post',
'cat' =>''.$link1.'',
'posts_per_page' =>'9'
));
set_transient( 'post'.$link1.'_query', $portfolio, 168 * HOUR_IN_SECONDS );
}
// And the rest of the code
}
That's not all, we need to delete the transient when a new post is created, so we register a hook to save_post action, in the constructor
function __construct() {
// Instantiate the parent object
parent::__construct( false, __('My widget') );
add_action('save_post', array( $this, 'delete_query_caches') );
}
/**
* Delete transients
*/
function delete_query_caches( $post_id ){
if( !isset( $_POST['post_type'] ) || $_POST['post_type'] !== 'post' ) return;
$categories = wp_get_post_terms( $post_id, 'category' );
if( $categories && ! is_WP_Error( $categories ) ) {
foreach( $categories as $cat ) {
delete_transient('post'.$cat->term_id.'_query');
}
}
}
So the final code looks something like this:
<?php
/**
* My widget class
*/
class dw_myWidget extends WP_Widget {
function __construct() {
// Instantiate the parent object
parent::__construct( false, __('My widget') );
add_action('save_post', array( $this, 'delete_query_caches') );
}
/**
* Delete transients
*/
function delete_query_caches( $post_id ){
if( !isset( $_POST['post_type'] ) || $_POST['post_type'] !== 'post' ) return;
$categories = wp_get_post_terms( $post_id, 'category' );
if( $categories && ! is_WP_Error( $categories ) ) {
foreach( $categories as $cat ) {
delete_transient('post'.$cat->term_id.'_query');
}
}
}
function widget( $args, $instance ) {
$title = apply_filters( 'title', $instance['title'] );
$link1 = apply_filters( 'category', $instance['category'] ); // category id
if ( false === ( $portfolio = get_transient( 'post'.$link1.'_query' ) ) ) {
$portfolio = new WP_Query(array(
'post_status' =>'publish',
'post_type' =>'post',
'cat' =>''.$link1.'',
'posts_per_page' =>'9'
));
set_transient( 'post'.$link1.'_query', $portfolio, 168 * HOUR_IN_SECONDS );
}
// And the rest of the code
}
function update( $new_instance, $old_instance ) {
// update stuff
}
function form( $instance ) {
// widget form stuff
}
}
function dw_MyWidget_register() {
register_widget( 'dw_myWidget' );
}
add_action( 'widgets_init', 'dw_MyWidget_register' );
Hope it'll help.
From the code point of view, it does not matter where the code resides. Your code is executed in the same way whether it's a widget or a template part, or a plugin function hooked to some action.
What does matter is what the code is actually doing and how (and again, it does not matter where the code is).
In your case, I suppose, the widget is getting some RSS feeds, thus making http requests to external resources. These requests are slow, the results must be cached to avoid repeating the requests on every page load. You can have different expiration time (even 30 seconds difference) for each instance of the widget to try to prevent them all updating at the same time. Or even better, you should update them in the background, never making a front-end visitor to fetch those feeds.
Transients API is what you should use to make it fast.

How to create wordpress widget by using shortcode

I want to create a widget that will be created in wordpress when the shortcode is activated in a post. I tried to get some things to $all and then return it but didnt work out. I want to do it without using any plugins.
function movie($atts) {
class wpb_widget extends WP_Widget {
function __construct() {
parent::__construct(
// Base ID of your widget
'wpb_widget',
// Widget name will appear in UI
__('WPBeginner Widget', 'wpb_widget_domain'),
// Widget description
array( 'description' => __( 'Sample widget based on WPBeginner Tutorial', 'wpb_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
global $wpdb;
$b = shortcode_atts(array(
'name' => 'Deadpool',
), $atts);
$vypis = $wpdb->get_row ("SELECT * FROM hodnoceni WHERE nazev = '" . $atts['name'] . "'", ARRAY_A);
$all = $args['before_widget'];
if ( ! empty( $vypis['nazev'] ) )
// This is where you run the code and display the output
$all .= $args['before_title'] . $title . $args['after_title'] . '<p style="margin:0px;">' . $vypis['rok'] . '<br>' . $vypis['delka'] . ' min</p><div class="my-image"><img src="' . $vypis['src'] . '" height="130" alt="' . $vypis['nazev'] . '"> </div> <div class="my-content">' . $vypis['clanek'] . '</div> </div>';
$all .= $args['after_widget'];
}
// Widget Backend
public function form( $instance ) {
if ( isset( $instance[ 'title' ] ) ) {
$title = $instance[ 'title' ];
}
else {
$title = __( 'New title', 'wpb_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 wpb_widget ends here
return $all;
}
function wpb_load_widget() {
register_widget( 'wpb_widget' );
}
add_action( 'widgets_init', 'wpb_load_widget' );
function register_shortcodes(){
add_shortcode('bacon', 'bacon_function');
add_shortcode('movie_form', 'movie_form');
add_shortcode('movie', 'movie');
}
add_action( 'init', 'register_shortcodes');
For your widget class "wpb_widget" you can use this code:
<?php add_shortcode( 'show_movie', 'movie_shortcode' );
function movie_shortcode( $atts ) {
// Configure defaults and extract the attributes into variables
extract( shortcode_atts(
array(
'type' => 'wpb_widget'
),
$atts
));
$args = array( //optional markup:
'before_widget' => '<div class="box widget scheme-' . $scheme . ' ">',
'after_widget' => '</div>',
'before_title' => '<div class="widget-title">',
'after_title' => '</div>',
);
ob_start();
the_widget( $type, $atts, $args );
$output = ob_get_clean();
return $output;
}
and then use this code
[show_movie]
to run the widget.

Delete the list of categories in the single product page WooCommerce WordPress

I have made a function in functions.php which lists items of current categories in the category page. This is the PHP code:
<?php
class trueTopPostsWidget extends WP_Widget {
/*
* создание виджета
*/
function __construct() {
parent::__construct(
'true_top_widget',
'SideBarCategories', // заголовок виджета
array( 'description' => 'Desc' ) // описание
);
}
/*
* фронтэнд виджета
*/
public function widget( $args, $instance ) {
echo $args['before_widget'];
global $wp_query;
$cat_obj = $wp_query->get_queried_object();
if($cat_obj) {
$category_name = $cat_obj->name;
$category_ID = $cat_obj->term_id;
}
function woocommerce_subcats_from_parentcat_by_ID($parent_cat_ID) {
$params = array(
'hierarchical' => 1,
'show_option_none' => '',
'hide_empty' => $temp,
'parent' => $parent_cat_ID,
'taxonomy' => 'product_cat'
);
$subcats = get_categories($params);
$c = count($subcats);
if ($c>0):?>
<div style="margin-top:20%;">
<h2>Категории товаров</h2>
</div>
<?php endif; echo '<ul class="wooc_sclist">';
foreach ($subcats as $sc) {
$link = get_term_link( $sc->slug, $sc->taxonomy );
echo '<li>'.$sc->name.'</li>';
}
echo '</ul>';
}
woocommerce_subcats_from_parentcat_by_ID($category_ID);
echo $c;
echo $args['after_widget'];
}
/*
* бэкэнд виджета
*/
public function form( $instance ) {
if ( isset( $instance[ 'title' ] ) ) {
$title = $instance[ 'title' ];
}?>
<p>
<label for="<?php echo $this->get_field_id( '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
}
/*
* сохранение настроек виджета
*/
public function update( $new_instance, $old_instance ) {
$instance = array();
$instance['title'] = ( ! empty( $new_instance['title'] ) ) ? strip_tags( $new_instance['title'] ) : '';
return $instance;
}
}
/*
* регистрация виджета
*/
function true_top_posts_widget_load() {
register_widget( 'trueTopPostsWidget' );
}
add_action( 'widgets_init', 'true_top_posts_widget_load' );
?>
But I have faced with a problem. I don't want to list categories in the single product page. Now I can see all categories in this page. And it isn't satisfied to my conditions. How can I fix it?
I have solved so:
<div class="unit-30">
<div class="blok">
<?php /* Widgetized sidebar */
$actual_link = "http://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]";
$look_for = "tovar";
$pos = strpos($actual_link,$look_for);
if ($pos==null)
if ( !function_exists('dynamic_sidebar') || !dynamic_sidebar('Сайтбар woocommerce') ) : ?>
<?php endif; ?>
</div>
</div>

I'm developing a Wordpress Plugin and I need to create a Page with a form inside my plugin code

I am developing a Plugin.
I've developed the core of my plugin, I've also developed a Widget that I need to use with my plugin, but now I don't know where else to find information.
What I need to do, is this: in my Plugin's Widget I want to put a link "Register here!!!", and when the user clicks there take him to a page like http://mysite.com/my_plugin_register
And there display a form so the user can register.
Is there any action hook to do this? Where can I find any example?
To get your widget to work you will create a class for your widget that extends the WP_Widget class. Example is below:
class My_Widget extends WP_Widget
{
function My_Widget()
{
$this->WP_Widget( false, __( 'My Widget', 'my_widget_i18n') );
}
/* This is where the widget content is printed. */
function widget( $args, $instance )
{
extract( $args );
$title = empty( $instance[ 'title' ] ) ? ' ' : apply_filters( 'widget_title', $instance[ 'title'] );
echo $before_widget;
if( !empty( $title ) )
echo $before_title . $title . $after_title;
echo 'My Form';
echo $after_widget;
}
/* Use this to update any widget options */
function update( $new_instance, $old_instance)
{
$instance = $old_instance;
$instance[ 'title' ] = strip_tags( $new_instance[ 'title' ] );
return $instance;
}
/* Use this to create the form for the widget options */
function form( $instance )
{
$instance = wp_parse_args( (array) $instance, array( 'title' => '' ) );
$title = strip_tags( $instance[ 'title' ] );
echo '<p><label for="' . $this->get_field_id( 'title' ) . '">' . __( 'Title', 'events_calendar' ) . ': <input class="widefat" id="' . $this->get_field_id( 'title' ) . '" name="' . $this->get_field_name( 'title' ) . '" type="text" value="' . attribute_escape( $title ) . '" /></label></p>';
}
}
You can find more information on WP_Widget at the widgets API page
To create your page you should use a shortcode and put your form in the function for that.
/* This will write the form that you need to right */
function my_shortcode( $attrs )
{
?>
<form>
<input type="text" />
<input type="submit" />
</form>
<?php
}
add_shortcode( 'my_shortcode', 'my_shortcode' );
You will then in the Wordpress Dashboard create a page with the slug 'my_plugin_register' and add the following content to it
[my_shortcode]
Here is the shortcode API page for more information.

Resources