ID, 'full' ); $replacements = array( '{{title}}' => get_the_title( $post->ID ), '{{excerpt}}' => get_the_excerpt( $post->ID ), '{{date}}' => get_the_date( 'c', $post->ID ), '{{url}}' => get_permalink( $post->ID ), '{{featured_image_url}}' => $featured_image ? $featured_image : '', ); $content = str_replace( array_keys( $replacements ), array_values( $replacements ), $content ); $content = preg_replace_callback( '/\{\{acf:([a-zA-Z0-9_-]+)\}\}/', function ( $matches ) use ( $post ) { $field_name = $matches[1]; if ( ! function_exists( 'get_field' ) ) { return ''; } $field_value = get_field( $field_name, $post->ID ); if ( is_array( $field_value ) ) { return wp_json_encode( $field_value ); } elseif ( is_string( $field_value ) || is_numeric( $field_value ) ) { return esc_js( $field_value ); } return ''; }, $content ); return $content; } } /** * Output JSON-LD schema in the head section. */ if ( ! function_exists( 'agilepress_schema_output_jsonld' ) ) { function agilepress_schema_output_jsonld() { global $post; if ( ! is_singular() || ! $post ) { return; } if ( ! has_blocks( $post->post_content ) ) { return; } $blocks = parse_blocks( $post->post_content ); foreach ( $blocks as $block ) { if ( 'agilepress/block-agilepress-schema-json-ld-block' === $block['blockName'] ) { if ( ! empty( $block['attrs']['jsonldContent'] ) ) { $jsonld_content = $block['attrs']['jsonldContent']; $processed_content = agilepress_schema_process_placeholders( $jsonld_content ); $decoded = json_decode( $processed_content, true ); if ( json_last_error() === JSON_ERROR_NONE && is_array( $decoded ) ) { $safe_json = wp_json_encode( $decoded ); if ( $safe_json ) { wp_print_inline_script_tag( $safe_json, array( 'type' => 'application/ld+json' ) ); } } } } } } } add_action( 'wp_head', 'agilepress_schema_output_jsonld', 1 );