Wordpress do_shortcode by passing a get_post_meta value - php

I'm using the iframe Wordpress plugin. It requires a syntax (shortcode) like:
[iframe src="http://www.youtube.com/embed/A3PDXmYoF5U" width="100%" height="480"]
I want to take the info inside a meta_key and pass in a Wordpress template through the do_shortcode function. The code I'm using is the following:
if ( get_post_meta($post->ID, 'youtube-video', true) ){
$youtube_video = get_post_meta($post->ID, 'youtube-video', true);
echo do_shortcode('[iframe src="' . $youtube_video . '" width="100%" height="480"]');
}
But it doesn't work. I think the problem is due to get_post_meta function that returns a value. How to solve this problem?
EDIT: Please note that if I modify the code above this way:
if ( get_post_meta($post->ID, 'youtube-video', true) ){
$youtube_video = get_post_meta($post->ID, 'youtube-video', true);
echo $youtube_video;
echo do_shortcode('[iframe src="' . $youtube_video . '" width="100%" height="480"]');
}
It print the url (http://www.youtube.com/embed/A3PDXmYoF5U) but the last echo do not print nothing.

Related

Struggling to show image with ACF WordPress

I've been trying to display an image with ACF code and have just got it working, but I can't work out why some code works and some doesn't.
if(get_row_layout() == 'bq_product'):
$image = the_sub_field('affiliate_image');
$affiliate_url = the_sub_field('affiliate_url');
?><img src="<?php echo $image ?>"/><?php //This line doesn't work and just displays the raw URL on the front end
?><img src="<?php the_sub_field('affiliate_image') ?>"/><?php //This line works and shows the image
?>Link //Similarly, this line doesn't use the URL set in affiliate_url, but does if I pass "the_sub_field('affiliate_url')"
How do I use the variable names within the image src without it just showing the raw URL on the front end?
I've tried using "get_sub_field" variations but they don't seem to make a difference.
Exactly as #Stender commented, attempting to store variables using ACF the_sub_field() will not work.
Use get_sub_field() instead to store returned ACF field data to your variables.
Then access variable data based on what you have set the ACF fields to return with... array, ID or URL
See example below (based on affiliate_image ACF field return format URL)
// if row layout is 'bq_product'
if(get_row_layout() == 'bq_product'):
// sub field vars
$affiliate_image = get_sub_field('affiliate_image');
$affiliate_url = get_sub_field('affiliate_url');
// if $affiliate_image variable is not boolean false
if($image) {
echo '<img src="' . $affiliate_image . '" alt="" />';
}
// if $affiliate_url is not boolean false
if($affiliate_url) {
echo 'Link';
}
endif;
See example below (based on affiliate_image ACF field return format array)
// if row layout is 'bq_product'
if(get_row_layout() == 'bq_product'):
// sub field vars
$affiliate_image = get_sub_field('affiliate_image');
$affiliate_url = get_sub_field('affiliate_url');
// use this to dump $affiliate_image array to see data for image sizes etc
// echo '<pre>' . print_r($affiliate_image, true) . '</pre>';
// if $affiliate_image variable is array
if(is_array($affiliate_image) {
echo '<img src="' . $affiliate_image['url'] . '" alt="' . $affiliate_image['alt'] . '" />';
}
// if $affiliate_url is not boolean false
if($affiliate_url) {
echo 'Link';
}
endif;

Using Fly Dynamic Image Resizer for ACF gallery

Trying to use a great plugin "Fly Dynamic Image Resizer" to return images from ACF but not having much luck. Can anyone provide any clue as to what I may be doing wrong? Thanks.
ACF variables:
<?php
$photo = get_field('photo', $post);
$fly_image = fly_get_attachment_image_src($photo, 'big_featured_works', true);
?>
HTML:
<img src="<?php echo $fly_image['src']; ?>"/>
$fly_image = fly_get_attachment_image_src(get_field('photo', $post), 'big_featured_works', true);
<img src="<?php echo $fly_image['src']; ?>"/>
then switch the return value of the custom field to image ID (from default "Image Array")
If your field is a repeater field you'll have to use get_sub_field, like this:
$image = get_sub_field('image');
fly_get_attachment_image_src($image, 'image-custom-size')['src'];
$image = fly_get_attachment_image_src( get_post_thumbnail_id(), 'home_page_square', array( 500, 500 ), true );
echo '<img src="' . $image['src'] . '" width="' . $image['width'] . '" height="' . $image['height'] . '" />';
(Reference)

Common php function called by other functions

Hello I would like to create two functions with different parameter but with a same common function. Here's my example...
The common function :
function my_responsive_pictures($post_id){
// Get alt text or set the $alt_text variable to the post title if no alt text exists
$alt_text = get_post_meta($attachment_id, '_wp_attachment_image_alt', true);
if ( !$alt_text ) { $alt_text = esc_html( get_the_title($post_id) ); }
// Get the info for each image size including the original (full)
$thumb_original = wp_get_attachment_image_src($attachment_id, 'slideshow');
$thumb_large = wp_get_attachment_image_src($attachment_id, 'slideshow-lg');
$thumb_medium = wp_get_attachment_image_src($attachment_id, 'slideshow-md');
$thumb_small = wp_get_attachment_image_src($attachment_id, 'slideshow-xs');
// Create array containing each image size + the alt tag
$thumb_data = array(
'thumb_original' => $thumb_original[0],
'thumb_large' => $thumb_large[0],
'thumb_medium' => $thumb_medium[0],
'thumb_small' => $thumb_small[0],
'thumb_alt' => $alt_text
);
// Echo out <picture> element based on code from above
echo '<picture>';
echo '<!--[if IE 9]><video style="display: none;"><![endif]-->'; // Fallback to <video> element for IE9
echo '<source srcset="' . $thumb_data['thumb_large'] . ', ' . $thumb_data['thumb_original'] . ' x2" media="(min-width: 800px)">';
echo '<source srcset="' . $thumb_data['thumb_medium'] . ', ' . $thumb_data['thumb_large'] . ' x2" media="(min-width: 400px)">';
echo '<source srcset="' . $thumb_data['thumb_small'] . ', ' . $thumb_data['thumb_medium'] . ' x2">';
echo '<!--[if IE 9]></video><![endif]-->'; // Fallback to <video> element for IE9
echo '<img srcset="' . $thumb_data['thumb_small'] . ', ' . $thumb_data['thumb_medium'] . ' x2" alt="' . $thumb_data['thumb_alt'] . '">';
echo '</picture>';
}
Another one which calls the common function :
function my_responsive_thumbnail($post_id){
// Get the featured image ID
$attachment_id = get_post_thumbnail_id($post_id);
my_responsive_pictures();
}
And a second one with other parameters $attachment_ID :
function my_responsive_acfthumbnail($post_id){
// Get the featured image ID
$attachment_id = get_field('image_bandeau');
my_responsive_pictures();
}
Nothing happens :(. What do I do wrong ? Thanx for your help...
Your function is expecting a parameter, and when you're calling it here
my_responsive_pictures();
you aren't passing anything.
You also have to call the my_responsive_thumbnail() function before it's going to make the subsequent calls to your "common" function.
There are a few issues with this code. The first thing we need to look at is the main function.
function my_responsive_pictures($post_id){
Your function definition doesn't give $post_id a default value therefore it's required any time you call the function. By attempting to call the function without passing an the argument you'll trigger an error.
$alt_text = get_post_meta($attachment_id, '_wp_attachment_image_alt', true);
if ( !$alt_text ) { $alt_text = esc_html( get_the_title($post_id) ); }
Here you're referring to $attachment_id which hasn't been set. When it's not found you're then getting the title of the post.
For this to work you'll need to set two parameters for the function.
function my_responsive_pictures( $attachment_id, $post_id ) {
Any time we call this function we need to pass in the $attachment_id (ID of the image) and $post_id (ID of the post).
Next up we need to modify the functions that ultimately call the main function.
function my_responsive_thumbnail( $post_id ) {
// Get the featured image ID
$attachment_id = get_post_thumbnail_id( $post_id );
// Now that we have the featured image ID, we really ought
// to do some error checking. Let's assume that all went well.
my_responsive_pictures( $attachment_id, $post_id );
}
This next function requires more attention. Remember that you're calling these functions with the post ID. You need to let get_field() know the ID of the post it should retrieve the image for.
function my_responsive_acfthumbnail( $post_id ) {
// Get the featured image ID
$attachment_id = get_field( 'image_bandeau', $post_id );
my_responsive_pictures( $attachment_id, $post_id );
}
Example usage:
my_responsive_acfthumbnail( get_the_ID() );
You may also want to consider setting a default for the post ID so you don't need to pass it in when retrieving an image for the current post you're viewing.
Finally, consider the level of duplication between the functions which call my_responsive_pictures. You'll want to check the attachment ID is valid so the functions are likely to become larger with only 1 line that's different.
Further information on get_field(): https://www.advancedcustomfields.com/resources/get_field/

Removing The IMG SRC tag in Wordpress

I'm trying to remove the "img src" tag from the php so it'll simply display the images url, rather than displaying the actual image. This is the code I've got so far and it works perfectly, but when it's rendered it shows thumbnails instead of urls.
<?php $pics = get_post_meta( $post->ID, 'pics', true );
foreach( $pics as $pics)
{
$image_attributes = wp_get_attachment_image_src( $pics['pictures'] );
echo '<img src="' . $image_attributes[0] . '" />';
}
?>
I know theres a way to do this, but I don't know how to remove the tags without breaking the image code. Any help is appreciated.
If you just want to echo the image src and not display it as an image then change
echo '<img src="' . $image_attributes[0] . '" />';
to
echo $image_attributes[0];
<?php
$pics = get_post_meta( $post->ID, 'pics', true );
foreach( $pics as $pics)
{
$image_attributes = wp_get_attachment_image_src( $pics['pictures'] );
echo $image_attributes[0];
}
?>
So do you want to show on the page the "html code" with the tag and the src attribute?
Have you tried to enclose "img" tag within "pre" tag?
echo '<pre><img src="' . $image_attributes[0] . '" /></pre>';

correct syntax to echo variable inside iframe

I know i am missing something simple. I just want to display this iframe if $video-code exists. Can anyone see what is wrong with this? working in wordpress. error is on the echo line. i've also tried adding .'$video-code'. into the url.
it is displaying the iframe correctly, but the variable is displaying as text in the url. if i call the variable elsewhere in the page without the If statement, it displays correctly.
THANKS for any help!
<?php
$key = 'video-code';
$themeta = get_post_meta($post->ID, $key, TRUE);
if($themeta != '') {
echo '<iframe id="player" width="560" height="315" frameborder="2" src="http://www.youtube.com/embed/$video-code" ></iframe>';
}?>
You can concatenate your $key, like so:
echo '<iframe id="player" width="560" height="315" frameborder="2"
src="http://www.youtube.com/embed/' . $key . '" ></iframe>';

Categories