Black Friday Sale! Get $20 off GP Premium, $40 off our new Lifetime license, and 45% off license renewals/extensions! Learn more

[Support request] JSON-LD into header of select pages?

Home Forums Support JSON-LD into header of select pages?

Viewing 15 posts - 1 through 15 (of 15 total)
  • Author
    Posts
  • #318958
    Mark

    Hi,

    I need to be able to add JSON-LD schema into the header (I assume that is where it should go) of certain pages and posts. The schema would be different for each of the pages/posts in question so I need to be able to update individually. Is there a built in way to do this in GP?

    GeneratePress 1.3.46
    #318975
    Mark

    A bit more information. Here is what I found on the net in terms of doing this:

    1. Create a custom field named “schema”

    2. Add code such as this to the custom field:

    <script type=”application/ld+json”>
    {
    “@context”: “http://schema.org&#8221;,
    “@type”: “LocalBusiness”,
    “address”: {
    “@type”: “PostalAddress”,
    “addressLocality”: “Example Town”,
    “addressRegion”: “Essex”,
    “streetAddress”: “123 Example Street”
    },
    “description”: “A super shop that sells everything at super low prices.”,
    “name”: “Super Shop”,
    “telephone”: “01234567890”
    }
    </script>

    3. In the header.php file just before </head> add:

    <?php
    $schema = get_post_meta(get_the_ID(), ‘schema’, true);
    if(!empty($schema)) {
    echo $schema;
    }
    ?>

    Is this the way I should do it in GP or do you have a built in way to accomplish similar things?

    Thanks!

    #319039
    Tom
    Lead Developer
    Lead Developer

    Instead of placing that in the header.php file, I would put it into the wp_head hook in GP Hooks.

    Other than that it looks great ๐Ÿ™‚

    #342383
    Sergio

    Hi!

    One question. How and where do you create that custom field where you have to write the JSON-LD code?

    #342423
    Tom
    Lead Developer
    Lead Developer

    If you click “Screen Options” at the top right of your editing page, you’ll see a checkbox that says “Custom Fields” which reveals a meta box for you to add custom fields.

    #342548
    Sergio

    thanks!

    #342737
    Tom
    Lead Developer
    Lead Developer
    #342784
    Sergio

    Hi again…

    I’m trying to do this section thing.

    I added the php script to wp_head and the JSON-LD script with the structured data to she schema section of the page (not post) and it’t not working. I’ve used Google Structured Data Testing Tool and those structured data are not showing, only WPHeader and SiteNavigationElement, data that I supposed are automatically added by WordPress. I have tried with the Execute PHP checked and unchecked.

    I’d be very grateful if you could provide some insight in this matter.

    S.

    #342785
    Sergio

    And I’d like to add some question into this issue.

    Ideally, I’d like to have some common (for every page and post) structured data, and then some specific data por each page.

    Where should I type de JSON-LD script for the common data?

    #342866
    Tom
    Lead Developer
    Lead Developer

    GeneratePress adds structured data to all of the built in elements for you.

    What kind of data are you specifically trying to add?

    #342891
    Sergio

    I’m trying to add specific information about each service offered, not the classical address and rating snippet thing.

    Those data shows when I type the JSON-LD script inside the wp_head hook. The thing is that if I do this this way, it’s shown in every page. I’d prefer to have specific data for each page.

    I wouldn’t mind typing the common structured data in every page, but I think it’s more elegant and easier in the long term if I split those data in common information for the whole site (like contact information) and specific information for each page.

    #342970
    Sergio

    Could it be that the following is not for pages but only for posts? I have no idea about his, please forgive me. I’m just used to copy & paste and trial & error.

    <?php
    $schema = get_post_meta(get_the_ID(), โ€˜schemaโ€™, true);
    if(!empty($schema)) {
    echo $schema;
    }
    ?>

    #343162
    Tom
    Lead Developer
    Lead Developer

    Just just grabbing whatever value you add for the “schema” name and outputting it. It won’t do anything unless it’s following schema guidelines inside your specific element.

    It will work no matter where you place it as long as that value exists.

    #343248
    Sergio

    hmmmmm well, then I don’t know what’s going wrong.

    I have the section, named “schema”, my JSON-LD script is in that section, and the script itself is working (it follows the guidelines and its actually passing the Google Structured Data Testing Tool on its own and, when I type that code in the wp_head hook it’s also outputted and the URL passes the testing tool), but it seems it’s not working when I do the section thing.

    #343547
    Tom
    Lead Developer
    Lead Developer

    Ok, so in the custom fields meta box, you have:

    The name: schema
    The value: Your code

    Then you’re adding this into the wp_head hook?:

    $schema = get_post_meta(get_the_ID(), 'schema', true);
    if ( !empty( $schema ) ) {
        echo $schema;
    }

    Execute PHP is checked?

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