How to onmouseover swap image in php?

I have this code in my wordpress template

<a onclick="get_project_data(<?php echo get_the_ID(); ?>)"><img src="<?php echo wp_get_attachment_url( $logoimg_id ); ?>" onmouseover="this.src='<?php echo wp_get_attachment_url( $logoimg_id+b ); ?>';" onmouseout="this.src='<?php echo wp_get_attachment_url( $logoimg_id ); ?>';"/></a>

But it doesn’t work, I need to show a full colour image and a black and white with on over, the thing is that I need to do this automatically so I will upload to my server photo.jpg and photob.jpg (black and white) css is not an option.

Read More

Thank you

Related posts

Leave a Reply

2 comments

  1. First of all, you are not concatenating the strings correctly.

    $logoimg_id+'b' (notice the quotes)
    

    Secondly, you need to add the ‘b’ before the extension. You can do that by

    $imgPath = wp_get_attachment_url( $logoimg_id );
    $tmp = explode('.', $imgPath);
    $tmp[0].='b';
    $imgPath = implode('.',$tmp);
    

    Another way would be to do a

    echo str_replace('.jpg', 'b.jpg', wp_get_attachment_url( $logoimg_id ));
    

    But I would not advise it

  2. You have escaped the single quote at the start of the ‘this.src’ definition but not at the end. the escaping is not required.
    eg.

    onmouseover="this.src='<php
    echo wp_get_attachment_url($logoimg_id+b);?>';"

    should be:

    onmouseover="this.src='<php
    echo wp_get_attachment_url($logoimg_id+b);?>';"