- This topic has 9 replies, 4 voices, and was last updated 4 years, 7 months ago by
Elvin.
-
AuthorPosts
-
March 11, 2021 at 10:27 am #1691561
Butch Pornebo
Can I get $content at this point?
If so, How?
Don’t have a complete understanding of PHP BUT I can read coding and sort of deciphering what the code meant.
March 11, 2021 at 2:30 pm #1691778Ying
StaffCustomer SupportHi Butch,
I’m not sure I understand your question here, could you explain more?
March 11, 2021 at 2:48 pm #1691784Butch Pornebo
On elements, if I’m going to add PHP code on Hook generate_after_content.
Is the variable parameter $content being passed to the hook?
What are the specifications for the hooks?
March 11, 2021 at 7:41 pm #1691909Leo
StaffCustomer SupportIs the variable parameter $content being passed to the hook?
No – hooks simply allow you to add your content/code to that specific spot.
What are the specifications for the hooks?
Not sure what you meant but perhaps some articles like these would help?
https://developer.wordpress.org/plugins/hooks/#actions-vs-filters
https://www.wpbeginner.com/glossary/hooks/You should be able to find lots of resources for using hooks online.
Hope this helps to get you started.
March 11, 2021 at 7:46 pm #1691913Butch Pornebo
I need my code that is going to execute on the hook “after content” to access the DOM of the post article.
How do I go about doing that within the GP environment?
March 11, 2021 at 8:06 pm #1691928Elvin
StaffCustomer SupportHi there,
Can you explain a bit more on what you’re trying to do?
If the content you’re trying to fetch isn’t exclusively added as a content but rather, a dynamic value from something else (ACF, post meta), then you should be able to fetch them and place them on a hook.
But if its exclusively added as a content (paragraph blocks, heading, etc), you may have to preg match the DOM element you’re looking for within
get_the_content()and save it as a variable you can use within a hook.Note: If you want something placed in a content but is also meant to be fetched or reused for a hook, Make sure it’s a dynamic value you can call anywhere rather than adding it in as a static thing within the content and try to take it from there.
March 11, 2021 at 8:34 pm #1691948Butch Pornebo
I’m not much of a PHP programmer BUT I can understand a little of what the code is supposed to do
Anyway, a colleague left with this code for me
function add_FAQSchema($content) {
$content = mb_convert_encoding($content, ‘HTML-ENTITIES’, “UTF-8”);
$dom = new DOMDocument();
@$dom->loadHTML($content);foreach($dom->getElementsByTagName(‘h2’) as $key=>$node) {
$matches[$key][‘h2Tag’] = $node->textContent;
$dom->saveHtml($node);
while(($node = $node->nextSibling) && $node->nodeName !== ‘h2’) {
if($node->nodeName == ‘p’) {
$matches[$key][‘ptag’][] = $dom->saveHtml($node);
}
}
}if(!empty($matches) && count($matches) > 0){
foreach ($matches as $key => $value) {
$jsoneContentArr[$key][“@type”] = “Question”;
$jsoneContentArr[$key][“name”] = $value[‘h2Tag’];
$jsoneContentArr[$key][“acceptedAnswer”][“@type”] = “Answer”;
$jsoneContentArr[$key][“acceptedAnswer”][“text” ] = $value[“ptag”][0].” … Read More“;
}
}
$contentArr[“mainEntity”] =$jsoneContentArr;$after_content = ‘<script id=”myFAQSchema” type=”application/ld+json”>’.json_encode($contentArr).'</script>’;
return $fullcontent = $content . $after_content;
}
add_filter(‘the_content’, ‘add_FAQSchema’);and it echoed out this to dynamically embed. It parses all the H2 with Span ID.
<script id=”myFAQSchema” type=”application/ld+json”>{“mainEntity”:[{“@type”:”Question”,”name”:”FAQ Number 1″,”acceptedAnswer”:{“@type”:”Answer”,”text”:”<p>Lorem Ipsum is simply dummied text of the printing and typesetting industry. Lorem Ipsum has been the industry\u2019s standard dummy text ever since the 1500s when an unknown printer took a galley of type and scrambled it to make a type specimen book.<\/p> … Read More<\/a>”}}]}</script>
What I wanted to do is used that PHP code as a hook on “after-content”?
What PHP mods I need to make it happen?
Also, since we are it,
It is missing ahead of “mainEntity”
“@context”: “https://schema.org”, “@type”: “FAQPage”,
So the correct echo should have
“@context”: “https://schema.org”, “@type”: “FAQPage”, “mainEntity”:[ ………..
I don’t suppose you would know the code so that I can prepend the missing “@context”: “https://schema.org”, “@type”: “FAQPage”,
March 11, 2021 at 8:47 pm #1691956Elvin
StaffCustomer SupportPlease use the proper reply formatting for the code party of your reply so your reply looks more legible.
https://share.getcloudapp.com/eDu8Od6RFirst off, your code is a filter which can be placed as is on your child theme’s
functions.phpor a Code Snippets plugin. You don’t need to hook it in.I assume what you wanted to hook in was the
<script> ....</script>part of the code?If so, you can simply place that particular code within a Hook Element.
https://docs.generatepress.com/article/hooks-element-overviewYou then set the display rule to the pages you want this applied and set the hook to
generate_after_contentorwp_footer.March 11, 2021 at 9:01 pm #1691967Butch Pornebo
in
wp_footercan I pull$contentwithin the hook by usingget_the_content()March 11, 2021 at 9:20 pm #1691978Elvin
StaffCustomer Supportin wp_footer can I pull $content within the hook by using get_the_content()
get_the_content()is the$contentby default.
https://developer.wordpress.org/reference/functions/the_content/#sourceSay if you’re using it on a Hook Element, you can call it like this:
<?php $post_content = get_the_content(); ?>and if you want to use it within a script:
<script> //echo content <?php echo $post_content; ?> </script>Another Example:
<script id="myFAQSchema" type="application/ld+json"> {"mainEntity":[{ @content:"<?php echo $post_content; ?>" }]} </script> -
AuthorPosts
- You must be logged in to reply to this topic.