2017 © Pedro Peláez
 

library widgets-helper

A class to ease creating powered Widgets on WordPress

image

wpbp/widgets-helper

A class to ease creating powered Widgets on WordPress

  • Tuesday, March 6, 2018
  • by Mte90
  • Repository
  • 1 Watchers
  • 9 Stars
  • 418 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 21 Forks
  • 1 Open issues
  • 6 Versions
  • 12 % Grown

The README.md

Widgets Helper Class

License Downloads, (*1)

A class that extends the built-in WP_Widget class to provide an easier/faster way to create Widgets for WordPress.
This is a fork of the original version with updates from the pull requests on the official repo and few little improvements., (*2)

Features

  • Automatic fields creation
  • Validation methods
  • Filter methods
  • Before/After form output methods
  • Custom form fields creation

Install

composer require wpbp/widgets-helper:dev-master, (*3)

composer-php52 supported., (*4)

Example

class MV_My_Recent_Posts_Widget extends WPH_Widget {
    function __construct() {
            // Widget Backend information
            $args = array(
                'label' => __( 'My Recent Posts', 'mv-my-recente-posts' ),
                'description' => __( 'My Recent Posts Widget Description', 'mv-my-recente-posts' ),
                'slug' => 'my_recent_posts',
                'options' => array() //classname, description
            );

            $args['fields'] = array(

                // Title field
                array(
                // field name/label
                'name' => __( 'Title', 'mv-my-recente-posts' ),
                // field description
                'desc' => __( 'Enter the widget title.', 'mv-my-recente-posts' ),
                // field id
                'id' => 'title',
                // field type ( text, checkbox, textarea, select, select-group )
                'type'=>'text',
                // class, rows, cols
                'class' => 'widefat',
                // default value
                'std' => __( 'Recent Posts', 'mv-my-recente-posts' ),
                /* Set the field validation type
                    'alpha_dash' Returns FALSE if the value contains anything other than alpha-numeric characters, underscores or dashes
                    'alpha' Returns FALSE if the value contains anything other than alphabetical characters
                    'alpha_numeric' Returns FALSE if the value contains anything other than alpha-numeric characters
                    'numeric' Returns FALSE if the value contains anything other than numeric characters
                    'boolean' Returns FALSE if the value contains anything other than a boolean value ( true or false )

                   You can define custom validation methods. Make sure to return a boolean ( TRUE/FALSE )
                    'validate' => 'my_custom_validation',
                   Will call for: $this->my_custom_validation( $value_to_validate );
                */
                'validate' => 'alpha_dash',
                /* Filter data before entering the DB
                     strip_tags ( default )
                     wp_strip_all_tags
                     esc_attr
                     esc_url
                     esc_textarea
                */
                'filter' => 'strip_tags|esc_attr'
                 ),
                // Amount Field
                array(
                'name' => __( 'Amount' ),
                'desc' => __( 'Select how many posts to show.', 'mv-my-recente-posts' ),
                'id' => 'amount',
                'type'=>'select',
                // selectbox fields
                'fields' => array(
                        array(
                            // option name
                            'name'  => __( '1 Post', 'mv-my-recente-posts' ),
                            // option value 
                            'value' => '1'
                        ),
                        array(
                            'name'  => __( '2 Posts', 'mv-my-recente-posts' ),
                            'value' => '2'
                        ),
                        array(
                            'name'  => __( '3 Posts', 'mv-my-recente-posts' ),
                            'value' => '3'
                        )
                 ),
                'validate' => 'my_custom_validation',
                'filter' => 'strip_tags|esc_attr',
                 ),
                // Output type checkbox
                array(
                'name' => __( 'Output as list', 'mv-my-recente-posts' ),
                'desc' => __( 'Wraps posts with the <li> tag.', 'mv-my-recente-posts' ),
                'id' => 'list',
                'type'=>'checkbox',
                // checked by default:
                'std' => 1, // 0 or 1
                'filter' => 'strip_tags|esc_attr',
                 ),
                // Taxonomy Field
                array(                          
                'name' => __( 'Taxonomy', 'mv-my-recente-posts' ),
                'desc' => __( 'Set the taxonomy.', 'mv-my-recente-posts' ),
                'id' => 'taxonomy',
                'type' => 'taxonomy',
                'class' => 'widefat',
                ),
                // Taxonomy Field
                array(
                'name' => __( 'Taxonomy terms', $this->plugin_slug ),
                'desc' => __( 'Set the taxonomy terms.', $this->plugin_slug ),
                'id' => 'taxonomyterm',
                'type' => 'taxonomyterm',
                'taxonomy' => 'category',
                'class' => 'widefat',
                ),
                // Pages Field
                array(
                'name' => __( 'Pages', $this->plugin_slug ),
                'desc' => __( 'Set the page.', $this->plugin_slug ),
                'id' => 'pages',
                'type' => 'pages',
                'class' => 'widefat',
                ),
                // Post type Field
                array(
                'name' => __( 'Post type', $this->plugin_slug ),
                'desc' => __( 'Set the post type.', $this->plugin_slug ),
                'id' => 'posttype',
                'type' => 'posttype',
                'posttype' => 'post',
                'class' => 'widefat',
                ),
             );

            $this->create_widget( $args );
        }

        /**
        * Custom validation for this widget 
        * 
        * @param string $value
        * @return boolean 
        */
        function my_custom_validation( $value ) {
            if ( strlen( $value ) > 1 )
                return false;
            else
                return true;
        }

        /**
         * Output function
         * 
         * @param array $args
         * @param array $instance
         */
        function widget( $args, $instance ) {
            $out = $args[ 'before_widget' ];
            // And here do whatever you want
            $out  = $args['before_title'];
            $out .= $instance['title'];
            $out .= $args['after_title'];
            // here you would get the most recent posts based on the selected amount: $instance['amount']
            // Then return those posts on the $out variable ready for the output
            $out .= '<p>Hey There! </p>';
            $out .= $args[ 'after_widget' ];
            echo $out;
        }

    }

    // Register widget
    if ( ! function_exists( 'mv_my_register_widget' ) ) {
        function mv_my_register_widget() {
            register_widget( 'MV_My_Recent_Posts_Widget' );
        }
        add_action( 'widgets_init', 'mv_my_register_widget', 1 );
    }

Credits

by @sksmatt
www.mattvarone.com, (*5)

Contributors:, (*6)

Joachim Kudish ( @jkudish )
Joaquin http://bit.ly/p18bOk
markyoungdev http://bit.ly/GK6PwU
riesurya https://github.com/sksmatt/WordPress-Widgets-Helper-Class/pull/7
ghost https://github.com/sksmatt/WordPress-Widgets-Helper-Class/pull/5
Mte90, (*7)

The Versions

06/03 2018

dev-master

9999999-dev https://github.com/WPBP/Widgets-Helper

A class to ease creating powered Widgets on WordPress

  Sources   Download

GPL-3.0

wordpress widget widgets

06/03 2018

1.0.4

1.0.4.0 https://github.com/WPBP/Widgets-Helper

A class to ease creating powered Widgets on WordPress

  Sources   Download

GPL-3.0

wordpress widget widgets

18/10 2017

1.0.3

1.0.3.0 https://github.com/WPBP/Widgets-Helper

A class to ease creating powered Widgets on WordPress

  Sources   Download

GPL-3.0

wordpress widget widgets

21/09 2017

1.0.2

1.0.2.0 https://github.com/WPBP/Widgets-Helper

A class to ease creating powered Widgets on WordPress

  Sources   Download

GPL-3.0

wordpress widget widgets

31/03 2017

1.0.1

1.0.1.0 https://github.com/WPBP/Widgets-Helper

A class to ease creating powered Widgets on WordPress

  Sources   Download

GPL-3.0

wordpress widget widgets

22/06 2016

1.0.0

1.0.0.0 https://github.com/WPBP/Widgets-Helper

A class to ease creating powered Widgets on WordPress

  Sources   Download

GPL-3.0

wordpress widget widgets