¿Cómo crear Widgets para WordPress?

wordpress

Vayamos por partes (como dijo Jack El Destripador :-))

¿Qué es un Widget?: Pequeña aplicación o programa, usualmente presentado en archivos o ficheros pequeños que son ejecutados por un motor de widgets o Widget Engine. Entre sus objetivos están los de dar fácil acceso a funciones frecuentemente usadas y proveer de información visual.

Wordpress: ¿Todavía alguien no sabe qué es Wordpress? Es un sistema de gestión de contenido enfocado a la creación de blogs (sitios web periódicamente actualizados).

Como todos sabéis, su desarrollo se basa principalmente en PHP y MySQL, vayamos pues a montar un widget.

Atención a este Widget para Wordpress:

/*
Plugin Name: Test Plugin
Plugin URI: http://www.widgetifyr.com
Description: Test Plugin
Author: widgetifyr.com
Version: 0.01
Author URI: http://www.widgetifyr.com

*/

// We're putting the plugin's functions inside the init function to ensure the
// required Sidebar Widget functions are available.

  function widget_test_init()
	  {
	  /* Your custom code starts here */
	  /* ---------------------------- */

	  /* Your Function */
	  function test()
	  {

		  /* Your Code ----------------- */ 

		  echo 'hello world';

		  /* End of Your Code ---------- */

	  }

	  /* -------------------------- */
	  /* Your custom code ends here */

	  function widget_test($args)
	  {

	  	  // Collect our widget's options, or define their defaults.
		  $options = get_option('widget_test');
		  $title = empty($options['title']) ? __('test') : $options['title'];

		  extract($args);
		  echo $before_widget;
		  echo $before_title;
		  echo $title;
		  echo $after_title;
		  test();
		  echo $after_widget;
	  }  

	  // This is the function that outputs the form to let users edit
	  // the widget's title. It's an optional feature, but were're doing
	  // it all for you so why not!

	  function widget_test_control()
	  {

		// Collect our widget options.
		$options = $newoptions = get_option('widget_test');

		// This is for handing the control form submission.
		if ( $_POST['widget_test-submit'] )
		{
			// Clean up control form submission options
			$newoptions['title'] = strip_tags(stripslashes($_POST['widget_test-title']));
		}

		// If original widget options do not match control form
		// submission options, update them.
		if ( $options != $newoptions )
		{
			$options = $newoptions;
			update_option('widget_test', $options);
		}

		$title = attribute_escape($options['title']);

		echo '
		<label for="test-title">';
		echo 'Title: <input id="widget_test-title" style="width: 250px;" name="widget_test-title" type="text" value="';
		echo $title;
		echo '" />';
		echo '
		';
		echo '<input id="widget_test-submit" name="widget_test-submit" type="hidden" value="1" />';
	  }

	// This registers the widget.
    register_sidebar_widget('test', 'widget_test');

	// This registers the (optional!) widget control form.
    register_widget_control('test', 'widget_test_control');

  }

  add_action('plugins_loaded', 'widget_test_init');

?>

Interesante, ¿verdad?

Ahora me vendrá alguien y dirá… ¡pero si eso sólamente hace un “Hello world”!

Cierto.

Hace unos días estuve ojeando http://widgetifyr.com, en esta web nos ayudan a generar Widgets a partir de código nuestro. No está nada mal, hice un par de cosillas y funcionaron bastante bien. Sin duda ahorras algo de tiempo :-)

Os animo a montar los vuestros, yo llevo unos días montando algunos (ahora los estoy testeando) y es bastante interesante.

Espero que os sirva la URL de referencia :-)

Leave a Reply

Desarrollo por Embat Consultors
 
Embat Consultors