fbpx
NeONBRAND Marketing & Business Development Experts
Contact Us
Site Navigation
  • Home
  • SEO
  • Social Media
  • Websites
  • Consulting
  • Contact Us
    • Las Vegas, NV
    • St. George, UT
    • Cedar City, UT
    • Provo, UT
  • Extras
    • About
    • Team
    • Portfolio
    • Website Hosting
    • Blog
    • Ebooks
    • Digital Marketing Dictionary

How to Call Yoast Local SEO Data Using PHP instead of Shortcode

Originally posted: June 20, 2017 by Kenny Eliason. Leave a comment
Scroll to Next
Call Yoast Local SEO Data Using PHP
Get Updates to Your Inbox!
This field is for validation purposes and should be left unchanged.

Home » Search Engine Optimization » Local SEO » How to Call Yoast Local SEO Data Using PHP instead of Shortcode

Reading Time: 3 minutes
  • Twitter
  • Facebook
  • Pinterest
  • LinkedIn

When developing a custom site we’ve realized that the shortcodes provided by the Yoast Local SEO plugin are very limiting. We still like using their data but end up having to pull the data into very specific places on the site. Using the built-in WordPress function get_option, here is how to get the local business data when using a single location (does not work with multiple locations):

Disclaimer: Yoast Probably Outputs the Data Better than You

The major reason for using the Yoast Local SEO plugin is because the output is spot on with all the schema and html markup. If you use the above and don’t take the time to consider the schema and html you really are losing out on a huge reason to use this plugin. That’s the stuff Google reads for Local Rankings. Gotta have it.

Step 1: Call the Local Data from the Options Table

Yoast stores your single location data as a serialized array in the options table for WordPress. In order to retrieve that data, use the following to set a custom variable as an array:

$wpseo_local = get_option('wpseo_local');

Step 2: Specify which data field you want

Since you have already set up the $wpseo_local variable as an array storing all of the Yoast Local SEO data, you can now retrieve that data using any of the following fields:

<?php echo $wpseo_local['location_name']; ?>
<?php echo $wpseo_local['business_type']; ?>
<?php echo $wpseo_local['location_address']; ?>
<?php echo $wpseo_local['location_address_2']; ?>
<?php echo $wpseo_local['location_city']; ?>
<?php echo $wpseo_local['location_state']; ?>
<?php echo $wpseo_local['location_zipcode']; ?>
<?php echo $wpseo_local['location_country']; ?>
<?php echo $wpseo_local['location_phone']; ?>
<?php echo $wpseo_local['location_phone_2nd']; ?>
<?php echo $wpseo_local['location_fax']; ?>
<?php echo $wpseo_local['location_email']; ?>
<?php echo $wpseo_local['location_url']; ?>
<?php echo $wpseo_local['location_vat_id']; ?>
<?php echo $wpseo_local['location_tax_id']; ?>
<?php echo $wpseo_local['location_coc_id']; ?>
<?php echo $wpseo_local['location_price_range']; ?>
<?php echo $wpseo_local['location_coords_lat']; ?>
<?php echo $wpseo_local['location_coords_long']; ?>
<?php echo $wpseo_local['locations_slug']; ?>
<?php echo $wpseo_local['locations_label_singular']; ?>
<?php echo $wpseo_local['locations_label_plural']; ?>
<?php echo $wpseo_local['locations_taxo_slug']; ?>
<?php echo $wpseo_local['business_image']; ?>
<?php echo $wpseo_local['opening_hours_monday_from']; ?>
<?php echo $wpseo_local['opening_hours_monday_to']; ?>
<?php echo $wpseo_local['opening_hours_monday_second_from']; ?>
<?php echo $wpseo_local['opening_hours_monday_second_to']; ?>
<?php echo $wpseo_local['opening_hours_tuesday_from']; ?>
<?php echo $wpseo_local['opening_hours_tuesday_to']; ?>
<?php echo $wpseo_local['opening_hours_tuesday_second_from']; ?>
<?php echo $wpseo_local['opening_hours_tuesday_second_to']; ?>
<?php echo $wpseo_local['opening_hours_wednesday_from']; ?>
<?php echo $wpseo_local['opening_hours_wednesday_to']; ?>
<?php echo $wpseo_local['opening_hours_wednesday_second_from']; ?>
<?php echo $wpseo_local['opening_hours_wednesday_second_to']; ?>
<?php echo $wpseo_local['opening_hours_thursday_from']; ?>
<?php echo $wpseo_local['opening_hours_thursday_to']; ?>
<?php echo $wpseo_local['opening_hours_thursday_second_from']; ?>
<?php echo $wpseo_local['opening_hours_thursday_second_to']; ?>
<?php echo $wpseo_local['opening_hours_friday_from']; ?>
<?php echo $wpseo_local['opening_hours_friday_to']; ?>
<?php echo $wpseo_local['opening_hours_friday_second_from']; ?>
<?php echo $wpseo_local['opening_hours_friday_second_to']; ?>
<?php echo $wpseo_local['opening_hours_saturday_from']; ?>
<?php echo $wpseo_local['opening_hours_saturday_to']; ?>
<?php echo $wpseo_local['opening_hours_saturday_second_from']; ?>
<?php echo $wpseo_local['opening_hours_saturday_second_to']; ?>
<?php echo $wpseo_local['opening_hours_sunday_from']; ?>
<?php echo $wpseo_local['opening_hours_sunday_to']; ?>
<?php echo $wpseo_local['opening_hours_sunday_second_from']; ?>
<?php echo $wpseo_local['opening_hours_sunday_second_to']; ?>
<?php echo $wpseo_local['sl_num_results']; ?>
<?php echo $wpseo_local['unit_system']; ?>
<?php echo $wpseo_local['map_view_style']; ?>
<?php echo $wpseo_local['address_format']; ?>
<?php echo $wpseo_local['default_country']; ?>
<?php echo $wpseo_local['show_route_label']; ?>
<?php echo $wpseo_local['custom_marker']; ?>
<?php echo $wpseo_local['api_key_browser']; ?>
<?php echo $wpseo_local['api_key']; ?>
<?php echo $wpseo_local['location_taxo_slug']; ?>
<?php echo $wpseo_local['location_timezone']; ?>

Step 3: Changing Opening Hours from Military time to 12 hour

The opening hours are stored in military time. In order to convert them to a 12 hour format, use the following on any of the time variables:

<?php echo date("g:i a", strtotime($wpseo_local['opening_hours_sunday_from'])); ?>

Step 4: Removing Non-numeric Characters from Phone Number in Href

Most people are familiar with using the anchor tag for a phone number, the typical format is as follows:

<a href="tel:7027066366">(702) 706-6366</a>

The issue is that when you call in the number from the Yoast field, it will contain whatever non-numeric characters you have entered into the field for appearance purposes. You definitely want to leave that data in there, and you woudn’t want to have a secondary field. If you plug that number straight into the href of the anchor tag your site will not pass validation. It will look like this:

<a href="tel:(702) 706-6366">(702) 706-6366</a>

Those parenthesis, spaces and hyphens are the issue. In order to remove them, use this simple preg_replace:

<?php echo preg_replace("/[^0-9,.]/", "", $wpseo_local['location_phone']); ?>

Step 5: Protecting Email Addresses from Spam

One further step to take is to use the built in WordPress function antispambot to protect your email addresses from spam bots. It’s a really simple one to implement, here’s what it looks like:

<?php echo antispambot($wpseo_local['location_email']); ?>
  • Twitter
  • Facebook
  • Pinterest
  • LinkedIn
Published: June 20, 2017
Updated: June 23, 2020
Headline: How to Call Yoast Local SEO Data Using PHP instead of Shortcode
Image: Call Yoast Local SEO Data Using PHP Height: Width:

Image: Yoast Local SEO Plugin Height: Width:

Publisher: NeONBRAND https://neonbrand.com
NeONBRAND https://neonbrand.com/app/themes/neonbrand/dist/images/logo-gray_280c67fe.png 98 120

« How Much Does It Cost to Make an App?
What You Should Know About Marketing & Social Media Agencies »

2 responses to “How to Call Yoast Local SEO Data Using PHP instead of Shortcode”

  1. Eric Eric says:
    October 2, 2019 at 10:31 pm

    This is great. How do you make this work for multiple locations?

    Reply
    • Kenny Eliason Kenny Eliason says:
      October 5, 2019 at 8:42 pm

      That’s a great question. All the data actually moves to the postmeta table and is reachable by using the get_post_meta function. For example, if you’ve enabled multiple locations and have added a location with the post ID of 111, you could reach all the local data in the following manner:


      get_post_meta(111, 'location_name', true);
      get_post_meta(111, 'business_type', true);
      get_post_meta(111, 'location_address', true);
      get_post_meta(111, 'location_address_2', true);
      get_post_meta(111, 'location_city', true);
      get_post_meta(111, 'location_state', true);
      get_post_meta(111, 'location_zipcode', true);
      get_post_meta(111, 'location_country', true);
      get_post_meta(111, 'location_phone', true);
      get_post_meta(111, 'location_phone_2nd', true);
      get_post_meta(111, 'location_fax', true);
      get_post_meta(111, 'location_email', true);
      get_post_meta(111, 'location_url', true);
      get_post_meta(111, 'location_vat_id', true);
      get_post_meta(111, 'location_tax_id', true);
      get_post_meta(111, 'location_coc_id', true);
      get_post_meta(111, 'location_price_range', true);
      get_post_meta(111, 'location_coords_lat', true);
      get_post_meta(111, 'location_coords_long', true);
      get_post_meta(111, 'locations_slug', true);
      get_post_meta(111, 'locations_label_singular', true);
      get_post_meta(111, 'locations_label_plural', true);
      get_post_meta(111, 'locations_taxo_slug', true);
      get_post_meta(111, 'business_image', true);
      get_post_meta(111, 'opening_hours_monday_from', true);
      get_post_meta(111, 'opening_hours_monday_to', true);
      get_post_meta(111, 'opening_hours_monday_second_from', true);
      get_post_meta(111, 'opening_hours_monday_second_to', true);
      get_post_meta(111, 'opening_hours_tuesday_from', true);
      get_post_meta(111, 'opening_hours_tuesday_to', true);
      get_post_meta(111, 'opening_hours_tuesday_second_from', true);
      get_post_meta(111, 'opening_hours_tuesday_second_to', true);
      get_post_meta(111, 'opening_hours_wednesday_from', true);
      get_post_meta(111, 'opening_hours_wednesday_to', true);
      get_post_meta(111, 'opening_hours_wednesday_second_from', true);
      get_post_meta(111, 'opening_hours_wednesday_second_to', true);
      get_post_meta(111, 'opening_hours_thursday_from', true);
      get_post_meta(111, 'opening_hours_thursday_to', true);
      get_post_meta(111, 'opening_hours_thursday_second_from', true);
      get_post_meta(111, 'opening_hours_thursday_second_to', true);
      get_post_meta(111, 'opening_hours_friday_from', true);
      get_post_meta(111, 'opening_hours_friday_to', true);
      get_post_meta(111, 'opening_hours_friday_second_from', true);
      get_post_meta(111, 'opening_hours_friday_second_to', true);
      get_post_meta(111, 'opening_hours_saturday_from', true);
      get_post_meta(111, 'opening_hours_saturday_to', true);
      get_post_meta(111, 'opening_hours_saturday_second_from', true);
      get_post_meta(111, 'opening_hours_saturday_second_to', true);
      get_post_meta(111, 'opening_hours_sunday_from', true);
      get_post_meta(111, 'opening_hours_sunday_to', true);
      get_post_meta(111, 'opening_hours_sunday_second_from', true);
      get_post_meta(111, 'opening_hours_sunday_second_to', true);
      get_post_meta(111, 'sl_num_results', true);
      get_post_meta(111, 'unit_system', true);
      get_post_meta(111, 'map_view_style', true);
      get_post_meta(111, 'address_format', true);
      get_post_meta(111, 'default_country', true);
      get_post_meta(111, 'show_route_label', true);
      get_post_meta(111, 'custom_marker', true);
      get_post_meta(111, 'api_key_browser', true);
      get_post_meta(111, 'api_key', true);
      get_post_meta(111, 'location_taxo_slug', true);
      get_post_meta(111, 'location_timezone', true);

      If you’re on the single location page and want to call the data, just swap out 111 with $post->ID instead and it should load the data correctly.

      Great question for real.

      Reply

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

About the Author
Kenny Eliason
Kenny Eliason
Having grown up the son of a marketer, the skills of the trade are pumping through his blood. When you couple that with his programming and computer knowledge, you get an explosive combination. Kenny has been an avid digital marketer for over 9 years now, often being the first to recognize the hottest trends coming to the market. In his free time, Kenny loves downhill mountain biking. He calls it his "old man sport" since BMX was what he did as a teenager and it's not quite as easy to ride those little bikes anymore. Kenny is also a huge technology enthusiast, specifically when it comes to Apple products - did someone say, fanboy? Those close to him are often asking for help solving tech-related problems which often results with them saying, "man, you can fix anything!"
Search
  • Home
  • Search Engine Optimization
  • Social Media
  • Website Design
  • Business Consulting
  • Digital Marketing
  • Video Marketing
  • Email Marketing
  • WooCommerce Development
  • Traditional Advertising
  • Retail Marketing
  • Dentist Marketing
  • Hotel Marketing
  • Medical Marketing
NeONBRAND Newsletter
Don't live with FOMO. Get subscribed to our newsletter and never worry about missing the awesomeness ever again.

"*" indicates required fields

This field is for validation purposes and should be left unchanged.

(702) 706-NeON
  • Las Vegas, NV
  • St. George, UT
  • Cedar City, UT
  • Provo, UT
  • NW Las Vegas, NV
  • Sitemap
  • © 2023 NeONBRAND. All Rights Reserved.