How to change multiple html elements with php (preg_replace?) - php

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?

Related

Error 403 Image not found in Drupal 7

I create a block from configuration. Inside this block, I write this code...
<div class="provider-bg" id="provider1">
<img alt="" class="img-responsive" src="<?php echo base_path().path_to_theme() ?>/images/provider-1.jpg" />
</div>
Then, I save with PHP in text format.
My virtual host is ... http://localhost:8888/drupal
So, the image path will be like this ...
<img alt="" src="/drupal/sites/all/themes/myancast/images/ios.png">
This image appears in the last few days ago. Today, I run the site and that image disappear immediately and got 403 error.
Failed to load resource: the server responded with a status of 403 (Forbidden).
I'm trying to find the solution the whole day. But, I still cannot solve.
Can anyone help me please ?
Add global $base_url in your code and use like below
<img alt="" class="img-responsive" src="<?php echo $base_url.'/'.path_to_theme(); ?>/images/provider-1.jpg" />
did you try this ?
<img alt="" class="img-responsive" src="<?php echo '/'.path_to_theme(); ?>/images/provider-1.jpg" />
try this,
<?php
$imgurl = file_create_url(path_to_theme().'/images/provider-1.jpg');
?>
<img alt="" class="img-responsive" src="<?php echo $imgurl ?>"/>

Can't load image

As you can see, there is no image. What's wrong with this?
$displayProdCat .= '<div class="product">
<img src="Customer/images/product'.$ItemNo.'.jpg" width="170" height="150" />
<h3>'.$ItemName.'</h3>
<p class="product_price">Php '.$Price.'</p>
Add to Cart</div>';
Probably a wrong image url is specified. Look at the source.

PHP file_get_contents with if/else

On this link I was learning about file_get_contents and I'd like to incorporate it into a simple if/else statement but can't seem to get it correct.
I'd like to check if the file exists and if not, display a no_image.jpg graphic.
My struggle is that the tag already has a PHP echo so it is causing the snippet to stop working. How do I format the if/else while still haveing the use the dynamic value from the echo if file_get_contents is true?
Currently I have a simple tag like this:
<img width="200" height="150" src="images/<?php echo(rawurlencode($row['MLS_NUMBER'])); ?>_1.jpg" alt="" align="left" vspace="3" hspace="3" />
Basically:
<?php
if (file_exists(...)) {
$path = ...;
} else {
$path = 'no_image.jpg';
}
?>
<img src="images/<?php echo $path; ?>" />
//add a semicolon after echo $path otherwise it won't work
Assuming you are looking for a jpg file based on $row['MLS_NUMBER']
<img width="200" height="150" src="images/
<?php
if (file_exists($row['MLS_NUMBER']) {
echo(rawurlencode($row['MLS_NUMBER'])).'_1.jpg';
} else {
echo "no_image.jpg";
}
?>" alt="" align="left" vspace="3" hspace="3" />

Remove/Edit Img classes in Wordpress

So Wordpress is outputting this
<img src="http://site.com/path/to/image.png"; alt="Text" width="600" height="100" class="alignnone size-full wp-image-168" />
However, I would like it to output this
<img src="http://site.com/path/to/image.png"; alt="Text" width="600" height="100" class="full wp-image-168" />
Removing the align class and removing the size- part.
How can I go about this via a filter in the functions.php file?
So here are a couple filters that may work for what you need.
add_filter( 'post_thumbnail_html', 'remove_image_classes', 10 );
add_filter( 'image_send_to_editor', 'remove_image_classes', 10 );
function remove_image_classes( $html ) {
$html = preg_replace( '/(size-|alignnone)/', "", $html );
return $html;
}
I don't know if I got that preg_replace right. Try it out and see if it modifies your code.

PHP function return. Nested functions

I know there's Wordpress StackExchange, but that's more PHP related question.
I'm writing my own shortcode for Wordpress it looks like:
function myShortcode_shortcode() {
return 'something';
}
This shortcode displays simple string "something".
The problem is I want to display an image from template directory:
<img src="<?php bloginfo('template_directory') ?>/images/myImage.jpg" alt="" />
And I don't know how?
When I do:
return '<img src="'. bloginfo('template_directory') .'/images/myImage.jpg" alt="" />';
Script is echoing template directory instead of image.
Any ideas?
The problem is that the bloginfo() function is an output function (intended for templates). You need get_bloginfo() rather.
You probly need to place <img src="<?php bloginfo('template_directory') ?>/images/myImage.jpg" alt="" /> in his own variable like
function shortcode(){
$shortcode = "<img src='". bloginfo('template_directory') ."/images/myImage.jpg' alt="" />"
return $shortcode;
}
Hope this helps

Categories