WordPress.org

Ready to get started?Download WordPress

Forums

php code for removing query string (32 posts)

  1. charlietech
    Member
    Posted 1 year ago #

    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?

  2. vtxyzzy
    Member
    Posted 1 year ago #

    Please explain more. Can you give an example of what you want to remove and tell when you want to remove it?

  3. charlietech
    Member
    Posted 1 year ago #

    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

  4. vtxyzzy
    Member
    Posted 1 year ago #

    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?

  5. charlietech
    Member
    Posted 1 year ago #

    http://cdn.xertionfitness.com/wp-content/themes/suffusion/scripts/suffusion.js?ver=4.3.1

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

  6. charlietech
    Member
    Posted 1 year ago #

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

  7. vtxyzzy
    Member
    Posted 1 year ago #

    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?

  8. vtxyzzy
    Member
    Posted 1 year ago #

    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.

  9. charlietech
    Member
    Posted 1 year ago #

    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.

  10. vtxyzzy
    Member
    Posted 1 year ago #

    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.

  11. charlietech
    Member
    Posted 1 year ago #

    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?

  12. vtxyzzy
    Member
    Posted 1 year ago #

    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.

  13. charlietech
    Member
    Posted 1 year ago #

    OK what would be another way to remove query string?

  14. vtxyzzy
    Member
    Posted 1 year ago #

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

  15. charlietech
    Member
    Posted 1 year ago #

    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);
    }

  16. vjpo
    Member
    Posted 1 year ago #

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

  17. charlietech
    Member
    Posted 1 year ago #

    thanks vjpo

    I dont know if the code is working because my pingdom query score didnt rise

  18. vtxyzzy
    Member
    Posted 1 year ago #

    Did you ever verify that the function is being called? If it is not called, it doesn't matter what the code is.

  19. charlietech
    Member
    Posted 1 year ago #

    exit("Function returns $parts[0]");

  20. charlietech
    Member
    Posted 1 year ago #

    If so I did put that code in and nothing happen once again
    here's is what I put in

    function hide_wp_version($src) {
    global $wp_version;
    exit("Function returns $parts[0]");
    return str_replace("?ver=$wp_version", '', $src);
    }
    add_filter('script_loader_src', 'hide_wp_version');
    add_filter('style_loader_src', 'hide_wp_version');

  21. vjpo
    Member
    Posted 1 year ago #

    The code is works for scripts and styles those added by wp_enqueue_script and wp_enqueue_style functions with not specified $ver - script version number.
    http://codex.wordpress.org/Function_Reference/wp_enqueue_script

  22. charlietech
    Member
    Posted 1 year ago #

    Well I need it to remove ?ver etc off my querystring, How do i do that with the code? as vtxzy was saying how to get the call function to the code above?

  23. vjpo
    Member
    Posted 1 year ago #

    Okay. I am not a regex specialist but it stripes for me all ?ver=x.x.x of enqueued scripts and styles.

    function hide_version($src) {
        return preg_replace('/\?ver=[.0-9]*/', '', $src);
    }
    add_filter('script_loader_src', 'hide_version');
    add_filter('style_loader_src', 'hide_version');
  24. charlietech
    Member
    Posted 1 year ago #

    Hey thanks for the response, so i put this code at the bottom of my function.php?

  25. vjpo
    Member
    Posted 1 year ago #

    Yep, functions.php

  26. charlietech
    Member
    Posted 1 year ago #

    Ok i put the code in the funcion php and it didnt remove the querystring, at least in my yslow or gt metrix

  27. charlietech
    Member
    Posted 1 year ago #

    Is there any codes i should have in my htaccess file or i need to remove in order for the code to work correctly?

  28. MickeyRoush
    Member
    Posted 1 year ago #

    Try this instead. Goes in your theme's (child theme's) functions.php

    // Remove WP version number from js and css files.
    function remove_cssjs_ver( $src ) {
        if( strpos( $src, '?ver=' ) )
            $src = remove_query_arg( 'ver', $src );
        return $src;
    }
    add_filter( 'style_loader_src', 'remove_cssjs_ver', 10, 2 );
    add_filter( 'script_loader_src', 'remove_cssjs_ver', 10, 2 );
  29. charlietech
    Member
    Posted 1 year ago #

    does it matter where i put it? i put it at the bottom and I dont think it did anything.

  30. vjpo
    Member
    Posted 1 year ago #

    For me all 3 snippets work. The very first, that charlietech posted in this topic. 2 - mine, and 3 - MickeyRoush's.

    charlietech, did you clear the cache to see the changes? Do you use caching plugins or, may be something else ?

    And again, all 3 snippets work only with scripts, those added by wp_enqueue_script and wp_enqueue_style functions.

Topic Closed

This topic has been closed to new replies.

About this Topic

Tags

No tags yet.