Skip to content

Instantly share code, notes, and snippets.

@erfg12
Last active August 11, 2020 18:31
Show Gist options
  • Save erfg12/67550a1ec4caf19fca27190bb487709c to your computer and use it in GitHub Desktop.
Save erfg12/67550a1ec4caf19fca27190bb487709c to your computer and use it in GitHub Desktop.
3rd Party WordPress CORS
<?php
$WPSite = "https://WordPressSite.com"; // CHANGE THIS
// Make CORS accessible
header("Access-Control-Allow-Origin: *");
header("Content-Type: application/json; charset=UTF-8");
error_reporting(E_ALL);
// Pull data via Curl
function GetURLData($url) {
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
$head = curl_exec($ch);
curl_close($ch);
return $head;
}
// Page navigation
$page = 1;
if (isset($_GET['page']))
$page = $_GET['page'];
// Convert XML to JSON
function XMLtoJSON($url) {
$fileContents = GetURLData($url);
$xml = simplexml_load_string($fileContents);
$json = json_encode($xml);
return $json;
}
// WP Search
if (isset($_GET['search'])) {
$safesearch = urlencode($_GET['search']);
print_r(XMLtoJSON($WPSite."/search/$safesearch/page/".$page."?feed=rss"));
} else {
if (isset($_GET['page']))
print_r(XMLtoJSON($WPSite."/feed/?paged=".$page));
else
print_r(XMLtoJSON($WPSite."/feed/"));
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment