I've created a simple function that outputs an image with the correct html markup, seen below. The problem I am facing is when I pass into the alt text that has a space in it, such as "My cat is great", the alt breaks and shows alt="My" like <img src="blah.jpg" alt="My" cat is great class="home">. I'm having a hard time trying to figure this out. Any thoughts?
function image_creator($image_url, $alt=false, $class=false) {
$string = '<img src='.$image_url.' alt='.$alt.' class='.$class.'>';
return $string;
}
You're not actually outputting <img src="blah.jpg" alt="My" cat is great class="home"> - that's just how the browser is interpreting it.
You're outputting <img src=blah.jpg alt=My cat is great class=home>
You need to output some quotes:
$string = '<img src="'.$image_url.'" alt="'.$alt.'" class="'.$class.'">';
$string = "<img src='".$image_url."' alt='".$alt."' class='".$class."'>';
try this i think it will work
A little user failure. No problem!
Make sure you have your <img> tag correctly.
$image_url = 'https://example.com/image';
$alt = 'My cat is awesome';
$class = 'image';
So:
function image_creator($image_url, $alt=false, $class=false) {
$string = '<img src='.$image_url.' alt='.$alt.' class='.$class.'>';
return $string;
}
Outcome:
<img src="https://example.com/image" alt="My" cat="" is="" awesome="" class="image">
Should be:
function image_creator($image_url, $alt=false, $class=false) {
$string = '<img src="'.$image_url.'" alt="'.$alt.'" class="'.$class.'">';
return $string;
}
Outcome:
<img src="https://example.com/image" alt="My cat is awesome" class="image">
Do you see the differences? Look carefully at the quotes.
Documentation:
http://php.net/manual/en/language.types.string.php
Some examples about using quotes:
https://www.virendrachandak.com/techtalk/php-double-quotes-vs-single-quotes/
Personally I would like to use:
// $image below is being sent to the function
$image = [
'image_url' => 'https://example.com/image',
'alt' => 'My cat is awesome',
'class' => 'image',
];
function image_creator($image = [])
{
if(empty($image))
{
return 'No image';
}
return '<img src="' . $image['image_url'] . '" alt="' . $image['alt'] . '" class="' . $image['class'] . '">';
}
//Multiple solution with multiple way
$image_url = 'https://example.com/image';
$alt = 'My cat is awesome';
$class = 'image';
$string = '<img src="'.$image_url.'" alt="'.$alt.'" class="'.$class.'">';
//also you can direct echo
echo '<img src="',$image_url,'" alt="',$alt,'" class="',$class,'">';
//also you can direct echo
$string= "<img src='{$image_url}' alt='{$alt}' class='{$class}' >";
//also you can direct echo
$string = "<img src='${image_url}' alt='${alt}' class='${class}' >";
echo $string;
Related
This code loop returns any number of image links. The variable $picture->descriptionreturns a string of text with underscores that need to be removed.
bluebell_glitter_print_one_piece_with_bubble_skirt_LGG5659WT14_china
All coding variations that I have tried either end in a single correct image link or a white screen.
How can I edit this loop so that the data-caption-desc has no underscores?
if(count($pictures)){
foreach($pictures as $picture){
$output .= '<a href="'.get_option('siteurl').$picture->path.'/'.$picture->filename.'" title="'.$picture->alttext.'" data-caption-title="'.$picture->alttext.'" data-caption-desc="'.$picture->description.'">
<img class="wpnggimgcls" src="'.get_option('siteurl').$picture->path.'/thumbs/thumbs_'.$picture->filename.'" style="" />
</a>';
}
}
You should use str_replace
str_replace('_', ' ', $input_string);
str_replace will replace a characte with another one.
Full:
if(count($pictures)) {
foreach($pictures as $picture){
$output .= '<a
href="'.get_option('siteurl').$picture->path.'/'
.$picture->filename.'" title="'.$picture->alttext.
'" data-caption-title="'.$picture->alttext.'"
data-caption-desc="'
.str_replace('_', ' ',$picture->description).'">
<img
class="wpnggimgcls"
src="'.get_option('siteurl').
$picture->path.'/thumbs/thumbs_'.
$picture >filename.'" style="" />
</a>';
}
}
If you just need to replace the "underscore" into whitespace, you then can use preg_replace(), as follows:
<?php
$strChg='bluebell_glitter_print_one_piece_with_bubble_skirt_LGG5659WT14_china';
echo $strChg;
echo '<br />';
echo '<br />';
$Chg=preg_replace('/_/', ' ', $strChg);
echo $Chg;
?>
So, the point is:
$Chg=preg_replace('/_/', ' ', $strChg);
I'm using the Advanced Custom Fields Repeater to pump out the image URL. But, it shows the ID, not the URL. Any help is greatly appreciated, thanks. Here is my code:
<?php
$rows = get_field('images');
foreach($rows as $row){
$image = wp_get_attachment_image_src(get_sub_field('image'), 'full');
//var_dump($row['image']);
echo '<img src="'. $row['image']['url'] . '" class="shadowed forced">';
}
?>
In response to your question, it doesn't work because get_sub_field has to be used in a while statement in conjunction with has_sub_field(), and because your $row['image']['url'] is referring to an the image object (set via return value on the field menu), while is seems as if you are actually having the field return the 'Attachment ID'.
If you want to use a foreach with the return value set to 'Attachment ID', you need to use the object/array that is being returned like so:
<?php
$rows = get_field('images');
foreach($rows as $row){
$image = wp_get_attachment_image_src($row['image'], 'full');
echo '<img src="'. $image[0] . '" class="shadowed forced">';
}
?>
If you want to use the foreach method, with the return value set to 'Image Object', it should look something like this:
<?php
$rows = get_field('images');
foreach($rows as $row){
echo '<img src="'. $row['image']['url'] . '" class="shadowed forced">';
}?>
Another solution, working of your answer/suggestion would be to set the fields return value to 'Image URL', and then you could simply call the image like so:
<?php $rows = get_field('images');
if ($rows)
{
while (has_sub_field('images'))
{
$image = get_sub_field('image');
echo '<img src="'. $image . '" class="shadowed forced">';
}
} ?>
Alternatively if you wanted to show a different size of the image, you could also set the 'Return Value' to 'Image Object', and do something like this:
<?php $rows = get_field('images');
if ($rows)
{
while (has_sub_field('images'))
{
$image = get_sub_field('image');
echo '<img src="'. $image['sizes']['large'] . '" class="shadowed forced">';
}
} ?>
Looks like what I needed to do was not use a foreach, and instead just use and if & while statement. I was calling in the repeater, not the subfield with the old code. Here's a reference if anyone runs into this issue -
<?php
$rows = get_field('images');
if ($rows)
{
while (has_sub_field('images'))
{
$image = wp_get_attachment_image_src(get_sub_field('image'), 'full');
echo '<img src="'. $image[0] . '" class="shadowed forced">';
}
}
?>
I have constructed a custom gallery type as a new shortcode in Wordpress.
However I want to make it a little more SEO friendly so am trying to call the alt tag, and if the alt tag is not found replace it with the title.
So far I have the following (and it works fine) but I am stuck at trying to have $alt fallback to title if $alt is empty? Any ideas/help appreciated!
$bigimageUrl = wp_get_attachment_image_src($attachment_id,$size='full-size');
$littleimageUrl = wp_get_attachment_image_src($attachment_id,$size='thumbnail');
$title_raw = $attachment->post_title;
$title = preg_replace("/[^a-zA-Z0-9\s]/", " ", $title_raw);
$alt_raw = get_post_meta($attachment_id, '_wp_attachment_image_alt', true);
$alt = preg_replace("/[^a-zA-Z0-9\s]/", " ", $alt_raw);
echo '<img class="gallerytype" title="'; echo $title; echo '" alt="'; echo $alt; echo '" data-rsTmb="'; echo $littleimageUrl[0]; echo '" src="'; echo $bigimageUrl[0]; echo '"/> ';
Instead of using one echo after the other:
echo '<img class="gallerytype" title="';
echo $title;
echo '" alt="';
echo $alt; echo '" data-rsTmb="';
echo $littleimageUrl[0];
echo '" src="';
echo $bigimageUrl[0]; echo '"/> ';
You can use a comma (,) to separate expressions to be output:
echo '<img class="gallerytype" title="', $title, '" alt="', ..., '"/> ';
That should already help you to make the code a little mit more readable.
Then you want to set $alt to $title if it is empty. So do that before the output:
empty($alt) && $alt = $title;
Read as: $alt is empty and $alt is $title.
You could also write that with an if clause:
if (empty($alt)) $alt = $title;
That should do it for you.
Here is my code:
$search = array('<script src="/',
'<link href="/',
'<a href="/',
'<img src="/',
'src="/');
$d = 'http://www.ifreewind.net';
$replace = array('<script src="'.$d.'/',
'<link href="'.$d.'/',
'<a href="'.$d.'/',
'<img src="'.$d.'/',
'src="'.$d.'/');
$result = str_replace($search, $replace, $contents);
echo $result;
These code have a problem is that they cannot replace img tag such as :
<img width="50px" src="/...">
into
<img width="50px" src="http://www.ifreewind.net/...">
How to fix that?
You can't use str_replace for this. You could try it with preg_replace:
preg_replace('~(src|href)="(?=/)~', '$1http://www.ifreewind.net', $contents);
However, I'd strongly advise you to use an HTML parser instead.
Hi I'm developing a custom forum on my website. I would like to convert the urls starting with : http://*.domain.com/photos/{username}/{photo_id} (I should get both username and photo_id ) to the direct image tag so that user get the image instead of the url.
This should be done if they insert the url with or without bbcode:
ie:
http://domain.com/photos/musthafa/12345
[url=http://domain.com/photos/musthafa/12345]my photo link here[/url]
[url=http://domain.com/photos/musthafa/12345]http://domain.com/photos/musthafa/12345[/url]
This should be converted to < html-imge tag src="url-to_photo-path/photo_id.j_p_g" />
I tried this:
$str = "http://www.domain.com/photos/musthafa/12345"
$str = preg_replace_callback("'\[url=http:\/\/www\.domain\.com\/photos\/(.*?)\](.*?)\[/url\]'i", 'self::parse_photo_url', $str);
AND
$str = preg_replace_callback("#^https?://([a-z0-9-]+\.)domain.com/photos/(.*?)$#", 'self::parse_gpp_photo', $str);
function parse_photo_url($url){
{
$full_url = "http://www.domain.com/" . $url[1];
$url_segs = parse_url($full_url);
$path = explode("/", $url_segs['path']);
return '<img src="http://www.domain.com/{path-to-the-gallery}/'.$path[2].'/jpg" />';
}
Musthafa.
I'm not sure that I've exactly got what you want but please try this:
<?php
$image_url_samples = array(
"http://domain.com/photos/musthafa/12345",
"[url=http://domain.com/photos/musthafa/12345]my photo link here[/url]",
"[url=http://domain.com/photos/musthafa/12345]http://domain.com/photos/musthafa/12345[/url]"
);
foreach ($image_url_samples as $image_url_sample)
{
$conversion_result = preg_replace("/^(http:\\/\\/domain.com\\/photos\\/\\w+\\/\\d+)$/i",
"<img src=\"\\1.jpg\" alt=\"\\1\" />", $image_url_sample);
$conversion_result = preg_replace("/^\\[url=(http:\\/\\/domain.com\\/photos\\/\\w+\\/\\d+)\\](.+)\\[\\/url\\]$/",
"<img src=\"\\1.jpg\" alt=\"\\2\" />", $conversion_result);
print $conversion_result . "<br />";
}
The output is:
<img src="http://domain.com/photos/musthafa/12345.jpg" alt="http://domain.com/photos/musthafa/12345" />
<br />
<img src="http://domain.com/photos/musthafa/12345.jpg" alt="my photo link here" />
<br />
<img src="http://domain.com/photos/musthafa/12345.jpg" alt="http://domain.com/photos/musthafa/12345" />
<br />
By the way, if you want to make your URL case insensitive add the i modifier to the end of regular pattern (PHP Pattern Modifiers).
Basically I would like to extract the id (12345 in this example) and I need to extract the direct link to the image url so that, user should get the image tag instead of url.
Thats why I called
preg_replace_callback function.
In simple words, I'm stuck at the regex pattern to match my domain url starting with:
http://domain.com/photos{username}/{id}
OR
http://www.domain.com/photos{username}/{id}"
You don't need regex.
function buildLink( $id, $name ){
$html = array();
$html[] = '<img alt="" src="http://www.domain.com/photos/';
$html[] = $name;
$html[] = '/';
$html[] = $id;
$html[] = '.jpg">';
return implode( '', $html );
}
$path = 'http://www.domain.com/photos/musthafa/12345';
$path = rtrim( $path, ' /' );
$path_parts = explode( '/', $path );
$id = ( int ) array_pop( $path_parts );
$name = array_pop( $path_parts );
$img = buildLink( $id, $name );
echo $img;