I need to access the current page URL

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 leroile 2 months ago.

Viewing 3 posts - 1 through 3 (of 3 total)
  • Author
    Posts
  • #37947
    leroile
    leroile
    Participant

    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

    #37960
    Ernest Marcinko
    Ernest Marcinko
    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;
    }
    Best,
    Ernest Marcinko

    If you like my products, don't forget to rate them on codecanyon :)


    #37974
    leroile
    leroile
    Participant

    That’s very clever, thank you 👍

Viewing 3 posts - 1 through 3 (of 3 total)

You must be logged in to reply to this topic.