简体   繁体   English

将JSON-LD(Schema)脚本注入WordPress网站的头部

[英]Injecting JSON-LD (Schema) scripts into head of WordPress site

I have a bunch of JSON-LD files that I want to inject into the head of various pages, some conditionally. 我有一堆JSON-LD文件,有条件地要注入到各个页面的头部。 I'm using WordPress, and their recommendation is to use a function like wp_enqueue_script . 我正在使用WordPress,他们的建议是使用类似wp_enqueue_script的函数。 The difficulty is I've yet to find a way to edit the <type> attribute using the wp_enqueue_script function. 困难在于我还没有找到使用wp_enqueue_script函数编辑<type>属性的方法。

I was able to do it clumsily with PHP like this (maybe there's a better PHP function?): 我可以像这样用PHP笨拙地做到这一点(也许有更好的PHP函数?):

// header.php

<head>
    ...all the regular stuff..

    ..near the bottom..
    <?php
        if ( is_front_page() ) {
            echo file_get_contents(get_template_directory() . '/assets/structured-data/local-business.js');
        } 
    ?>

</head>

The scripts I'm trying to inject are formatted like this: 我尝试注入的脚本的格式如下:

// JSON-LD format
<script type="application/ld+json">
{
    "@context": "http://schema.org",
    "@type": "localBusiness",
    "currenciesAccepted": ...
}
</script>

So I`m just trying to read the files in. This breaks when I try to include multiple scripts. 因此,我只是想读取文件。当我尝试包含多个脚本时,这会中断。

I would like to follow WP recommendations and use a method like wp_enqueue_script() but as of yet, I haven't found a way to do that. 我想遵循WP建议,并使用类似wp_enqueue_script()的方法,但wp_enqueue_script() ,我还没有找到一种方法来做到这一点。 The whole function won't work unless I can mark the scripts as type="application/ld+json" , required for JSON-LD. 除非我可以将脚本标记为JSON-LD所需的type="application/ld+json" ,否则整个功能将无法工作。

The way I am currently doing this is with add_action(); 我目前的方式是使用add_action();

add_action( 'wp_head', 'my_theme_schema' );

Then in my_theme_schema, I build my schema, and just output it with the script tags. 然后,在my_theme_schema中,构建我的架构,然后将其与脚本标签一起输出。 ( I typically build the schema with an array ). (我通常使用数组构建架构)。

function my_theme_schema() {
  $schema = [];
  ... // build up the schema
  $output = json_encode( $schema, JSON_UNESCAPED_SLASHES );
  ?>
    <script type="application/ld+json">
      <?= $output; ?>
    </script>
  <?php
}

This gives me the opportunity to put it in the functions.php. 这使我有机会将其放入functions.php中。

Also, I typically do not put it in the head, I put it in the footer. 另外,我通常不放在头部,而是放在页脚。

add_action( 'wp_footer', 'my_theme_schema' );

I don't know if one is inherently better, I just prefer JS in the footer. 我不知道一个人是否天生好一点,我只喜欢在页脚中使用JS。

I found a way to do this: 我找到了一种方法来做到这一点:

//here we enqueue a dummy script to help us with what we want to achieve
add_action('wp_enqueue_scripts', 'myslug_wp_load_files');
function myslug_wp_load_files()
{
    wp_register_script( 'myslug-dummy-handle-json-footer', plugins_url('scripts/loader.js', __FILE__), [], '', true );
    wp_enqueue_script( 'myslug-dummy-handle-json-footer'  );
}

//here we construct our ldjson and add it to our enqueued script
add_action('wp_head', 'myslug_construct_ldjson');
function myslug_construct_ldjson()
{
    $my_ldjson = '{construct_here_your_ldjson}';
    wp_add_inline_script( 'myslug-dummy-handle-json-footer', $my_ldjson );
}

//here we add a filter to the script loader, and modify text/javascript to application/ld+json
add_filter( 'script_loader_tag', 'myslug_async_tag', 10, 3 );

function myslug_async_tag( $tag, $handle, $src ) 
{
    if ( $handle !== 'myslug-dummy-handle-json-footer' ) 
    {
        return $tag;
    }
    $tag = str_replace("type='text/javascript'", "type='application/ld+json'", $tag);
    return $tag;
}

The above script registers and enqueue a dummy script - which can be practically anything (or even blank). 上面的脚本注册并加入一个虚拟脚本-几乎可以是任何虚拟脚本(甚至是空白)。 Also, it adds the dynamically generated JSON-LD to the dummy script and then it filters all scripts, selects only the script we created and replaces type 'text/javascript' with type 'application/ld+json'. 另外,它将动态生成的JSON-LD添加到虚拟脚本中,然后过滤所有脚本,仅选择我们创建的脚本,然后将类型“ text / javascript”替换为类型“ application / ld + json”。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM