How to add Microdata attributes in PHP

I use Microdata attributes in HTML code.

How can I add Microdata attributes to the following tag (PHP)?

if (!empty($speciality)) {
echo "<p><strong>" . __('Speciality', 'framework') . "</strong><span>" . $speciality . "</span></p>";}

The page will not load when I enter the following way :

echo "<p itemscope itemtype="https://schema.org/medicalSpecialty"><strong>" . __('Speciality', 'framework') . "</strong><span itemprop="medicalSpecialty" >" . $speciality . "</span></p>";

2 answers

  • answered 2017-08-12 09:46 Sunil Rajput

    you are using double quotes in a double quotes which is wrong, use backslash or single quote when you need. Try This code

    echo "<p itemscope itemtype='https://schema.org/medicalSpecialty'><strong>" . __('Speciality', 'framework') . "</strong><span itemprop='medicalSpecialty' >" . $speciality . "</span></p>";
    

  • answered 2017-08-12 11:09 Simon Williams

    You're mixing single and double quotes but you need to reserve either single or double quotes to escape the string so that your PHP isn't read as HTML when echoed. The below snippet will work without any errors:

    if (!empty($speciality)) {    
    echo "<p itemscope itemtype='https://schema.org/medicalSpecialty'><strong>'". 
    __('Speciality', 'framework') . "'</strong><span itemprop='medicalSpecialty' 
    >'" . $speciality . "'</span></p>";
    }