Site logo

Archived topic

$content on Hook generate_after_content

9 replies · Started by Butch Pornebo on March 11, 2021

Viewing posts 1–10 of 10

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.

Hi Butch,

I'm not sure I understand your question here, could you explain more?

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?

Is 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.

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?

Hi 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.

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",

Please use the proper reply formatting for the code party of your reply so your reply looks more legible.
https://share.getcloudapp.com/eDu8Od6R

First off, your code is a filter which can be placed as is on your child theme's functions.php or 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-overview

You then set the display rule to the pages you want this applied and set the hook to generate_after_content or wp_footer.

in wp_footer can I pull $content within the hook by using get_the_content()

in wp_footer can I pull $content within the hook by using get_the_content()

get_the_content() is the $content by default.
https://developer.wordpress.org/reference/functions/the_content/#source

Say 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>
This archived topic is closed to new replies.