Comment créer de nouveaux widgets wordpress pour mes thèmes
Comment créer de nouveaux widgets wordpress pour mes thèmes ?
Solution n°1 trouvée
C’est mon tutoriel préféré à ce sujet:
http://justintadlock.com/archives/2009/05/26/the-complete-guide-to-creating-widgets-in-wordpress-28
Je l’utilise comme base pour tous mes widgets.
Fondamentalement, le widget est un plugin, mais vous étendez la classe WP_Widget. C’est assez simple, il suffit de suivre ce tutoriel.
Cela est également utile http://codex.wordpress.org/Widgets_API
Bonne chance!
Solution n°2 trouvée
Si vous souhaitez créer un widget. Accédez à votre dossier de plugins et créez un fichier vierge nommé « sample.php » dans le dossier « Sample ».
Copiez le code ci-dessous et collez-le dans le fichier sample.php.
<?php
/*
Plugin Name: Widget
Description: Widget Description
*/
// Creating the widget
// Create a class named as "sample_widget" that is a child class of "WP_Widget".
class sample_widget extends WP_Widget
{
//Constructor of class
public function __construct()
{
parent::__construct(
// Id of our widget.
'sample_widget',
// This is the widget name that will be visible to user
__('Sample Widget'),
// Description of our widget
array(
'description' => __('Description of our widget.')
));
}
// Creating widget front-end
// This is where the action happens
// Creating function named as "widget", receiving two parameters.
public function widget($args, $instance)
{
/*Getting and assigning our widget title to wordpress hook "widget_title"
and passing its value to "$title" */
$title = apply_filters('widget_title', $instance['title']);
// Area, that is before the widget is diplayed
echo $args['before_widget'];
// Checking "$title" is empty or not
if (!empty($title)) /* If "$title" is not empty, below code will execute.
$args['before_title'] -> Displaying content before our widget title
$title -> Display our widget title
$args['after_title'] -> Displaying content after our widget title */ {
echo $args['before_title'] . $title . $args['after_title'];
}
// Displaying text of our widget
echo __('Hello, this is our widget text!');
// Displaying content after our widget
echo $args['after_widget'];
}
// This function naming our widget title
public function form($instance)
{
// If title is already set.
if (isset($instance['title'])) {
// $title is getting already assigned title
$title = $instance['title'];
}
// Otherwise our default title will be "Widget title"
else {
$title = __('Widget title');
}
?>
<!-- These are the settings and user interface that an admin will see -->
<p>
<!-- Already set title will be displayed at the top of our widget in admin panel -->
<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
}
// This function will replace old title with new title of our widget
public function update($new_instance, $old_instance)
{
$instance = array();
$instance['title'] = (!empty($new_instance['title'])) ? strip_tags($new_instance['title']) : '';
return $instance;
}
}
// Function to register and load our newly widget
function sample_widget_load()
{
// Registering our widget named as "sample_widget"
register_widget('sample_widget');
}
// Calling our newly created function named as "sample_widget_load" to register our widget
add_action('widgets_init', 'sample_widget_load');
?>
Solution n°3 trouvée
C’est plus une question pour google que stackoverflow. Vous trouverez des tonnes de tutoriels et d’exemples sur le Web.
- API widget sur le site wordpress : http://codex.wordpress.org/Widgets_API
- Exemple de tutoriel : http://justintadlock.com/archives/2009/05/26/the-complete-guide-to-creating-widgets-in-wordpress-28
- Un autre : http://www.lonewolfdesigns.co.uk/create-wordpress-widgets/
0 commentaire