Support » Plugin: The SEO Framework » How to retrieve JSON-LD for use in headless WP?

  • Hi all,

    I’ve been using WP in a headless capacity for years now and a client has requested that I implement The SEO Framework into a specific project we are working on.

    I would like to make use of TSF’s generated JSON-LD so that I might be able to render it within our client-side app, and to do so, I need to be able to retrieve the html_output from init.class.php in a variety of places such as custom endpoint responses, etc.

    I know I can access methods via the_seo_framework() but if I run ->html_output without somehow specifying which post to run it against I get the wrong information returned, obviously.

    How do I go about using a method such as html_output outside of the WordPress loop?

    • This topic was modified 5 months, 3 weeks ago by jmikrut.
Viewing 2 replies - 1 through 2 (of 2 total)
  • Plugin Author Sybre Waaijer

    (@cybr)

    Hello 🙂

    The method html_output() is meant to run for the main query only; after all, it’s an action callback… which I should mark as private. It calls most methods from render.class.php, which use the main query to obtain the values and put them in HTML meta tags.

    Keep in mind that nothing in render.class.php nor init.class.php is meant to be called outside the loop. However, you may find that functions deeper than the ones in render.class.php to be suited for this.

    That said, I’m afraid I have to disappoint you: the LD-JSON output doesn’t have an API for displaying; only for modifying.

    This is a symptom of TSF not presenting this information to the user on the admin-side; while all other methods–which have inputs, placeholders, and SEO Bar ratings–do have an API for displaying, which include titles, descriptions, social meta, robots, etc.

    I may change this behavior in TSF v3.4 as I’m planning to rewrite the breadcrumb support–I may as well overhaul the JSON generation while I’m at it.
    See: https://github.com/sybrew/the-seo-framework/issues/440

    In the generate-*.class.php files you can find the API-friendly methods, to name a few:

    get_title( $args )
    get_custom_field_title( $args )
    get_generated_title( $args )
    
    get_description( $args )
    get_description_from_custom_field( $args )
    get_generated_description( $args )
    
    get_canonical_url( null ) // don't supply $args, use create_canonical_url() instead
    create_canonical_url( $args )
    
    robots_meta( $args ) // N.B. from TSF v3.3+ it accepts $args
    

    Note that I didn’t list image methods, as they’re getting a rework in v3.3, but I still have to get started on the images.

    I hope this explains the lot! Cheers 🙂

    Sybre,

    Thanks for the response. I appreciate it – notes all make sense and are what I assumed. I will stay tuned for any further work on the JSON-LD front and for now I’ll use the methods you’ve indicated to reconstruct them on the frontend.

    Cheers! Keep up the good work (and the fast responses)!

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