Are you a GenerateCustomer?

Do you have an active GP Premium or GenerateBlocks Pro license key?

Create a GenerateSupport account for GeneratePress and GenerateBlocks support in one dedicated place.

Create an account
Already have a GenerateSupport account? Login

Just browsing?

Feel free to browse the forums. Support for our free versions is provided on WordPress.org (GeneratePress, GenerateBlocks).

Want to become a premium user? Learn more below.

scheama problems – posting from api, custom meta ID

  • Hello all!

    I am posting from an api http request, it works fine, until i tried to add this setup..

    I have created a schema per post after it is written. I would like to add it to a custom meta ID field custom_schema and then display it in the header.

    It works when I immediately post the article with the custom filed as “published”, but when I “schedule” a post, the custom field does not get added via the api. Here is the code snippet I am using.

    function add_custom_schema_to_header() {
        if ( is_singular( 'post' ) ) {
            $cf = get_post_meta( get_the_ID(), 'custom_schema', true );
            if ( ! empty( $cf ) ) {
                echo '<script type="application/ld+json">' . wp_kses_post( $cf ) . '</script>'; // Outputs schema in a script tag
            }
        }
    }
    add_action( 'wp_head', 'add_custom_schema_to_header' );

    Do i need to register the field before the api call to add it?

    As is I am now getting a critical error with the setup. I try to add the field through the API step and then the post edits and looks good until i preview or post it.

    When i disable the script it works again.
    Custom meta field is not showing in custom fields, so not sure how to proceed from here. Is it in the database on the backend somewhere and now needs to be removed by hand?

    Please help!

  • Hi there,

    Your question is not related to the GP theme or any of our products, so it’s outside our area of expertise. That said, I’m happy to share my personal thoughts on the matter.

    It sounds like when you schedule a post, the custom_schema meta field is not being set properly via the API. If you’re using the REST API to create or update scheduled posts, you might need to ensure that the meta field is correctly saved.

    If you’re adding custom_schema via the WordPress REST API, make sure it’s registered properly using register_post_meta():

    function register_custom_schema_meta() {
        register_post_meta( 'post', 'custom_schema', array(
            'show_in_rest' => true, // Enables API access
            'type'         => 'string',
            'single'       => true,
            'sanitize_callback' => 'wp_kses_post',
        ) );
    }
    add_action( 'init', 'register_custom_schema_meta' );
    
Viewing 2 posts - 1 through 2 (of 2 total)
  • You must be logged in to reply to this topic.