• Resolved Group Of Oceninfo

    (@princebhalani143)


    [Moderator Note: moved to Hacks Forum. The best forum for custom code questions]

    I have some image thumb code like below on my website coming from third party website to my website

    <div class="thumb"><img src="//www.abc.com/website/thumbs/461/beautifulimage/test1a.jpg" rel="//www.abc.com/website/thumbs/461/beautifulimage/test1a.jpg"></div>

    I want to replace URL from
    //www.abc.com/website/thumbs/461
    to
    //www.abc.com/website/images
    So, I can replace URL on page load so it’ll load code something like below

    <div class="thumb"><img src="//www.abc.com/website/images/beautifulimage/test1a.jpg" rel="//www.abc.com/website/images/beautifulimage/test1a.jpg"></div>

    I find below script to make it work but it’s not working on wordpress

    jQuery(document).ready(function($){
        $('.thumb img').each(function(index){
           var urlpath=$(this).attr('src');
           urlpath=urlpath.replace("/thumbs/461/", "/images/");
           $(this).attr('src',urlpath);
        });
    });

    Do you know guys how I can run this code after complete gadget load and then it’ll change all the URL??

    • This topic was modified 7 years, 4 months ago by Group Of Oceninfo.
    • This topic was modified 7 years, 4 months ago by Group Of Oceninfo.
    • This topic was modified 7 years, 4 months ago by bcworkz. Reason: moved to hacks
Viewing 3 replies - 1 through 3 (of 3 total)
  • Moderator bcworkz

    (@bcworkz)

    Are you loading the jQuery library? The correct way is to use wp_enqueue_script('jquery'); Code Reference

    Verify that other JS errors are not occurring by checking your browser console. Any errors occurring before your script will prevent it from running. If that all checks out, it’s time for some detailed debugging of your script. It looks OK to me, but details are easy to miss. They’ll show up in debugging work.

    Thread Starter Group Of Oceninfo

    (@princebhalani143)

    I have checked that all things but still result is same.

    function wpdocs_scripts_method() {
        wp_enqueue_script( 'custom-script', get_stylesheet_directory_uri() . '/changeimage.js', array( 'jquery' ) );
    }
    add_action( 'wp_enqueue_scripts', 'wpdocs_scripts_method' );

    I have placed above code in my function.php and that script under changeimage.js

    Moderator bcworkz

    (@bcworkz)

    That looks good, but check the resulting path to your script file anyway. It’s surprisingly easy to get this wrong. I.e. look at your page’s HTML source in your browser and locate the script tag in <head> that loads your external JS file and be sure the path is correct.

    You can verify jQuery is loaded too, though there’s no reason I see for it to not to be. It’ll be a parameter in a call to a dedicated load-scripts.php file, also inside a script tag.

    How are these URLs you’re altering getting on the page in the first place? Inserted by some JS code perhaps? If so, your code needs to run after this to work. If it’s in page content or part of some PHP script, is there any possibility of altering the URL server side?

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘How to rewrite some part of the URL?’ is closed to new replies.