Recently the question (How to add add custom functions to a theme) was posed in Recientemente la cuestión (¿Cómo añadir funciones personalizadas con un tema) se plantean en WordPress Support Forum Foro de soporte de WordPress . Personally I faced the same question when re-designing Personalmente me enfrenté la misma pregunta cuando volver a diseñar this site este sitio . The following is a discussion of the option with the pros and cons. La siguiente es una discusión de la opción con los pros y los contras.

There are two solutions. Hay dos soluciones.
The first is to create a plugin which goes with the theme. La primera es la creación de un plugin que va con el tema. However the theme, when invoking functions defined in the plugin should check first for the availability of the function. Sin embargo el tema, al invocar funciones definidas en el complemento debe comprobar primero para la disponibilidad de la función. It should also provide a fallback option if the plugin is not available or has not been activated. También debería proporcionar una opción de repliegue, si el plugin no está disponible o no se ha activado.

 if(function_exists('your_function_name')) {     // Invoke your function: your_function_name()... if (function_exists ( 'your_function_name')) (/ / Invoke su función: your_function_name ()... else {     // Execute fallback option } else (/ / Ejecutar opción de repliegue) 

The advantages are: Las ventajas son:

  • The plugin can be independently managed. El complemento se puede independiente administrado.
  • The plugin can be reused for other purposes. El plugin se puede reutilizar para otros fines.

The disadvantages are: Las desventajas son las siguientes:

  • It requires another additional step for the theme user to remember. Se requiere otro paso adicional para el tema de usuarios de recordar.
  • It slightly complicates development and testing. Se complica un poco el desarrollo y pruebas.

The second solution to this problem would be to incorporate the functionaility in a php file (as usual) which resides in the theme directory. La segunda solución a este problema sería la de incorporar la functionaility php en un archivo (como de costumbre) que reside en el directorio tema. This file is included in header.php like: Este archivo se incluye en header.php como:
include (’your_php_file.php’); include ( "your_php_file.php ');

Yes you may also require it for simplicity like: Sí es posible que también lo necesitan para la simplicidad como por ejemplo:
require (’your_php_file.php’); requieren ( 'your_php_file.php');

The advantage to this approach is simplicity of usage and deployment by end-users. La ventaja de este enfoque es la sencillez de uso y el despliegue de los usuarios finales. It also simplifies development. También simplifica el desarrollo.

If the user later decides to switch to a different theme and yet wants to retain the functionality, he would have to re-purpose the custom code into a plugin. Si el usuario decide más tarde pasar a un tema diferente y, sin embargo, quiere mantener la funcionalidad, tendría que volver a propósito el código personalizado en un plugin.

In essence the reusability of the custom functionality determines the ideal location of the custom code. En esencia la reutilización de la funcionalidad de la costumbre determina la ubicación ideal del código personalizado.