Home › Forums › Product Support Forums › Ajax Search Pro for WordPress Support › I need to access the current page URL
- This topic has 2 replies, 2 voices, and was last updated 3 years, 12 months ago by
leroile.
-
AuthorPosts
-
June 8, 2022 at 8:06 pm #37947
leroile
ParticipantHello,
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 #37960Ernest Marcinko
KeymasterHi 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; }June 9, 2022 at 5:07 pm #37974leroile
ParticipantThat’s very clever, thank you 👍
-
AuthorPosts
- You must be logged in to reply to this topic.