query - WordPress add_query_arg from ajax and make url friendly

admin2025-01-08  3

In WordPress I have set a query arg in ajax like this

add_action( 'wp_ajax_get_prepare_link', 'prepare_link');

add_action( 'wp_ajax_nopriv_get_prepare_link', 'prepare_link');

function prepare_link() {
    //Do some logic and get the models name
    $url = add_query_arg( array(
            'compare' => aaa-and-bbb-and-ccc,
        ), '/' );
    echo $url;
    wp_die();
}

So after this I have got my url like this

/?compare=aaa-and-bbb-and-ccc

So from jQuery I am doing redirect the page to the response url like this

jQuery('apare-vechicles').click(function(e){
    jQuery.ajax({
        type: "POST",
        url: script.ajax_url,
        data: {
            action : 'get_prepare_link',
        },
        success: function(data){
            window.location = data; //Redirect to the prepared link
        }
    });
});

Now I want to make it as seo friendly url. So the output url should be shown like this


So to achieve this I have made like this

add_action('init', 'custom_rewrite_basic' );

function custom_rewrite_basic() {
    add_rewrite_rule('^compare/([^/]*)/?',
        'index.php?pagename=$matches[1]',
        'top');
}

But its not working at all. So can someone tell me how can I achieve this?

Thanks,

In WordPress I have set a query arg in ajax like this

add_action( 'wp_ajax_get_prepare_link', 'prepare_link');

add_action( 'wp_ajax_nopriv_get_prepare_link', 'prepare_link');

function prepare_link() {
    //Do some logic and get the models name
    $url = add_query_arg( array(
            'compare' => aaa-and-bbb-and-ccc,
        ), 'http://example.com/cars/' );
    echo $url;
    wp_die();
}

So after this I have got my url like this

http://example.com/cars/?compare=aaa-and-bbb-and-ccc

So from jQuery I am doing redirect the page to the response url like this

jQuery('a.compare-vechicles').click(function(e){
    jQuery.ajax({
        type: "POST",
        url: script.ajax_url,
        data: {
            action : 'get_prepare_link',
        },
        success: function(data){
            window.location = data; //Redirect to the prepared link
        }
    });
});

Now I want to make it as seo friendly url. So the output url should be shown like this

http://example.com/cars/compare/aaa-and-bbb-and-ccc

So to achieve this I have made like this

add_action('init', 'custom_rewrite_basic' );

function custom_rewrite_basic() {
    add_rewrite_rule('^compare/([^/]*)/?',
        'index.php?pagename=$matches[1]',
        'top');
}

But its not working at all. So can someone tell me how can I achieve this?

Thanks,

Share Improve this question edited Jul 19, 2019 at 12:07 NewUser asked Jul 19, 2019 at 7:54 NewUserNewUser 1812 gold badges3 silver badges15 bronze badges 1
  • Looking at the format - compare/<slug> - do you actually have a page having the slug <slug> like aaa-and-bbb-and-ccc? Because your rewrite rule actually worked for me. So either you didn't flush the permalinks (just visit the Permalink Settings page) or that the page (aaa-and-bbb-and-ccc) doesn't exist. – Sally CJ Commented Jul 20, 2019 at 13:55
Add a comment  | 

1 Answer 1

Reset to default 0

This add_query_arg function is add argument after URL you have enter.

You have to get pretty URL so add below code in your functions.php file :

function custom_rewrite_rule() {
    add_rewrite_rule('^cars/([^/]*)/([^/]*)/?','index.php?page_id=PAGE_ID&type=$matches[1]&models=$matches[2]','top');
    flush_rewrite_rules();
}
add_action('init', 'custom_rewrite_rule', 10, 0);

function custom_rewrite_tag() {
  add_rewrite_tag('%type%', '([^&]+)');
  add_rewrite_tag('%models%', '([^&]+)');
}
add_action('init', 'custom_rewrite_tag', 10, 0);


add_action( 'wp_ajax_get_prepare_link', 'prepare_link');
add_action( 'wp_ajax_nopriv_get_prepare_link', 'prepare_link');
function prepare_link() {

    $url = "";
    $url .= get_the_permalink( PAGE_ID );
    $url .= "compare/aaa-and-bbb-and-ccc"

    echo $url;
    wp_die();
}

In above code replace "PAGE_ID" with your "car" page id.

转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1736266334a1112.html

最新回复(0)