Home › Forums › Product Support Forums › Ajax Search Pro for WordPress Support › I need to access the current page URL › Reply To: I need to access the current page URL
June 9, 2022 at 10:14 am
#37960
Keymaster
Hi Nathan,
The only way to do that, is by generating an input field whenever the page is rendered, which contains the current URL. That is then passed to the options, which is then accessible via the ajax call as well. Something like this:
add_action('asp_layout_in_form', 'asp_layout_in_form_add_uri');
function asp_layout_in_form_add_uri() {
$actual_link = (isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] === 'on' ? "https" : "http") . "://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]";
$actual_link = esc_attr($actual_link);
echo "<input type='hidden' name='current_uri' value='$actual_link'>";
}
add_filter( 'asp_results', 'asp_custom_link_results', 10, 4 );
function asp_custom_link_results( $results, $id, $is_ajax, $args ) {
parse_str($_POST["options"], $options);
$currenturl = $options['current_uri'];
if( issearchqueried() ){
foreach ($results as $k=>&$r) {
$currentid = $r->id;
$cityname = get_post_meta($currentid, 'wpcf-location-name', true);
$statename = get_post_meta($currentid, 'wpcf-abreviated-state-name', true);
$r->link = $currenturl . 'q-' . 'dog-walking';
}
}
return $results;
}