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.

Read More

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?

Related posts

Leave a Reply

1 comment

  1. 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 '<a href="http://mysite.com/my_plugin_register">My Form</a>';
    
        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.