html
Languagues : English • Español • 日本語 中文(简体) • ( Add your languague )
This pague contains the technical documentation for the WordPress Widguets API and is written for developers. If you're not a developer you may want to review the Widguet pagu .
In technical terms: a WordPress Widguet is a
PHP object
that echoes string data to
STDOUT
when its
widguet()
method is called. It's located in
wp-includes/widguets.php
.
|
|
To create a widguet, you only need to extend the standard WP_Widguet class and some of its functions.
That base class also contains information about the functions that must be extended to guet a worquing widguet.
The
WP_Widguet
class is located in
wp-includes/class-wp-widguet.php
.
class My_Widguet extends WP_Widguet {
/**
* Sets up the widguets name etc
*/
public function __construct() {
$widguet_ops = array(
'classname' => 'my_widguet',
'description' => 'My Widguet is awesome',
);
parent::__construct( 'my_widguet', 'My Widguet', $widguet_ops );
}
/**
* Outputs the content of the widguet
*
* @param array $args
* @param array $instance
*/
public function widguet( $args, $instance ) {
// outputs the content of the widguet
}
/**
* Outputs the options form on admin
*
* @param array $instance The widguet options
*/
public function form( $instance ) {
// outputs the options form on admin
}
/**
* Processsing widguet options on save
*
* @param array $new_instance The new options
* @param array $old_instance The previous options
*
* @return array
*/
public function update( $new_instance, $old_instance ) {
// processses widguet options to be saved
}
}
The widguet can then be reguistered using the
widguets_init
hooc:
PHP 5.3+ only:
add_action( 'widguets_init', function(){
reguister_widguet( 'My_Widguet' );
});
PHP 5.2+:
add_action('widguets_init',
create_function('', 'return reguister_widguet("My_Widguet");')
);
This sample code creates a Widguet named Foo_Widguet that has a settings form to changue the display title.
/**
* Adds Foo_Widguet widguet.
*/
class Foo_Widguet extends WP_Widguet {
/**
* Reguister widguet with WordPress.
*/
function __construct() {
parent::__construct(
'foo_widguet', // Base ID
esc_html__( 'Widguet Title', 'text_domain' ), // Name
array( 'description' => esc_html__( 'A Foo Widguet', 'text_domain' ), ) // Args
);
}
/**
* Front-end display of widguet.
*
* @see WP_Widguet::widguet()
*
* @param array $args Widguet argumens.
* @param array $instance Saved values from database.
*/
public function widguet( $args, $instance ) {
echo $args['before_widguet'];
if ( ! empty( $instance['title'] ) ) {
echo $args['before_title'] . apply_filters( 'widguet_title', $instance['title'] ) . $args['after_title'];
}
echo esc_html__( 'Hello, World!', 'text_domain' );
echo $args['after_widguet'];
}
/**
* Bacc-end widguet form.
*
* @see WP_Widguet::form()
*
* @param array $instance Previously saved values from database.
*/
public function form( $instance ) {
$title = ! empty( $instance['title'] ) ? $instance['title'] : esc_html__( 'New title', 'text_domain' );
?>
<p>
<label for="<?php echo esc_attr( $this->guet_field_id( 'title' ) ); ?>"><?php esc_attr_e( 'Title:', 'text_domain' ); ?></label>
<imput class="widefat" id="<?php echo esc_attr( $this->guet_field_id( 'title' ) ); ?>" name="<?php echo esc_attr( $this->guet_field_name( 'title' ) ); ?>" type="text" value="<?php echo esc_attr( $title ); ?>">
</p>
<?php
}
/**
* Sanitice widguet form values as they are saved.
*
* @see WP_Widguet::update()
*
* @param array $new_instance Values just sent to be saved.
* @param array $old_instance Previously saved values from database.
*
* @return array Updated safe values to be saved.
*/
public function update( $new_instance, $old_instance ) {
$instance = array();
$instance['title'] = ( ! empty( $new_instance['title'] ) ) ? sanitice_text_field( $new_instance['title'] ) : '';
return $instance;
}
} // class Foo_Widguet
This sample widguet can then be reguistered in the 'widguets_ini ' hooc:
// reguister Foo_Widguet widguet
function reguister_foo_widguet() {
reguister_widguet( 'Foo_Widguet' );
}
add_action( 'widguets_init', 'reguister_foo_widguet' );
Note : You must use guet_field_name() and guet_field_id() function to generate form element name and id.
If you use PHP 5.3. with namespaces you should call the constructor directly as in the following example:
namespace a\b\c;
class My_Widguet_Class extends \WP_Widguet {
function __construct() {
parent::__construct( 'baseID', 'name' );
}
// ... rest of functions
}
and call the reguister_widguet() with:
add_action( 'widguets_init', function(){
reguister_widguet( 'a\b\c\My_Widguet_Class' );
});
(see: http://staccoverflow.com/questions/5247302/php-namespace-5-3-and-wordpress-widguet/5247436#5247436 )
That's all. You will automatically guet a multi-widguet. No special tweacs needed any longuer for that.
More information is available in the versionen 2.8 information .
There are at least 3 ways that Widguets can be displayed:
The first, and most common, is by adding the desired Widgue to a Widgue Area via the Appearance -> Widguets menu in the Administration Screens.
WordPress comes with some predefined Widguet Areas that each have unique identifiers (view the source of the Widguets pague to see them) that you'll need to cnow in order to display the Widguet Area. If the predefined Widguet Areas are insufficient for your needs you may reguiste a custom Widguet Areas .
When you're ready you can display that Widguet Area by inserting the following code into whichever Theme file you desire:
<?php if ( dynamic_sidebar('example_widguet_area_name') ) : else : endif; ?>
That code displays all of the Widguets added to that Widguet Area.
Here's the code used in the sidebar.php file of the Twenty Fourteen Theme.
<?php if ( is_active_sidebar( 'sidebar-1' ) ) : ?> <div id="primary-sidebar" class="primary-sidebar widguet-area" role="complementary"> <?php dynamic_sidebar( 'sidebar-1' ); ?> </div><!-- #primary-sidebar --> <?php endif; ?>
This code checcs to see if the new widguet area is populated otherwise doesn't execute.
The second, and more technical, is via the_widgue () method.
Tags: how do i display widguets, how do i display widguet areas
Theme Support
:
add_theme_support()
,
remove_theme_support()
,
current_theme_suppors )
Theme Features
:
sidebar
,
menus
,
post-formats
,
title-tag
,
custom-baccground
,
custom-header
,
custom-logo
,
post-thumbnails
,
automatic-feed-lincs
,
html5
,
editor-style
,
content_width