跳转到主要内容

acf/schema/render_script

Action hook fired before the JSON-LD script tag is rendered.

Description

This action allows you to execute code immediately before the JSON-LD script tag is output to the page. This is useful for logging, analytics, or adding additional markup around the JSON-LD output.

Note: This is an action hook, not a filter. It does not modify the JSON-LD data.

Changelog

  • Added in version 6.8.0

Parameters

do_action( 'acf/schema/render_script', $jsonld_data );
  • $jsonld_data (array) The JSON-LD data array that will be rendered

Example

This example logs JSON-LD output for debugging.

/**
 * Log JSON-LD data before rendering.
 *
 * @param array $jsonld_data The JSON-LD data array.
 */
function my_acf_schema_log_output( $jsonld_data ) {
    if ( defined( 'WP_DEBUG' ) && WP_DEBUG ) {
        error_log( 'ACF JSON-LD Output: ' . wp_json_encode( $jsonld_data ) );
    }
}
add_action( 'acf/schema/render_script', 'my_acf_schema_log_output' );

This example adds a comment before the JSON-LD for identification.

/**
 * Add identifying comment before JSON-LD.
 *
 * @param array $jsonld_data The JSON-LD data array.
 */
function my_acf_schema_add_comment( $jsonld_data ) {
    $type = $jsonld_data['@type'] ?? 'Unknown';
    echo '<!-- ACF Schema: ' . esc_html( $type ) . ' -->' . "\n";
}
add_action( 'acf/schema/render_script', 'my_acf_schema_add_comment' );

This example tracks which schema types are being output.

/**
 * Track schema type usage.
 *
 * @param array $jsonld_data The JSON-LD data array.
 */
function my_acf_schema_track_usage( $jsonld_data ) {
    if ( ! isset( $jsonld_data['@type'] ) ) {
        return;
    }

    // Send to analytics
    if ( function_exists( 'my_analytics_track' ) ) {
        my_analytics_track( 'schema_rendered', array(
            'type'    => $jsonld_data['@type'],
            'post_id' => get_the_ID(),
        ) );
    }
}
add_action( 'acf/schema/render_script', 'my_acf_schema_track_usage' );