theme customizer – How to remove html tags from output with json_encode in WordPress? I want to show only plain text

Question

I’ve been on trial and error for a few days, researching a lot, but I can’t solve this problem.

objective: this function generates schema output for data structured in JSON.

the problem: the texts are being delivered with html <p> tags, n etc, and I think that Google is not integrating my structured FAQ data because of this.

Output example:

"@type": "Question",
                    "name": "<p>What are the best destinations for my trip?</p>n",

the solution: I need to remove the <p> and </p> tags and show only plain text.

I tried using strip_tags and it worked partially:
"name": "'.strip_tags(apply_filters('the_content', $faq['question'])).'",

partially because I was able to remove the tags with this “solution”, but in contrast the JSON became invalid because added blank space and line breaks. Validation error message from JSONLint:
Expecting ‘STRING’, ‘NUMBER’, ‘NULL’, ‘TRUE’, ‘FALSE’, ‘{‘, ‘[‘, got ‘undefined’

this is the complete function code:

     public function render_schema()
{
  $faqs = $this->render_faqs();
  
    if (!$faqs)
    {
        return;
    }
    $schema="
    <script type="application/ld+json">
    {
        "@context": "https://schema.org",
        "@type": "FAQPage",
        "mainEntity": [";

    $comma="";

    foreach ($faqs as $faq)
      
    {
        $schema .= ' 
                ' . $comma . ' {
                    "@type": "Question",
                    "name": '.json_encode(apply_filters('the_content', $faq['question'])).',
                    
                    "acceptedAnswer": {
                        "@type": "Answer",
                        "text": ' . json_encode(apply_filters('the_content', $faq['reply'])) . '
                    }
                }
                ';

        $comma=",";

    }
    $schema .= '
        ]
    }
    </script>';

    return $schema;
}

0
user3416348 2 months 2021-04-24T07:39:40-05:00 0 Answers 0 views 0

Leave an answer

Browse
Browse