WordPress

Widgetize

Recently noesh changed her template, just in case you hadn’t noticed. Thanks to WordPress’ flexibility in this regard, changing themes is pretty easy to do.

But along the way there were a couple of issues: The first one is, when changing, pretty much all of the custom code noesh manually added to the template was lost; the second one is the new template uses widgets. When the WordPress widgets are activated all the code that’s not part of the widget code – usually in the ‘sidebar’ file – is ignored.

As I figure it, there are a few ways to use the old code>

  • 1. Copy the XHTML code from the old template to the new template
  • 2. Look for updated versions of installed plugins and install those new versions, hoping they have widget functionality built into them
  • 3. Create new widgets from the existing plugins, modifying the plugin code to widgetize it.
  • 4. Use the old code in a widget-like way by way of an additional plugin or script

All of them have their benefits and their drawbacks. In my case I went for the last option, for it entailed the least time spent, because I don’t know much PHP to write plugins/widgets for wordpress and I’m not about to spend my energy learning how. Maybe some other time.

With this decided, I looked for a plugin that would let me do what I had in mind. After a while I found this plugin, which lets you use any PHP file in the template directory and add its contents as a widget in the configuration page.

Second step was to create a PHP file in which I put a fragment of code. In noesh’s case, it’s a call for the AJAX shoutbox which was originally on the sidebar code file:
< ?php jal_get_shoutbox(); ?>

In ruidoz’s case, it’s a call to the phrases script.

For all this to work, the file must begin with ‘widget_’ or ‘widget-‘ as the filename. Upload it to the server and save it on the active template directory. If it’s put somewhere it just won’t work. After that the necessary amount of theme widgets have to be activated on the widget configuration page. Read the plugin instructions for more details.

With these steps all those plugins that had to be called from the template sidebar file can be added as widgets without having to muck around the code itself. Helps a lot with themes that had the widget coded added to them and with plugins that don’t have widget functionality built into them yet.

This is great for PHP… but what if you had a bit of XHTML code? Not to worry; all that needs to be done is to add it as a text widget. This is useful for fragments of code like a flickr badge. For this the theme widgets plugin doesn’t have to be installed, since the function is included with the widgets plugin itself. For example, noeshtiosita’s badge now looks like this:
HTML Fragment.

The total time spent? Just about an hour and a half, which – for the effort – is nice. Another good thing is it lets you change theme quicker and easier since no coded needs to be added or removed from the template code. Just move the ‘widget_’ or ‘widget-‘ files to the new template’s directory and reactivate them at the widget configuration page.

Now, keep in mind this is pretty much a temporal solution until most plugins have widget funcions implemented. Until then, this is something to do to avoid code headaches.

widgetizar

Recientemente noesh cambio de template, por si no se habian dado cuenta. Gracias a la flexibilidad de wordpress, cambiar de template es bastante facil.

Pero aqui hubo un problemita que consta de dos partes: La primera es, que al cambiar, se perdio mucho del codigo que noesh habia agregado manualmente y la segunda esque el nuevo template usa widgets. Cuando se estan usando los widgets de wordpress todo el codigo que esta puesto manualmente – por lo regular en el archivo ‘sidebar’ del template – es ignorado.

Para poder usar todo ese codigo existente hay varias formas de poderlo hacer:

  1. 1. Copiar el codigo XHTML del template viejo al nuevo.
  2. 2. Buscar versiones nuevas de los plugins e instalarlas para agregar esas funciones ya como un widget.
  3. 3. Crear widgets nuevos en base a los plugins existentes, modificando el codigo del plugin para “widgetizarlo
  4. 4. Usar el codigo viejo a manera de widget usando algun plugin/script adicional.

Todas tienen sus beneficios y sus contras. En mi caso me fui por la ultima opcion para terminar rapido, por que no se gran cosa de PHP ni como hacer plugins/widgets para wordpress y ahorita tampoco estoy como para ponerme a aprender a hacerlo.

Ya con todo esto decidido, me puse a buscar algun plugin que me permitiera hacer lo que tenia en mente. Despues de buscar un rato, di coneste plugin, que agarra cualquier archivo PHP en el directorio del template y permite agregar funciones adicionales como un widget en la pantalla de configuracion.

Segundo paso fue hacer un archivo PHP en el cual puse un fragmento de codigo. En el caso de noesh, es una llamada para el AJAX shoutbox que originalmente estaba en el codigo del template:
< ?php jal_get_shoutbox(); ?>

En el caso de ruidoz, es una llamada para el script de frases.

Para que esto funcione, se tiene que crear un archivo que comience con ‘widget_’ o ‘widget-‘. Hay que subirlo al servidor y guardarlo en el directorio del template que esta activo; si se mete en cualquier otro directorio no va a funcionar. Despues hay que activar la cantidad necesaria de ‘theme widgets’ en la pantalla de configuracion de widgets. Leer las instrucciones del plugin para mas detalles.

Con esos pasos todos esos plugins que habia que llamar manualmente desde el codigo del template pueden quedar como theme widgets sin estar jugando con codigo. Ayuda mucho con templates a los que el codigo para widgets les fue puesto despues de su creacion y para plugins que aun no son actualizados con funciones de widget.

Esto es util para PHP… pero que hay que hacer cuando se tiene un fragmento de XHTML? En lugar de agegar el fragmento al codigo en si nada mas hay que ponerlo en un widget de texto; bastante practico para cosas que solo son un fragmento de codigo como los flickr badges. Para cosas asi no hace falta el plugin de theme widgets, la funcion ya esta incluida con el plugin de widgets. Por ejemplo, el codigo del badge de noesh quedo asi:
HTML Fragment.

El total de tiempo que inverti en esta solucion? Mas o menos hora y media, lo cual – para el esfuerzo – es bastante bueno. Ademas es una opcion que permite cambiar de template con mas facilidad, puesto que ya no hay que agregar y eliminar codigo del template manualmente. Lo unico que se tendria que hacer es mover los archivos ‘widget_’ o ‘widget-‘ al template nuevo y reactivarlos en la pantalla de configuracion.

Esto es mas que nada una solucion temporal, pero en lo que todos los plugins que agregan contenido a la pagina final sean actualizados, es algo para evitar dolores de cabeza.