Montar Schema FAQs WordPress

Hoy me ha tocado pelearme con el schema FAQs, el esquema de datos estructurados dedicados a las páginas de preguntas frecuentes.

¿Qué es eso del schema?

El schema de una página web, es un conjunto de datos estructurados que la web contiene, y que el usuario a simple vista no puede ver, pero que los distintos buscadores y programas que la analizan utilizan para saber algo más de ella.

En la página oficial de schema.org se puede ver toda la documentación sobre los distintos de datos que necesitamos incorporar según la naturaleza de nuestra página.

Schema para Preguntas Frecuentes

En este caso además de los datos genéricos que indican el tipo de contenido entre otros, se indicarán cada una de las preguntas, así como sus respuestas.

Como ejemplo, tenemos:

<script type="application/ld+json">
{
  "@context": "https://schema.org",
  "@type": "FAQPage",
  "mainEntity": [{
    "@type": "Question",
    "name": "What is the return policy?",
    "acceptedAnswer": {
      "@type": "Answer",
      "text": "Most unopened items in new condition and returned within <strong>90 days</strong> will receive a refund or exchange. Some items have a modified return policy noted on the receipt or packing slip. Items that are opened or damaged or do not have a receipt may be denied a refund or exchange. Items purchased online or in-store may be returned to any store.<br /><p>Online purchases may be returned via a major parcel carrier. <a href=http://example.com/returns> Click here </a> to initiate a return.</p>"
    }
  }, {
    "@type": "Question",
    "name": "How long does it take to process a refund?",
    "acceptedAnswer": {
      "@type": "Answer",
      "text": "We will reimburse you for returned items in the same way you paid for them. For example, any amounts deducted from a gift card will be credited back to a gift card. For returns by mail, once we receive your return, we will process it within 4–5 business days. It may take up to 7 days after we process the return to reflect in your account, depending on your financial institution's processing time."
    }
  }, {
    "@type": "Question",
    "name": "What is the policy for late/non-delivery of items ordered online?",
    "acceptedAnswer": {
      "@type": "Answer",
      "text": "Our local teams work diligently to make sure that your order arrives on time, within our normaldelivery hours of 9AM to 8PM in the recipient's time zone. During  busy holiday periods like Christmas, Valentine's and Mother's Day, we may extend our delivery hours before 9AM and after 8PM to ensure that all gifts are delivered on time. If for any reason your gift does not arrive on time, our dedicated Customer Service agents will do everything they can to help successfully resolve your issue. <br/> <p><a href=https://example.com/orders/>Click here</a> to complete the form with your order-related question(s).</p>"
    }
  }, {
    "@type": "Question",
    "name": "When will my credit card be charged?",
    "acceptedAnswer": {
      "@type": "Answer",
      "text": "We'll attempt to securely charge your credit card at the point of purchase online. If there's a problem, you'll be notified on the spot and prompted to use another card. Once we receive verification of sufficient funds, your payment will be completed and transferred securely to us. Your account will be charged in 24 to 48 hours."
    }
  }, {
    "@type": "Question",
    "name": "Will I be charged sales tax for online orders?",
    "acceptedAnswer": {
      "@type": "Answer",
      "text":"Local and State sales tax will be collected if your recipient's mailing address is in: <ul><li>Arizona</li><li>California</li><li>Colorado</li></ul>"}
    }]
  }
</script>

En el ecosistema WordPress

Llevémonos nuestras preguntas frecuentes al mundo WordPress.
Lo primero que vamos a necesitar es definir nuestra la entidad «faq», y para ello usaremos un custom post type «faq».

Como las preguntas constan de un enunciado y su respuesta, vamos a hacerlo fácilmente usando el título como enunciado, y content del post type como respuesta, por lo que no tendremos ni que definir campos personalizados.

La definición del custom port type quedaría tal que así:

$rewrite = array(
        'slug'                  => 'faq',
        'with_front'            => true,
        'pages'                 => true,
        'feeds'                 => true,
);
	    
$args = array(
        'label'                 => __( 'FAQ' ),
        'description'           => __( 'FAQ' ),
        'supports'              => array( 'title', 'editor' ),
        'taxonomies'            => array(),
        'hierarchical'          => false,
        'public'                => false,
        'show_ui'               => true,
        'show_in_menu'          => true,
        'menu_position'         => 5,
        'menu_icon'             => 'dashicons-editor-help',
        'show_in_admin_bar'     => true,
        'show_in_nav_menus'     => true,
        'can_export'            => true,
        'has_archive'           => true,
        'exclude_from_search'   => true,
        'publicly_queryable'    => true,
        'rewrite'               => $rewrite,
        'capability_type'       => 'page',
        'show_in_rest'          => true,
);
register_post_type( 'faq', $args );

Definida la entidad, ya sólo nos quedaría recuperarlas en la plantilla para añadirlo al script del schema.
Usaremos get_posts para recuperarlas y mediante bucle poder recuperar sus datos:

add_shortcode('add_faq_schema', 'add_faq_schema' );   
function add_faq_schema() {
        $output = '';
        $faqs = get_posts( array( 'post_type' => 'faq', 'numberposts' => -1 ) );
        if ( $faqs ) {
            $output .= '
                <script type="application/ld+json">
                    {
                      "@context": "https://schema.org",
                      "@type": "FAQPage",
                      "mainEntity": [{
                        "@type": "Question",
                        "name": "Título general de nuestras preguntas frecuentes, a modo de pregunta",
                        "acceptedAnswer": {
                          "@type": "Answer",
                          "text": "Descripción general de la respuesta a la pregunta general."
                        }
                      }
            ';
            foreach ( $faqs as $faq ) {
                $output .= '
                  , {
                        "@type": "Question",
                        "name": "' . addslashes( get_the_title($faq)) . '",
                        "acceptedAnswer": {
                          "@type": "Answer",
                          "text": "' . addslashes( apply_filters('the_content', $faq->post_content) ) . '"
                        }
                      }
                ';
            }
            $output .= '
                    ]
                  }
                </script>
            ';
        }
        return $output;
    }

Y de esta forma tan sencilla tendríamos nueva web preparada para alimentar a los buscadores con datos enriquecidos sobre preguntas frecuentes.

Eliminar schema Yoast en una página

Como tendremos ya nuestro schema FAQs en nuestra página de preguntas frecuentes, no vamos a necesitar ningún otro schema adicional.

Por lo que si estás usando el plugin de Yoast SEO, tendremos que quitar el schema que nos genera por defecto. Para ello, con este simple script que meteremos en functions.php, lo conseguiremos (suponemos que la página de preguntas frecuentes es la 156):

add_filter( 'wpseo_json_ld_output', 'yoast_seo_json_remove_partial' );
function yoast_seo_json_remove_partial() {
    if ( is_page ( 156 ) ) {
        return false;
    }
}