7

I would like to add to my pages the structured data in JSON-LD (https://developers.google.com/search/docs/guides/intro-structured-data)

Basically, what I need to do is to add somewhere in the HTML for, let's say a basic page, a snippet like the following:

<script type="application/ld+json">
{  "@context":"http://schema.org",
   "@type":"WebSite",
   "name":"Site name",
   "alternateName":"Site alternatime name",
   "description":"Site description",
   "keywords":"list of keyword",
   "inLanguage":"en",
   "url":"http://www.example.eu/en",
   "potentialAction":{
   "@type":"SearchAction",
   "target":"http://www.example.com/en/find/{search_term_string}",
   "query-input":"required name=search_term_string"
   }
}
</script>

I've tried to use the full HTML input but the snippet gets (kind of) escaped with the CDATA and google cannot parse it.

So, how can I provide that snippet without any escaping?

  • Are you aware that Drupal 8 comes with RDFa (which is an alternative to JSON-LD) support? See e.g. rdfui – unor Jan 22 '17 at 3:42
  • 1
    Welcome to Drupal Answers! Do you have a more specific question which is not Do you have any suggestions? This site is not for suggestions, but to answer well-defined questions with a definitive answer. – kiamlaluno Jan 22 '17 at 7:53
  • My specific question is: I need to add to the HTML of the page (let's say basic page) the snippet provided above. I've tried to use the full HTML input but the snippet gets (kind of) escaped with the CDATA and google cannot parse it. So, how can I provide that snippet without any escaping? – user3535189 Jan 22 '17 at 10:28
  • I think that in some way RDFa is an old way of providing semantic metadata about a website and it will be replaced by JSON-LD structured data. Now google is pushing for JSON-LD as you can see here: developers.google.com/search/docs/guides/intro-structured-da‌​ta. – user3535189 Jan 22 '17 at 11:38
  • 1
    There is a sandbox module for D8 JSON-LD (D8 core). Since it's a sandbox project, it is not advisable to use it in production because it is not properly tested. But you can have a look at it and try in local if it works. You can download the module code from here. If you're interested to work on it, you can also ask the module owner to add you as maintainer so you can continue work and make the module complete and stable. – Yogesh Jan 30 '17 at 5:32
3
+100

Here's a simple way to do this with no additional modules required. For the sake of this example, I'm going to add this field to the Basic page content type that comes with the Standard installation profile.

First, let's create a new Text format. Go to admin/config/content/formats and click Add text format. I name the Text format JSON-LD and make it only available for Administrators. Be sure to leave all options blank and set Text editor to None, like so:

Step 1

Then, let's add a new field to the Basic page content type. Go to admin/structure/types/manage/page/fields/add-field and select Text (formatted, long). I name the field JSON-LD with an automatic machine name of field_json_ld.

Step 2

Then let's make sure we hide the field's label. Go to admin/structure/types/manage/page/display and make sure to set the Label to our new JSON-LD field to - Hidden -, like so:

Step 3

Now it's time to add the script to a new page. Go to node/add/page and fill in the Title and Body as you like. Here's the important bit: for the JSON-LD textarea, be sure to set the Text format to JSON-LD, like so:

Step 4

Now click Save and publish. Your node will look like this:

Step 5

Nothing special, right? Now let's take a look at our source code:

Step 6

Bingo!

  • Hi, I am somewhat confuse here, first lets understand what is the purpose of this script. From its content it and from JSON-LD words, it looks like it has to be detected by Search engine boats. Hence, it has to be present in <head></head> section of the site. Please correct me if I am wrong. Thanks – CodeNext Feb 3 '17 at 4:29
  • @CodeNext: yes it should be inside the head tag. But I noticed that Google is reading it even if it is placed in the body. Is there a way to insert some custom HTML in the head tag? – user3535189 Feb 3 '17 at 9:54
  • @user3535189 If you need the way to add that part, please see my answer HERE, I have compiled all ways to add code in <head></head>, I hope it will help. – CodeNext Feb 3 '17 at 10:18
  • @CodeNext thank you for your suggestion, it is good for something like a meta tag for the whole website. But i was searching tomething more clever, I mean, if I have 20 pages, each page should have a different "JSON-LD" snippet. So I'll probably need a text field like the one explained by Wim, that is then placed in the "head2 instead of the "body" tag. – user3535189 Feb 3 '17 at 10:27
  • @user3535189, Hhmmm, in that case this is appropriate way. Please after testing in your live site let me know, if google is identifying this way this snippet. I need to reward bounty to this answer, if you are accepting as a answer. Thx – CodeNext Feb 3 '17 at 10:32
1

The default Full HTML format uses CKEditor, which performs it's own text filtering and is likely what is causing your problems. You can disable CKEditor's Advanced Content Filter, but what I would recommend is creating a new text format with no filtering and no text editor. Then create a new long text field for your page's content type, and set the new text format as default.

You should be able to paste whatever scripts you need in the new field.

-2

If you want to add the script to the editor then use PHP code filter instead full html,(PHP module in drupal 8) then add your snippet. It will works fine. Hope its resolve your issue. See attached screenshots:

enter image description here enter image description here

  • This could be a way, but I think that Wim's answer is better. – user3535189 Feb 3 '17 at 11:16
  • Yes Sure, but in this way there is no need to add the new text format. – Karuna Feb 3 '17 at 12:21
-3

use :

<pre> <code> your codes here </code> </pre>.

this is the way all sites render any type of codes in HTML.

and this way write code in code tag, i think code will not escape.

  • 2
    Swing, but a miss :) The OP doesn't want to display the script in pre tags, they want to insert the script into a normal page without Drupal performing XSS filtering on it – Clive Feb 1 '17 at 18:42

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.