Tips & How-To

Category Archives — Tips & How-To

Code snippets, recommendations and tutorials for WordPress.

Quick Tip: How to Disable Page Templates in Child Themes

While developing a website with WordPress, you’ve probably been or will be in the following situation:

You created a Child Theme, inheriting a number of page templates from the parent theme. The thing is, maybe you don’t want some of those page templates. You don’t want your users to select it, or you’re not gonna support it, or it’s not fully compatible with the modifications you’ve made, or you just simply don’t like it. Whatever the reason is, you’d like to remove it from the dropdown in the page editor when creating a new page.

Before WordPress 3.9, there were some bizarre and pretty much complicated things to accomplish this. However, wlth the introduction of the theme_page_templates filter, it has become a really easy task to do.

Read more

Why Using Shortcodes Inside Templates Isn’t Always a Good Idea

The WordPress Shortcode API is going through a great deal of a refactoring process, which was necessary since a long time ago. Though still in its initial stages, one of the main goals is to provide more strict guidelines about the way shortcodes should be used, specially regarding what can and what cannot be passed as attributes, being HTML code the more complicated case. This is part of an ongoing discussion that began when WordPress 4.2.3 was launched, and lots of sites broke because they were using shortcodes in a way the update didn’t support anymore.

I’ll skip my point of view about the way the update was managed by the Core team, and I won’t dare to say that there’s a “wrong” way to use shortcodes, since I think that any provided tool should be used in any possible way that’s allowed by its internal logic. I’m just gonna stick to talk about a practice that can help to prevent some issues with the Shortcode API.

Read more

How to make public methods and functions private when used as hooks

If you worked for even just a little while with WordPress, then you must know by now that action and filter hooks are the cornerstone of its extensibility. Through the Plugin API, and using functions such as add_action(), do_action(), add_filter() and apply_filters() we can build almost any thing we can think of on top of the core, often in the form of plugins and themes, but there are still problems that can appear when we implement them.

Read more

How to Avoid Redeclaring Inline Styles for WordPress Customizer

My first publicly released theme, Follet (the one that powers this site right now), features my also first experiments working with the Customizer. This API offers a great way to see modifications to a theme in real time without actually having to apply those changes to the live site, and a large number of articles have been written about its benefits. What I learned while working on Follet was that sometimes is pretty difficult to manage some styling modifications for the customizable areas of the theme, and I particularly had a really nasty time with colours. However, I think I finally could solve that in an acceptably elegant way.

Read more

Buenas Prácticas en PHP: Funciones Privadas

Uno de los grandes problemas que podemos encontrarnos al trabajar con PHP es que, por carecer nativamente de una separación de ámbitos, podemos ejecutar casi cualquier bloque de código en casi cualquier lado. Eso puede ser una gran ventaja en muchos casos, pero también puede llevarnos a provocar, muchas veces inconscientemente, un desorden enorme en la organización de nuestros proyectos, tanto a nivel código como a nivel de archivos.

Teniendo en cuenta este problema, este post es un intento de repaso por algunas prácticas que, creo, deberíamos considerar al programar con PHP. Más concretamente, cuando trabajamos con funciones privadas o internas.

Read more

Mejorando tu Trabajo: Deploy Automatizado de WordPress con Capistrano

Nota: este artículo se publicó originalmente en inglés acá.

Si desarrollás sitios web con WordPress de manera profesional (básicamente, si vivís de eso o si una parte significativa de lo que ganás viene de ahí), y si todavía no implementaste un sistema de deployment automatizado, deberías considerar fuertemente hacerlo. Siendo sincero, no es lo más fácil del mundo. De hecho, muy probablemente te lleve unas cuantas horas de trabajo, de pruebas infructuosas y de lidiar con la comunicación entre servidores. Lo bueno es que podés aprovechar unos cuantos recursos variados en la experiencia de mucha gente que se quemó las pestañas poniendo en práctica un sistema de deployment antes que vos. Incluso existen herramientas especialmente pensadas para WordPress que te pueden resultar recontra útiles.

Read more

How to Deal with Users Being Automatically Generated by WP e-Commerce

In the last four months there was a lot of discussion about some decisions made by the developers of the famous shopping cart plugin WP e-Commerce. I think that discussion would not have exist if there were no users affected by the decisions that originated it. It is to be noted that version 3.8.14, the most recent release of WP e-Commerce, is supposed to definitely fix the issue, but I’m sure there will be a lot of people not putting the plugins on their sites up to date, and there can appear some possible issues with backward compatibility that need to be solved. That’s what this article is meant to.

You can read the first page of the thread if your interested in the technical implications, but basically, since version 3.8.13, WP e-Commerce saves the session data of anonymous customers into the wp_users and wp_usermeta tables, instead of doing it into transients, as was formerly done and other shopping cart solutions, such as WooCommerce, still do. This means that every time a non-registered visitor enters your site a new user will be created and some meta data for that user will be stored. So, if your site gets a lot of visitors, your database will be populated with lots of dummy users in a considerably short time. There is a scheduled task meant to remove this users hourly, but it doesn’t work quite well in some server configurations, so there will be sites where the tables won’t ever be automatically cleaned, with databases growing uncontrollably larger and larger in just a matter of days. That’s not something that any capable developer or site owner wants for his website.

So let me stop here for a minute. I’m not in the train of detractors of this practice. I don’t think it is ideal, but I do think there is a really good reason to do things this way and not another. I’m not that into discussing things that were already done as I am for trying to fix what’s wrong with them. There has been a lot of hate comments, ranting and whining in the thread I linked at the beginning, and I’m pretty sure that’s not the way to solve things, and don’t have me start on the kind of motivation that a programmer finds to get something fixed when he or his product is being attacked. So please, if you need some software to be fixed, be nice and friendly to your programmer. Otherwise, either he won’t do it well or will just walk away from you.

Now let’s get into the problem. This is not exactly a tutorial, so if you’re looking for a copy & paste kind of solution, you’re not gonna find it here. If you have this problem, the solution will depend on your specific server configuration, so you may need to do some modifications to the examples of code I’m gonna provide here.

Read more

How to Remove Yoast’s Google Analytics Warning in Network Admin

First of all, I admit I may be missing something here, but this issue annoys me big time, and I’m sure some people will feel the same.

So, what’s the deal? Well, I’ve been using Google Analytics for WordPress for quite a lot of time now in many installations, some of which are multisites. I love this plugin and all its functionality, and I wouldn’t change it for another one in most of the sites I run. But every time I download and install it in a multisite environment, I keep seeing a notice which reads “Google Analytics is not active. You must select which Analytics Profile to track before it can work”.

Some of you may say “why don’t you just set your Analytics Profile and stop whining?”. Well, I’d wish it was that easy. And that’s because, for some of this multisite installations, the main site is just an internal hub that will be hidden from the public, and doesn’t need to have an analytics profile set up.

Read more

How to Automatically Activate Jetpack Modules

If you, the like me, use Jetpack cheap jerseys for Inspiration a lot of sites (for Iniciando example, in a multisite or some complex installations), you probably need to have some modules always active for all your sites. It would be awesome cheap jerseys online if you could just have those modules active cheap jerseys without having to do it manually once you have activated the plugin, right? Well, you 2011 can do it.

Read more