Created
September 21, 2019 07:13
-
-
Save salvatorecapolupo/48d5c49f329aac8bd39bb6352e754d97 to your computer and use it in GitHub Desktop.
Dynamic WordPress FAQPage Rich Snippets - use this in a plugin or functions.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?PHP | |
function add_rich_snippets_content( $content ) { | |
global $post; | |
$post_ID = $post->ID; | |
$page_title = get_the_title ( $post_ID ); | |
$page_object = get_post( $post_ID ); | |
$page_excerpt = wp_strip_all_tags ( $page_object->post_content ); | |
$page_excerpt = preg_split ("/\s/", $page_excerpt); | |
$page_excerpt2 =""; | |
for ( $j=0; $j<65; $j++ ){ | |
$page_excerpt2 .= $page_excerpt[ $j ] . " "; | |
} | |
$flag = in_category("blog-seo"); | |
if ( $flag==1 ) { | |
$titolo_domanda = $page_title; | |
$risposta = $page_excerpt2; | |
$code = ' | |
<script type="application/ld+json"> | |
{ | |
"@context": "https://schema.org", | |
"@type": "FAQPage", | |
"mainEntity": [ | |
{ | |
"@type": "Question", | |
"name": "'.$titolo_domanda.'", | |
"acceptedAnswer": { | |
"@type": "Answer", | |
"text": "'.$risposta.'" | |
} | |
} | |
] | |
} | |
</script> | |
'; | |
$content .= $code; | |
} | |
return $content; | |
} | |
add_filter( 'the_content', 'add_rich_snippets_content',100 ); | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment