Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

197
Vistas
Code to automatically generate meta tags in wordpress is not working properly

I need to automatically generate meta tags from content in wordpress, the problem is that the code that I found on many blogs while searching either uses $des_post = str_replace( array( "\\n", "\\r", "\\t" ), ' ', $des_post); or $des_post = str_replace( array( "\n", "\r", "\t" ), ' ', $des_post);, which offers a security flaw (XSS), not to mention that it doesn't always strip the html tags correctly, so I've modified the code as follows:

function gretathemes_meta_description() {
    global $post;
    if ( is_singular() ) {
        $des_post = strip_tags( $post->post_content );
        $des_post = strip_shortcodes( $post->post_content );
        $des_post = esc_attr( $des_post );
        $des_post = mb_substr( $des_post, 0, 300, 'utf8' );
        print '<meta name="description" content="' . $des_post . '" />' . "\n";
    }
    if ( is_home() ) {
        print '<meta name="description" content="' . get_bloginfo( "description" ) . '" />' . "\n";
    }
    if ( is_category() ) {
        $des_cat = strip_tags(category_description());
        print '<meta name="description" content="' . $des_cat . '" />' . "\n";
    }
}
add_action( 'wp_head', 'gretathemes_meta_description');
function gretathemes_meta_tags() {
    print '<meta name="meta_name" content="meta_value" />';
}
add_action('wp_head', 'gretathemes_meta_tags');

The problem is that now it doesn't remove the html tags, so I get a meta description like this while verifying with metatag checkers (seo tools):

<!-- wp:paragraph -->
<p>The description goes here.

The problem is that html code like <!-- wp:paragraph --><p> shouldn't show up. The thing here is that the function esc_attr is getting the html code and copying, isn't there a way to get the rendered post's text instead? Or any other solution?

about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

Use html purifier

in your theme's functions.php or plugin file:

require_once '/path/to/HTMLPurifier.auto.php';

function better_strip_tags($dirty_html){
  $config = HTMLPurifier_Config::createDefault();
  $purifier = new HTMLPurifier($config);
  return $purifier->purify($dirty_html);
}


about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda