0

I have a website that gets fed data from a local MLS. I put together PHP to grab the title of the page which contains the address & MLS # of the current property, and create schema for it. I also collect some information from meta and compase the image url based on known information. However, when I use the code below, my site becomes slow and starts to crash. I have put the script below just above the ending body tag, below wp_footer(). I would feel better about myself if the problem was something complex, but it is probably something dumb. Thank you in advance.

        $path="http://" . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];//gets url of current page
$tags = get_meta_tags($path); //retrieves an array of all meta name tags
$description= $tags['og:description']; // tag containing the property descriion
        $the_address= explode(",",ucwords(strtolower(get_the_title())) ); //get post title, parse address parts
        $street=$the_address[0];
        $city =$the_address[1];
        $add2 = explode ("(", $the_address[2]);
        $state = strtoupper(substr($the_address[2],1,2));
        $zip = substr($the_address[2],4,5);
        $mls= substr($the_address[2],17,8); echo $mls;
$img_url = "https://images.marketleader.com/HouseImages/MFRMLS/".substr($mls,5,3). "/f_". $mls. ".jpg";
$price="";
/*SCHEMA TYPE RESIDENCE */
        $schema="<script type=\"application/ld+json\">{\"@context\": \"http://schema.org\",";
    $schema.="\"@type\": \"Residence\",
                         \"name\":\"Home for sale in $city, $state\",";
    $schema.="\"address\": {
              \"@type\": \"PostalAddress\",
              \"addressLocality\": \"$city\",
             \"addressRegion\": \"$state\",
              \"postalCode\": \"$zip\",
              \"streetAddress\": \"$street\"
                },
            \"image\": \"$img_url\",
            \"description\": \"$description\"
            }";
        $schema .= "</script>";
        echo $schema;

/*SCHEMA TYPE PRODUCT */
        $schema="<script type=\"application/ld+json\">{\"@context\": \"http://schema.org\",";
    $schema.="\"@type\": \"Product\",
                         \"name\":\"Home for sale in $city, $state\",";
$schema.= "offers\":{
        \"@type\":\"Offer\",
        \"@context\":\"http://schema.org\",
        \"price\":".$price.",
        \"priceCurrency\":\"USD\",
        \"availability\":\"http://schema.org/InStock\",
        \"url\":\"".$path."\"},";
    $schema.="\"name\": {" .$street;
             "\"@type\": \"PostalAddress\",
              \"addressLocality\": \"$city\",
             \"addressRegion\": \"$state\",
              \"postalCode\": \"$zip\",
              \"streetAddress\": \"\"
                },
            \"image\": \"$img_url\",
            \"description\": \"$description\"
            }";
        $schema .= "</script>";
        echo $schema;
  • 1
    Instead of get_meta_tags() which contacts your own web server as if it were a different server, try get_post_meta() which should pull from the initial query. You will want to run this inside the Loop so it gets the postmeta of the currently displayed post. – WebElaine Apr 3 at 19:27
  • I don't have a custom post template on this site; is there a way to put this procedure into a function on functions.php? I know how to put it there, just not sure on how to make it run when desired. Thanks! – Lisa Baird Apr 4 at 19:54
  • You could try hooking a function in functions.php to the wp_footer() hook, which would insert your code at the bottom of the page. You would want to add global $post to try to obtain the post object since this would be outside the Loop. – WebElaine Apr 4 at 20:27

Your Answer

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

Browse other questions tagged or ask your own question.