Filter

Un Filter es un hook que intercepta un dato que WordPress va a usar o mostrar. Tu función lo recibe, lo modifica y debe devolverlo obligatoriamente. Si no devuelves el valor, desaparece.

¿Dónde se registra?

En functions.php o en el plugin, usando add_filter().

add_filter( 'nombre_del_hook', 'mi_funcion' );

function mi_funcion( $valor ) {
    // modifica $valor
    return $valor; // SIEMPRE hay que devolver
}

Ejemplo real: añadir texto al contenido

add_filter( 'the_content', 'añadir_nota_autor' );
function añadir_nota_autor( $content ) {
    if ( ! is_single() ) {
        return $content; // sin cambios si no es post individual
    }
    $nota = '<p class="nota">Artículo escrito por Alexei</p>';
    return $content . $nota;
}

Ejemplo real: cambiar la longitud del excerpt

add_filter( 'excerpt_length', 'mi_longitud_excerpt' );
function mi_longitud_excerpt( $length ) {
    return 25; // 25 palabras en lugar de 55
    // ↑ devolvemos el nuevo valor, no hacemos echo
}

Error más común

// ❌ INCORRECTO: se pierde el contenido
add_filter( 'the_content', 'mi_filter_malo' );
function mi_filter_malo( $content ) {
    echo $content . 'extra'; // echo en lugar de return
    // WordPress recibe null → página en blanco
}

// ✅ CORRECTO
function mi_filter_bien( $content ) {
    return $content . 'extra';
}