Creating custom wordpress function not working - php

Im trying to add a little code to an already made thumbnail plugin.
Basically i want to get the image src of an image and the just print it into the HTML, but everytime i run it, its just echoing 'Array'
This is what i have so far.
$testing = wp_get_attachment_image_src($post_thumbnail_id);
$html = '<a href="'.$testing.'">';
Any help would be greatly appreciated.

According to the Wordpress Documentation for wp_get_attachment_image_src the return value is:
An array containing:
$image[0] => url
$image[1] => width
$image[2] => height
Therefore, this is what you need:
$html = '<a href="'.$testing[0].'">';

Related

WordPress custom image gallery linking to custom image size

I'm creating a custom image gallery shortcode that opens the images in a lightbox. The line that outputs the images is:
$image_output = "<a href='" . wp_get_attachment_url( $id ) . "' data-toggle='lightbox' data-gallery='$selector' data-footer='$attachment->post_excerpt'> " . wp_get_attachment_image($id, $atts['size'], false, $attr ) . "</a>";
Everything works great except this links to the original (and full size) image. I want it to instead link to a custom image size. I tried wp_get_attachment_url( $id ) with wp_get_attachment_image_src( $id, 'gallery-thumbnail', false ).
However, instead of getting the desired image url I get Array. Any ideas what I'm doing wrong? Should be using something else?
wp_get_attachment_image_src() returns an array (url, width, height) so use
wp_get_attachment_image_src(...)[0]
wp_get_attachment_image_src takes the post_id and required size as parameters, and returns an array containing:
[0] = url
[1] = width
[2] = height
[3] = is_intermediate (i.e. whether it is a resized image or the original)
Therefore to get the url for a custom size called gallery-thumbnail, you need to do the following:
$img_attrib = wp_get_attachment_image_src( $id, 'gallery-thumbnail');
if ($img_attrib)
$url = $img_attrib[0];
$image_output = "<a href='".$url."' [...rest of code...] </a>";
I've done it step-by-step so you can see what's happening but you can of course shorten this as follows - however this will have a broken link if the image can't be found.
<a href='".wp_get_attachment_image_src( $id, 'gallery-thumbnail')[0]."' [...rest of code...] </a>"
Note: This assumes you have defined your custom size in functions.php using add_image_size and regenerated your thumbnails so that version of all images has been created.
Ref: Developer Reference for wp_get_attachment_image_src

if / else in wordpress to pull a specific image

I had some trouble with facebook pulling the wrong images from Wordpress links added to the FB news feed. I was able to correct it with the "if" snippet of code below. I added the "else" statement to provide for a fall-back image in case the user hasn't added a featured image to the linked WP post, but it doesn't seem to be working. Obviously I'm no php coder... any help?
<?php
if ( has_post_thumbnail())
{
$fb_image = wp_get_attachment_image_src(get_post_thumbnail_id($post->ID), 'full');
}
else {
$fb_image = wp_get_attachment_image_src ("http://MY-IMAGE-URL"), 'full');
}
?>
I also tried the code below in the else statement, but to avail
wp_get_attachment_image_src( 732, $size, $icon )
Thanks in advance
The function wp_get_attachment_image_src is used to get the URL (src) for an image attachment.
However, you cannot directly pass on an URL like you do as it works based on an attachment ID.
In your else statement, replace the line with the following in order to work with a default image :
$fb_image = "http://MY-IMAGE-URL";
Hope this helps

WordPress Image Shortcode Exporting Thumbnail Sized Image

To get it out of the way here is my code
<!-- Start Slider -->
<?php
// Get Data From Meta
$images = rwmb_meta( 'cam_slider_images', 'type=image_advanced' );
// Blank Variable To Append Too
$slider_image_output = "";
// Append Meta Value To Variable
foreach ( $images as $image )
{
$slider_image_output.= '[slide][image src="'.$image['url'].'" alt="'.$image['alt'].'"][/slide]';
}
// Echo The Shortcode With WordPress :)
echo (do_shortcode('[slider animation="fade, slide" slide_time="4000" slide_speed="500" slideshow="true" random="true" control_nav="false" prev_next_nav="true" no_container="true"]'. $slider_image_output . '[/slider]'));
?>
<!-- End Slider -->
I am attempting to display the images in the slider shortcode, now it works great however on the page the images only come out to be 150x150 pixels which is thumbnail size. So if you look at the image src url in firebug etc its -150x150.. so I was wondering how could I make WordPress export fullsize.
I am using the X theme and its shortcodes.
Thanks in advance.
EDIT
Also using the following plugin for my meta box
http://metabox.io/docs/get-meta-value/
I don't 100% get what exactly you are trying to achieve , but using documentation page you sent,
you can try change your line :
$slider_image_output.= '[slide][image src="'.$image['url'].'" alt="'.$image['alt'].'"][/slide]';
to this one:
$slider_image_output.= '[slide][image src="'.$image['full_url'].'" alt="'.$image['alt'].'"][/slide]';

Why is a specific variable causing html to print out

I am trying to display some images in my wordpress site. I am trying to make the background div and image via style=""
So when I do this:
<?php
if(has_post_thumbnail($property->ID)){
$image_url = get_the_post_thumbnail($property->ID,array(300,220),array('class' => "post_thumbnail"));
}else{
$image = $image_url = '';
}
?>
<div class="property_photo" style="background-image:url('<?php echo $image_url;?>') no-repeat;">
</div>
The photo is not displaying anymore and it's printing ') no-repeat;"> where the image should display. When I use a variable without an "_" in the name, it doesn't print anything. So to avoid that I tried $image = $image_url = ''; to get around that.
Is this is a simple syntax problem or is there something in the php that is causes this? It doesn't seem like syntax because when I use other variable it does not do this.
Your get_the_post_thumbnail() function is returning HTML, not a URL. You need to either change the function to only return a URL (or use a different function which does that), or change your HTML to put the image inside the div instead of setting it as a background image.

php - get the source of an image

I need to get all the source values from all image inside a container. I'm having some difficulty with this.
Allow me to explain the process.
All the data comes from a database. Inside the backofficce the user enter all the text and the image inside a textarea. To separate the text with the image the user must enter a pagebreak.
Let's go to the code
while ($rowClients = mysql_fetch_array($rsClients)) {
$result = $rowClients['content'];
$resultExplode = explode('<!-- pagebreak -->', $result);
// with resultExplode[0] I get the code and with resultExplde[1] I get the image
// Now with I want to get only the src value from resultExplode[1]
I already tried with strip_tags
$imageSrc = strip_tags($resultadoExplode[1]);
but it doesn't print anything.
I found this post but without success. I stopped in the first print_r.
Can anyone help me??
Thanks
try foreach, if you can't print it out.. (if that's the problem)
foreach($resultExplode as $key => $value){
echo "[".$key."]".$value;
}
I found a solution:
continuing with the previous code I worked with the split function.
So I start to strip the tags. This way I get the img isolated from the rest.
$image = strip_tags($resultExplode[1],"<img>");
Because all the img has the same structure like this: <img width="111" height="28" alt="alternative text" src="/path/to/the/file.png" title="title of the image">
I split this string, using " as a delimiter
$imgSplit = split('"', $image);
$src = $imgSplit[3];
Voilá. It's working
What do you say about this procedeure??

Categories