Create API to get data from WordPress site to external site
Budget: $250 – $750 USD
+ Urgent job, if you can't start now and work fast until complete, please don't bid.
+ If you are interested in the job, let's not waste 5+ hours on a reply to a message, let's reply fast.
I have a new WP website and need some of the data from the DB in another site. For this we need to create a 7 API's. Is this something you can help with? There are already some API's for this on our old site and they look like this:
REGISTER_REST_ROUTE:
register_rest_route('faqs', '/v1(?P<lang>)', array(
'methods' => 'GET',
'callback' => __NAMESPACE__ . '\\api_faqs',
));
FUNCTION api_faqs:
function api_faqs($data)
{
$faqs = Helper\get_api_faqs($data['lang']);
return rest_ensure_response($faqs);
}
FUNCTION get_api_faqs:
function get_api_faqs($lang)
{
global $sitepress;
$sitepress->switch_lang($lang);
$faqs = array();
$args = array(
'post_type' => 'knowledgebase',
'numberposts' => -1,
'order' => 'DESC',
'suppress_filters' => 0,
'post_status' => 'publish'
);
$posts = get_posts($args);
foreach ($posts as $key => $post) {
$api_faq = get_field('api_faq', $post->ID);
if ($api_faq) {
$faqs[] = $post;
}
}
$json_response = [];
foreach ($faqs as $key => $faq) {
$faqTitle = do_shortcode($faq->post_title);
$faqLink = get_the_permalink($faq->ID);
$faqContent = html_entity_decode(preg_replace("/%u([0-9a-f]{3,4})/i", "&#x\\1;", urldecode(do_shortcode($faq->post_content))), ENT_QUOTES, 'UTF-8');
// Remove youtube videos (iframe) from the content
$faqContent = preg_replace('/<iframe.*?\/iframe>/i', '', $faqContent);
// Check if faqContent has links
if (preg_match_all('/<a[^>]+href=([\'"])(?<href>.+?)\1[^>]*>/i', $faqContent, $faqContentLinks)) {
// loop through all links on current faq article
foreach ($faqContentLinks['href'] as $link) {
if (strpos($link, '=')) {
// isolate the id
$id = explode('=', $link)[1];
if (get_the_permalink($id)) {
// replace page id or post id with article's permalink
$faqContent = str_replace($link, get_the_permalink($id), $faqContent);
}
}
}
}
if (preg_match_all('/<img[^>]+src=([\'"])(?<src>.+?)\1[^>]*>/i', $faqContent, $faqContentImages)) {
// loop through all images of the current faq article
foreach ($faqContentImages['src'] as $image) {
$url = parse_url($image);
// Replace the host with the host of the request
$new_url = $url['scheme'] . "://" . $_SERVER["SERVER_NAME"] . $url['path'];
$faqContent = str_replace($image, $new_url, $faqContent);
}
}
$faqContentTextOnly = preg_replace('/(\\r\\n){3,}/', "\r\n", strip_tags($faqContent));
$json_response[] = [
'faqTitle' => $faqTitle,
'faqUrl' => $faqLink,
'faqContent' => $faqContent,
'faqContentTextOnly' => $faqContentTextOnly,
];
}
return $json_response;
}
+ If you are interested in the job, let's not waste 5+ hours on a reply to a message, let's reply fast.
I have a new WP website and need some of the data from the DB in another site. For this we need to create a 7 API's. Is this something you can help with? There are already some API's for this on our old site and they look like this:
REGISTER_REST_ROUTE:
register_rest_route('faqs', '/v1(?P<lang>)', array(
'methods' => 'GET',
'callback' => __NAMESPACE__ . '\\api_faqs',
));
FUNCTION api_faqs:
function api_faqs($data)
{
$faqs = Helper\get_api_faqs($data['lang']);
return rest_ensure_response($faqs);
}
FUNCTION get_api_faqs:
function get_api_faqs($lang)
{
global $sitepress;
$sitepress->switch_lang($lang);
$faqs = array();
$args = array(
'post_type' => 'knowledgebase',
'numberposts' => -1,
'order' => 'DESC',
'suppress_filters' => 0,
'post_status' => 'publish'
);
$posts = get_posts($args);
foreach ($posts as $key => $post) {
$api_faq = get_field('api_faq', $post->ID);
if ($api_faq) {
$faqs[] = $post;
}
}
$json_response = [];
foreach ($faqs as $key => $faq) {
$faqTitle = do_shortcode($faq->post_title);
$faqLink = get_the_permalink($faq->ID);
$faqContent = html_entity_decode(preg_replace("/%u([0-9a-f]{3,4})/i", "&#x\\1;", urldecode(do_shortcode($faq->post_content))), ENT_QUOTES, 'UTF-8');
// Remove youtube videos (iframe) from the content
$faqContent = preg_replace('/<iframe.*?\/iframe>/i', '', $faqContent);
// Check if faqContent has links
if (preg_match_all('/<a[^>]+href=([\'"])(?<href>.+?)\1[^>]*>/i', $faqContent, $faqContentLinks)) {
// loop through all links on current faq article
foreach ($faqContentLinks['href'] as $link) {
if (strpos($link, '=')) {
// isolate the id
$id = explode('=', $link)[1];
if (get_the_permalink($id)) {
// replace page id or post id with article's permalink
$faqContent = str_replace($link, get_the_permalink($id), $faqContent);
}
}
}
}
if (preg_match_all('/<img[^>]+src=([\'"])(?<src>.+?)\1[^>]*>/i', $faqContent, $faqContentImages)) {
// loop through all images of the current faq article
foreach ($faqContentImages['src'] as $image) {
$url = parse_url($image);
// Replace the host with the host of the request
$new_url = $url['scheme'] . "://" . $_SERVER["SERVER_NAME"] . $url['path'];
$faqContent = str_replace($image, $new_url, $faqContent);
}
}
$faqContentTextOnly = preg_replace('/(\\r\\n){3,}/', "\r\n", strip_tags($faqContent));
$json_response[] = [
'faqTitle' => $faqTitle,
'faqUrl' => $faqLink,
'faqContent' => $faqContent,
'faqContentTextOnly' => $faqContentTextOnly,
];
}
return $json_response;
}