Support » How-To and Troubleshooting » php code for removing query string

php code for removing query string

  • I need a php code that actually work for removing query string. I have read articles and applied a couple methods but they didnt work. Can someone help me?

Viewing 15 replies - 1 through 15 (of 31 total)
  • Please explain more. Can you give an example of what you want to remove and tell when you want to remove it?

    I have about 25-30 string that need to be remove example 8d9si?

    I have tried this code but its not improving my gt metrix or pingdom score

    function _remove_script_version( $src ){
    $parts = explode( ‘?’, $src );
    return $parts[0];
    add_filter( ‘script_loader_src’, ‘_remove_script_version’, 15, 1 );
    add_filter( ‘style_loader_src’, ‘_remove_script_version’, 15, 1 );

    I put it in the fucntion php

    Please show a couple of examples of the full strings and what you want to remove from each one.

    Have you tested to see if your filter function is actually getting called?


    PIngdom say the resources with ? is not being cache which is 25 of them

    And no I haven’t tested them out, dont know how.

    So you want to remove everything from the question mark to the end of the string?

    It looks like your filter function should do that. Are you sure the function is getting called?

    One way to quickly test is to add an ‘exit’ statement to the code:

    function _remove_script_version( $src ){
       $parts = explode( '?', $src );
       exit("Function returns $parts[0]");
       return $parts[0];

    This will kill the script execution and print the message if the function is called.

    Kill the script far as make the site white and have an error?

    If it didnt do that, then it works? if so, my pingdom and yslow grade havent change.

    If you got the normal page display, your function is not called.

    If the function is called, all you should see on the screen is the message.

    Ok when it show the message, i would take out the exit line code correct? What do i need to do to make it called?

    I cannot tell you how to have the script called. I am not familiar with the filters that you have tried and they are not listed in the WP Filter Reference.

    OK what would be another way to remove query string?

    I can’t answer that either since I do not know where the string was created.

    what about this code

    function remove_querystring_var($url, $key) {
    $url = preg_replace(‘/(.*)(?|&)’ . $key . ‘=[^&]+?(&)(.*)/i’, ‘$1$2$4’, $url . ‘&’);
    $url = substr($url, 0, -1);
    return ($url);

    There is snippet. It removes WP version like ?ver=3.4.2 from the end of enqueued scripts and styles links.

Viewing 15 replies - 1 through 15 (of 31 total)
  • The topic ‘php code for removing query string’ is closed to new replies.