1. Go to this page and download the library: Download underpin/widget-loader library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
underpin / widget-loader example snippets
// Register widget
underpin()->widgets()->add( 'hello-world-widget', [
'name' => underpin()->__( 'Hello World Widget' ), // Required. The name of the widget.
'id_base' => 'widget_name', // Required. The ID.
'description' => underpin()->__( 'Displays hello to a specified name on your site.' ), // Widget description.
'widget_options' => [ // Options to pass to widget. See wp_register_sidebar_widget
'classname' => 'test_widget',
],
'get_fields_callback' => function ( $fields, \WP_Widget $widget ) { // Fetch, and set settings fields.
$name = isset( $fields['name'] ) ? esc_html( $fields['name'] ) : 'world';
return [
new \Underpin\Factories\Settings_Fields\Text( $name, [
'name' => $widget->get_field_name( 'name' ), // See WP_Widget get_field_name
'id' => $widget->get_field_id( 'name' ), // See WP_Widget get_field_id
'setting_key' => 'name', // Must match field name and field ID
'description' => underpin()->__( 'Optional. Specify the person to say hello to. Default "world".' ),
'label' => underpin()->__( 'Name' ),
] ),
];
},
'display_callback' => function ( $instance, $fields ) { // Render output
$name = ! empty( $fields['name'] ) ? esc_html( $fields['name'] ) : 'world';
echo underpin()->__( sprintf( 'Hello, %s!', $name ) );
},
] );