Home › Forums › Product Support Forums › Ajax Search Pro for WordPress Support › I need to access the current page URL
This topic contains 2 replies, has 2 voices, and was last updated by leroile 1 year, 10 months ago.
- AuthorPosts
- June 8, 2022 at 8:06 pm #37947
Hello,
I have just bought the $25 support to ask you the following question: how can I retrieve the current URL in asp_custom_link_results filter (URL where is the user is typing in the search bar)?
Here is why it is so crucial for me: I have a website where people can search for jobs. Let’s say that they search for jobs in Houston, they are redirected to /search/l-houston-tx/ (nice). But now, let’s say that they are on this page and they are looking for dog walking jobs in Houston, if they type dog walking in the search bar, I need to get access to the current URL to redirect user to the page /search/l-houston-tx-q-dog+walking/
Here is the current code I have, in the following code the function $_SERVER[‘REQUEST_URI’]; does not work because the function is inside AJAX call … I need to have an alternative, it would be great if the URL was inside the $args so that I can access it.
add_filter( ‘asp_results’, ‘asp_custom_link_results’, 10, 1 );
function asp_custom_link_results( $results, $id, $is_ajax, $args ) {
$current = ‘https://salarship.com’ . $_SERVER[‘REQUEST_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;
}I really hope we can find a solution for this.
Best, Nathan
June 9, 2022 at 10:14 am #37960Hi 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:
Best,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; }
Ernest Marcinko
If you like my products, don't forget to rate them on codecanyon :)
June 9, 2022 at 5:07 pm #37974That’s very clever, thank you đź‘Ť
- AuthorPosts
You must be logged in to reply to this topic.