0

I'm using wordpress and I want to change the logo on some pages. Since the theme I'm using (flatsome) doesn't support this I thought using php would be a good idea. I'm not sure how to do it though.

I tried this:

<?php
function change_logo_on_single($html) {

   if(is_single( array(1441, 1425, 1501, 1494, 1498, 1503))){
      $html = preg_replace('<a(.*?)><img(.*?)><img(.*?)></a>', 
'<a href="https://example.com/" title="" rel="home">
        <img width="146" height="150" src="https://example.com/wp-content/uploads/2021/02/Logo.png" class="header_logo header-logo" alt="">
        <img width="146" height="150" src="https://example.com/wp-content/uploads/2021/02/Logo.png" class="header-logo-dark" alt="">
</a>', $html);
   }

   return $html;
}

add_filter('get_custom_logo','change_logo_on_single');
?>

I think I used the wrong pattern in preg_replace. Can someone suggest a way to do it?

Meeps
  • 11
  • Add a `~` / tilde delimiter at the beginning and the end of your pattern, like so: '~~ . That should work. More info here: https://stackoverflow.com/a/20705419/ – FiddlingAway Dec 17 '22 at 14:18
  • Sadly `~~` doesn't work either. The annoying thing is that I don't get errors from wordpress. It says the file has been saved but it doesn't actually do anything on the page. Do you see what else could cause this problem? – Meeps Dec 21 '22 at 18:34
  • If you have any newlines in your rendered html, that might break it (this in particular: `.*`). I think you might have to use `[\s\S]`, as well. Try this regex: https://regex101.com/r/fdmpRl/1 – FiddlingAway Dec 21 '22 at 19:08
  • And if that fails (as it seems to in my repl.it tests), try this: `~]*>[^<]*(]*>[^<]*){2}~`. This passes in my repl.it tests, so it might work with what you have. This one targets all of the `a` elements which contain exactly 2 `img` within them. That means that it'll work only in those cases, so it might not be what you're looking for, if the number of `img` is dynamic, and is not always set to 2. – FiddlingAway Dec 21 '22 at 19:26

0 Answers0