Wordpress add_filter inside function not works

admin

Administrator
Staff member
I will try to explain my case.

I would like to change the <strong>wpseo_title, wpseo_metakey, wpseo_metadesc, wpseo_opengraph_type, wpseo_opengraph_image, wpseo_title,</strong> and <strong>wpseo_opengraph_image_size</strong> on the "archive-page" of "custom post"

Because yoast plugin not translated meta tags inside "archive" post. And i need to translate all

I have this code:

Code:
function get_archive_seo() {

include 'seo_archive.php';

$tipoSel = get_post_type(get_the_ID());

if(is_post_type_archive( $tipoSel )){

    foreach ($traduccionesArchive as $keyTipo =&gt; $tipo){
        foreach ($tipo as $keyMeta =&gt; $palabra){
            if($keyMeta == 'opengraph_type' || $keyMeta == 'opengraph_image_size') continue;
            icl_register_string ('my-theme', $keyTipo." - ".$keyMeta, $palabra);
        }
    }

    foreach ($traduccionesArchive[$tipoSel] as $key =&gt; $palabra){
        add_filter( 'wpseo_'.$key, function($nombre) use ( $palabra, $tipoSel, $key ) {
            if($key == 'opengraph_type' || $key == 'opengraph_image_size') return $palabra;
            return icl_t('my-theme', $tipoSel." - ".$key, $palabra);
        });
    }

}

}

add_filter( 'wp_head', 'get_archive_seo');

And this is the <strong>"seo_archive.php"</strong>:

Code:
$traduccionesArchive['hoteles']['title'] = 'Hotel, Vacaciones en Andalucia';
$traduccionesArchive['hoteles']['metakey'] = 'palabra1, palabra2, palabra3';
$traduccionesArchive['hoteles']['metadesc'] = 'Mapa de Hotel en Andalucia, Costa del Sol, Costa de la Luz y Sierra de Grazalema. El lugar ideal para tus vacaciones.';
$traduccionesArchive['hoteles']['opengraph_type'] = 'object';
$traduccionesArchive['hoteles']['opengraph_image'] = 'http://www.hotel.com/img/logo.png';
$traduccionesArchive['hoteles']['opengraph_image_size'] = 100;

$traduccionesArchive['apartamentos']['title'] = 'Apartamentos de vacaciones Costa del Sol ';
$traduccionesArchive['apartamentos']['metakey'] = 'palabra1, palabra2, palabra3';
$traduccionesArchive['apartamentos']['metadesc'] = 'Mapa de localizacion de los Apartamentos Vacacionales de Hotel. Disfruta de tus vacaciones en familia en la Costa del Sol.';
$traduccionesArchive['apartamentos']['opengraph_type'] = 'object';
$traduccionesArchive['apartamentos']['opengraph_image'] = 'http://www.hotel.com/img/logo.png';
$traduccionesArchive['apartamentos']['opengraph_image_size'] = 100;

$traduccionesArchive['destinos']['title'] = 'Mapa de localizacion de Hotel, Andalucia';
$traduccionesArchive['destinos']['metakey'] = 'palabra1, palabra2, palabra3';
$traduccionesArchive['destinos']['metadesc'] = 'Mapa de Hoteles en Andalucia, en los destinos vacacionales de Costa del Sol, Costa de la Luz y Sierra de Grazalema Andalucia.';
$traduccionesArchive['destinos']['opengraph_type'] = 'object';
$traduccionesArchive['destinos']['opengraph_image'] = 'http://www.hotel.com/img/logo.png';
$traduccionesArchive['destinos']['opengraph_image_size'] = 100;

$traduccionesArchive['ofertas']['title'] = 'Ofertas Hotel Costa del Sol, Costa de la Luz ';
$traduccionesArchive['ofertas']['metakey'] = 'palabra1, palabra2, palabra3';
$traduccionesArchive['ofertas']['metadesc'] = 'Ahorra en tus vacaciones con las ofertas en nuestros hoteles de la Costa del Sol, Costa de la Luz y Andalucia.';
$traduccionesArchive['ofertas']['opengraph_type'] = 'object';
$traduccionesArchive['ofertas']['opengraph_image'] = 'http://www.hotel.com/img/logo.png';
$traduccionesArchive['ofertas']['opengraph_image_size'] = 100;

But now, it not works, but in the past works fine, the filter is form module yoast SEO <a href="https://yoast.com/wordpress/plugins/seo/" rel="nofollow noreferrer">https://yoast.com/wordpress/plugins/seo/</a>

Y tried with this <a href="https://stackoverflow.com/questions...n-value-when-use-add-filter/13797597#13797597">Wordpress: How to return value when use add_filter?</a>

I'm a little confused because this:

Code:
add_filter( 'wpseo_title', function(){return 'foo';}

Works outside the get_archive_seo functions, but not works inside function.

Can anybody help me?