Create Schema in WordPress with PHP

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=""; echo $schema; /*SCHEMA TYPE PRODUCT */ $schema=""; echo $schema; 
asked Apr 3, 2019 at 19:02 Lisa Baird Lisa Baird 21 2 2 bronze badges

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.

Commented Apr 3, 2019 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!

Commented Apr 4, 2019 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.