// USER IP
function get_the_user_ip_custom() {
if (!empty($_SERVER['HTTP_CLIENT_IP'])) {
return $_SERVER['HTTP_CLIENT_IP'];
} elseif (!empty($_SERVER['HTTP_X_FORWARDED_FOR'])) {
return $_SERVER['HTTP_X_FORWARDED_FOR'];
} else {
return $_SERVER['REMOTE_ADDR'];
}
}
// JOBS PER PAGE
$per_page = 10;
// CURRENT PAGE
$current_page = max(
1,
get_query_var('paged'),
get_query_var('page')
);
// REMOVE jobs FROM TITLE
$keyword = preg_replace('/\bjobs\b/i', '', $page_title);
$keyword = trim($keyword);
// WHATJOBS API
$api_url = 'https://api.whatjobs.com/api/v1/jobs.xml?' .
'publisher=4861' .
'&user_ip=' . get_the_user_ip_custom() .
'&user_agent=' . urlencode($_SERVER['HTTP_USER_AGENT']) .
'&keyword=' . urlencode($keyword) .
'&limit=' . $per_page .
'&page=' . $current_page;
// FETCH API
$response = wp_remote_get($api_url);
// CHECK RESPONSE
if (is_array($response) && !is_wp_error($response)) {
$body = wp_remote_retrieve_body($response);
$xml = simplexml_load_string($body);
if ($xml) {
$total_pages = intval($xml->last_page);
foreach ($xml->data->job as $job) {
preg_match('/(\d+)/', parse_url($job->url, PHP_URL_PATH), $matches);
if (isset($matches[1])) {
$job_id = $matches[1];
// STATIC HTML URL
$job_page_url = home_url('/job-' . $job_id . '.html');
// SAVE FILE IN ROOT
$file = ABSPATH . 'job-' . $job_id . '.html';
// SHORT DESCRIPTION
$short_description = wp_strip_all_tags($job->snippet);
// HTML PAGE
$html = '
' . esc_html($job->title) . '
' . esc_html($job->title) . '
Company: ' . esc_html($job->company) . '
Location: ' . esc_html($job->location) . '
Posted: ' . esc_html($job->age) . '
' . $job->snippet . '
Apply Now
';
// SAVE HTML
file_put_contents($file, $html);
// SHORT SNIPPET
$job_snippet = wp_strip_all_tags(
substr($job->snippet, 0, 250)
);
// JOB CARD
echo '
';
echo '
';
echo '
';
echo '
Company: ' .
esc_html($job->company) .
'
';
echo '
Location: ' .
esc_html($job->location) .
'
';
echo '
' . esc_html($job_snippet) . '...
';
echo '
Posted: ';
echo (
((int)$job->age_days === 0)
? 'Today'
: (
((int)$job->age_days === 1)
? '1 day ago'
: esc_html($job->age_days . ' days ago')
)
);
echo '
';
echo '
';
echo 'Read More';
echo 'Apply Now';
echo '
';
echo '
';
echo '
';
}
}
// PAGINATION
echo '';
} else {
echo 'Error parsing XML.';
}
} else {
$error_message = $response->get_error_message();
echo 'Error fetching jobs: ' . $error_message;
}
get_template_part('template-parts/footer-menus-widgets');
get_footer();
?>