[Support request] Elements > Hook > generate-after-content

Home Forums Support [Support request] Elements > Hook > generate-after-content

Home Forums Support Elements > Hook > generate-after-content

Viewing 11 posts - 1 through 11 (of 11 total)
  • Author
    Posts
  • #1679708
    Butch Pornebo

    Not very familiar with the internal workings of WordPress BUT I want to hire a PHP developer the creates and outputs a SCHEMA and embed it after the content.

    With the hook generate-after-content,

    what is being passed to the element?

    Will the HTML of the post available for parsing?

    What is the data layout that is being passed to the element?

    #1679730
    Butch Pornebo

    here is the requirement I’m sending to the coder.

    WordPress hook “after-content”

    Create a FAQName Schema just like the example below and MUST pass Test from Google https://search.google.com/test/rich-results.

    Output exactly as the example below except to fill up the following variables that are pulled from the post html content.

    The code will execute as a WordPress hook after content. Not sure what wordpress will pass to the hook at this point. It could be HTML code for the post or if wordpress creates a DOM for the post.

    So you should need to be familiar with how to use wordpress hooks in order for this to work.

    You are going to locate all H2 HTML Tags and every H2 HTML Tags will have its own entry.

    ASSUME that for every H2 tag that you encounter it will be exactly coded like this.

    <h2><span class=”ez-toc-section” id=”How-IKEA-Kitchen-Cabinets-Are-Made”></span>How IKEA Kitchen Cabinets Are Made<span class=”ez-toc-section-end”></span></h2>
    <p>Kitchen cabinets IKEA are made from different types of wood byproducts. Their cabinets are made from medium-density fiberboard (MDF) which makes every kitchen cabinet at IKEA unique from its competitors.</p>

    So to fill up the variables:

    “name”: “Question #n”
    Question #n will be the H2 Text

    “text”: “Answer to Question #n … Read More”
    Answer to Question #n – Excerpt the 15 words from the paragraph <p> following right after the H2 tag.

    … Read More – is the 2nd part of the answer. It is an internal hyperlink that links directly to the H2 tag. You will be using the <span id of this H2 tag entry. This is what it should output.

    … Read More

    ————————————————————-

    <script type=”application/ld+json”>
    {
    “@context”: “https://schema.org/&#8221;,
    “@type”: “FAQPage”,
    “mainEntity”: [
    {
    “@type”: “Question”,
    “name”: “Question #1”,
    “acceptedAnswer”: {
    “@type”: “Answer”,
    “text”: “Answer to Question #1 … Read More
    }
    },
    {
    “@type”: “Question”,
    “name”: “Question #2”,
    “acceptedAnswer”: {
    “@type”: “Answer”,
    “text”: “Answer to Question #2 … Read More
    }
    },
    {
    “@type”: “Question”,
    “name”: “Question #n”,
    “acceptedAnswer”: {
    “@type”: “Answer”,
    “text”: “Answer to Question #n … Read More
    }
    }
    ]
    }
    </script>

    #1680552
    David
    Staff
    Customer Support

    Hi there,

    the GP Hooks does little more than provide the ability to insert code / content into various parts of the theme ( or WP or plugin ) templates. It won’t do anything to assist with what you’re trying to do.

    As a note generally JSON-LD is output to the wp_head ( Header ) of a site – so it gets parsed first by search engines.
    Generating JSON-LD from HTML would require the HTML to contain the relevant schema attributes for it to generate the code you require. Generally it is done the other way around ie. Custom Fields are used to generate both the JSON-LD and the required HTML.

    #1680588
    Butch Pornebo

    ok. got you BUT my main concern is when the code executes, what does the hook “generate-after-content” pass as a variable to the code, “the_content”?

    Just forgot for a second what exactly I want for the coder to do.

    #1680594
    David
    Staff
    Customer Support

    The generate-after-content hook ( or any GP Hooks ) doesn’t pass anything to the the_content.
    Hooks are located in the theme template you can see its location here in the single content template for example:

    https://github.com/tomusborne/generatepress/blob/7b35168f546f83a7abb098a8da3bc0674d49ddeb/content-single.php#L99

    the_content is output earlier in the template, in this case line 73.

    Thera are no action hooks within the_content. But it is filterable:
    https://developer.wordpress.org/reference/hooks/the_content/

    #1680604
    Butch Pornebo

    ok… let me change the question….

    once the generate-after-content hook passes control to my code,

    Can I access the HTML or DOM of the post article? How?

    #1681580
    David
    Staff
    Customer Support

    There are methods of parsing the DOM in PHP – such as DOMDocument function. You’ll also find users who have written PHP Classes to extend those functions for parsing and generating JSON-LD such as this:

    https://www.phpclasses.org/package/11664-PHP-Generate-metadata-for-HTML-pages-in-JSON-LD-format.html

    But with WP you can create Custom Post Types and Custom Fields for storing the ‘data’ you require – which can then be used to both output the HTML and JSON-LD …. which would be the more logical method. There are also plenty of plugins that do this kinda thing for you… in particular most SEO plugins offer it such as RankMath which has a dedicated FAQ Block.

    #1682089
    Butch Pornebo

    Thank you for your research and it is much appreciated.

    However, on the PHP classes web, I already got a plugin installed to do the “generalized” global schema and it is NOT what I’m looking for as far as the FAQ schema is concerned.

    Rankmath & Yoast does have had a FAQ Schema BUT it is only available as a Gutenberg block and it requires you to enter the FAQ MANUALLY.

    I have over 300+ articles in my blog and building another blog that I plan to publish even more than 300 articles.

    Can you imagine entering all the FAQ manually as what Rankmath & Yoast requires?

    What I want is to generate a FAQ Schema dynamically which DOES NOT require me to enter it manually.

    Since all of my published articles have a standard format that all SUB-SECTIONS are tag with an <H2> HTML tag, the code will execute after the article content is generated that is hook into “after-content” or “after-footer”, it will parse all the <H2> tag and generates the FAQ Schema code dynamically that is unique for every article.

    With that said, Does GP hooks provide access to the HTML or DOM of the article content and how to access those in PHP code?

    #1682139
    David
    Staff
    Customer Support

    Yeah no fun manually creating that.

    Hooks don’t know what comes before or after them. They’re just a placeholder for inserting your own code at that particular point in the template. Their general use is to make a function callback

    For example:

    function my_made_up_function() {
       // Do some stuff
       // Out put some stuff
    }
    add_action('generate-after-content', 'my_made_up_function'):
    

    What happens is WP will execute the code in sequential order, when it comes to a Hook in a template ( or any code ) it looks to see if any function callbacks have been registered to that hook, if there are functions, it executes them before continuing down the code.

    In my silly example, anywhere the generate-after-content hook is found, WP will make the my_made_up_function callback.

    So the hook itself will have absolutely no involvement in the function your PHP Developer will write. It simply sets when the code should be executed.

    The method your suggesting sounds more like a Client Side Javascript function where you want to scan the HTML on the frontend to generate the JSON. Whereas with PHP you will do everything server side before it is passed to the browser.

    You would need a function that uses regex to match the HTML you want to extract and output that in the form you need it. Heres an example function that does that to create a Table Of Contents that uses that method:

    https://webdeasy.de/en/wordpress-table-of-contents-without-plugin/

    You can then make the function callback from any hook on a single post. For JSON-LD you would want to use the wp_Head so its at the very top of the DOM.

    #1682407
    Butch Pornebo

    much appreciated…. will look into your suggestions.

    “Whereas with PHP you will do everything server-side before it is passed to the browser.”

    Kind of this BUT not necessarily for the browser because the browser would NOT care about the FAQ Page Schema. It’s more for when google is crawling and indexing the page because it uses the FAQ Page Schema to display in the SERPS.

    #1682601
    David
    Staff
    Customer Support

    “Kind of this BUT not necessarily for the browser because the browser would NOT care about the FAQ Page Schema. It’s more for when google is crawling and indexing the page because it uses the FAQ Page Schema to display in the SERPS.”

    The point i was trying to make is that the PHP server side method makes sure that the JSON-LD is part of the markup on DOM load – whether that be for the browser or for Bots to crawl the site. Although it is acceptable to add it dynamically using JS, see here, it is preferable to do it server side, read here

    Hope that helps with your developer brief.

Viewing 11 posts - 1 through 11 (of 11 total)
  • You must be logged in to reply to this topic.