• Resolved pilardcv1

    (@pilardcv1)


    Hi, I need to dynamically replace the tag with the <amp-img> tag.

    For example, replace this:

    with:

    <amp-img layout=”fixed” class=”fixed alignnone wp-image-5369 i-amphtml-element i-amphtml-layout-fixed i-amphtml-layout-size-defined i-amphtml-layout” src=”image.png” alt=”” width=”24″ height=”24″ i-amphtml-layout=”fixed” style=”width: 24px; height: 24px;”> </amp-img>

Viewing 8 replies - 1 through 8 (of 8 total)
  • Plugin Author Rui Guerreiro

    (@takanakui)

    Hi @pilardcv1

    Try using str_replace inside the menu-image.php around line 534

    Replace this line
    $none = ''; // Sugar.

    by these 2 lines

    $none = ''; // Sugar.
    $image = str_replace('<img', '<amp-img', $image);
    Thread Starter pilardcv1

    (@pilardcv1)

    Thanks for the reply.
    But, I need to modify it from the “function.php” file of my custom theme, so it doesn’t get lost with the plugin update.

    It’s possible?

    Plugin Author Rui Guerreiro

    (@takanakui)

    I can add a filter in the next update so you can do it in the functions.php of the child theme. But for now, you will have to replace it directly in the plugin.

    Thread Starter pilardcv1

    (@pilardcv1)

    Excellent! Thank you! I look forward to the update.
    One last comment. Doing this:

    Try using str_replace inside the menu-image.php around line 534

    Replace this line
    $ none = ”; // Sugar

    by these 2 lines

    $ none = ”; // Sugar
    $ image = str_replace (‘<img’, ‘<amp-img’, $ image);

    I get:

    <li>
     <a href="">
      <amp-img width="24" height="24" src="images.png">
       <img width="24" height="24" src="image.png" />
       <span class="menu-image-title">Title</span>
      </amp-img>
     </a>
    </li>

    Instead of:

    <li>
     <a href="">
      <amp-img width="24" height="24" src="images.png">
       <img width="24" height="24" src="image.png" />
      </amp-img>
      <span class="menu-image-title">Title</span>
     </a>
    </li>

    The title is within <amp-img>.

    • This reply was modified 6 years, 9 months ago by pilardcv1.
    Plugin Author Rui Guerreiro

    (@takanakui)

    Use this modified version that already contains those filters.

    https://www.dropbox.com/s/lnbvnscp1fn76hc/menu-image.zip?dl=0

    The problem can be in the str_replace, I didn’t test that intensively. Check other ways to replace the tags.

    Use the code below inside the functions.php of your child themes to use the filter

    add_filter('menu_image_img_html', 'replace_menu_item_img_html');
    
    function replace_menu_item_img_html( $image ){
    	$image = str_replace('<img', '<amp-img', $image);
    	return $image;
    }
    Thread Starter pilardcv1

    (@pilardcv1)

    I used the modified version of the plugin and the filter code in the function.php of my theme and it doesn’t work. The tag is not replaced with <amp-img>

    Plugin Author Rui Guerreiro

    (@takanakui)

    Check if the pasted code doesn’t have any incorrect format, especially the single quotes. That can happen in the copy/paste process.

    It was working in my tests, well the HTML was being changed but the results of the preg_replace weren’t accurate.

    Plugin Author Rui Guerreiro

    (@takanakui)

    Will close the topic for now.

Viewing 8 replies - 1 through 8 (of 8 total)

The topic ‘How to replace with from function.php?’ is closed to new replies.