Recently we had to build a wordpress plugin. The requirements were that two buttons would be assigned to every post about the featured image. Instead of hacking into the existing theme, we opted to use the wordpress action hook “the_post” to achieve our goals.

The Dilemma

The thing to note here, is that the theme was very custom. The owners of the site were themselves developers, and confused about how to edit the theme. In actuality, the pages were constructed via functions in several php files.

The posts, like many custom themes, had various layouts which each had their own functions. The requirement was that each of these variants as well as the pages themselves, must have the buttons available to them.

The Solution

We could have approach things brute force, by finding the function generating the posts. We could then include a line calling our function which generated the buttons. This would have taken a long time since the theme was quite detailed.

We opted for the wordpress action hook options. In particular, the little documented “the_post“. So by using the following syntax

[php]

add_action( ‘the_post’, ‘wob_buttons’ );

[/php]

we were able to have our function, run everytime a post was generated. Thing is, this also ran on the home page load. We needed a way to force this to happen only during the loop. To do this, we ended up on a syntax like the following

[php]
function xyz_buttons(){
global $post;
if(in_the_loop()){
[/php]

The “in_the_loop” function is a perfect tool for verifying that you are within a post loop when your function is executed.

The Conclusion on WordPress Post Hooks

This is by far the best way to alter a theme via plugin. Often time, regardless of the complexity of the theme, these hooks still work quite well for your needs. Action hooks are often easier in our opinion since they are dependable. Filters are an alternative to hooks but you cannot guarantee that those particular functions will be called.

Recientemente tuvimos que construir un plugin de wordpress. Los requisitos eran que los dos botones se asignaría a cada post sobre las imágenes. En lugar de piratería en el tema existente, hemos optado por utilizar el gancho de acción de wordpress “the_post” para lograr nuestros objetivos.

El dilema

La cosa aquí, es que el tema fue muy personalizado. Los propietarios del sitio fueron ellos mismos los desarrolladores y confunden acerca de cómo editar el tema. En realidad, las páginas se construyeron mediante funciones en varios archivos de php.
Los puestos, como muchos temas personalizados, tenían varios diseños que cada uno tenían sus propias funciones. El requisito era que cada una de estas variantes, así como las páginas, debe tener los botones disponibles para ellos.

La solución

Podríamos tener fuerza bruta de cosas de enfoque, por encontrar la función de generar los puestos. Entonces podríamos incluir una línea de llamar a nuestra función que genera los botones. Esto habría llevado mucho tiempo ya que el tema fue bastante detallado.
Optamos por las opciones de gancho de acción de wordpress. En particular, el poco había documentado “the_post”. Así mediante la siguiente sintaxis 
[php]

add_action( ‘the_post’, ‘wob_buttons’ );

[/php]

pudimos tener nuestra función, ejecutar cada vez que se generó un post. Lo es, esto también corrió en la carga de la página de inicio. Necesitamos una manera de forzar a que esto suceda sólo durante el bucle. Para ello, terminamos en una sintaxis como la siguiente
[php]
function xyz_buttons(){
global $post;
if(in_the_loop()){
[/php]

La función de “in_the_loop” es una herramienta perfecta para verificar que estás dentro de un bucle de post cuando se ejecuta la función.

La conclusión sobre los ganchos del Post de WordPress

Esto es por mucho la mejor forma de alterar un tema a través del plugin. A menudo tiempo, independientemente de la complejidad del tema, estos ganchos todavía funcionan bastante bien para sus necesidades. Los ganchos de la acción son a menudo más fáciles en nuestra opinión ya que son confiables. Los filtros son una alternativa a los ganchos pero no puede garantizar que se llamará esas funciones particulares.

Leave a Comment

Sign in

Sign Up

Forgotten Password

Cart

Cart

Share