Solved

Passing simple php value in Wordpress single.php file

Posted on 2011-02-20
15
1,021 Views
Last Modified: 2012-05-11
These 2 lines:

$str = get_post_meta($post->ID, $key, true);
echo 'str is '.$str;

output this:
"str is http://www.site.com/-dan-baker.mp3 6787576 audio/mpeg"

That has been TESTED on my wordpress single.php file.

The original code is attached bellow, it returns the mp3 URL inside the string BUT it works fine ONLY if I provide $str value direct, like this: $str = "http://www.site.com/-dan-baker.mp3 6787576 audio/mpeg"

If I provide it like this: $str = get_post_meta($post->ID, $key, true); then $file will return empty.

Why? I am 100% sure get_post_meta($post->ID, $key, true) is returning the string.

I hope you understand.
Thank you.




<?php

// Code tested on wordpress file single.php

$key="enclosure";
$str = get_post_meta($post->ID, $key, true);
echo 'str is '.$str; //this output: str is http://www.site.com/-dan-baker.mp3 6787576 audio/mpeg

$split_at_space=explode(" ",$str);
$mimetype=array_pop($split_at_space);
$numbers=array_pop($split_at_space);
$file=join(" ",$split_at_space);
echo "\n";

echo 'File:'.$file;
?>

Open in new window

0
Comment
Question by:Fernanditos
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 8
  • 6
15 Comments
 
LVL 27

Expert Comment

by:Lukasz Chmielewski
ID: 34937971
Are you sure there are no whitespaces characters ? Trim the $str.
$str = trim(get_post_meta($post->ID, $key, true));
0
 

Author Comment

by:Fernanditos
ID: 34938013
Yes there are white spaces:

$str = "http://www.site.com/-dan-baker.mp3 6787576 audio/mpeg"

the crazy thing is that it works perfect if I assign the value to $str manually, like above.

But if I assign it this way: $str = get_post_meta($post->ID, $key, true); then $file return empty.

and again: I have test get_post_meta($post->ID, $key, true); is returning the same string.

Any idea?
0
 

Author Comment

by:Fernanditos
ID: 34938042
Look, I will post here the real code here and I take screeshot code result, this will clear any doubt:

result of attached code
<?php
$key="enclosure";
$str = get_post_meta($post->ID, $key, true);
echo 'str: '.$str; 

$split_at_space=explode(" ",$str);
$mimetype=array_pop($split_at_space);
$numbers=array_pop($split_at_space);
$file=join(" ",$split_at_space);
echo "<br/>";

echo 'File:'.$file;
?>

Open in new window

0
[Live Webinar] The Cloud Skills Gap

As Cloud technologies come of age, business leaders grapple with the impact it has on their team's skills and the gap associated with the use of a cloud platform.

Join experts from 451 Research and Concerto Cloud Services on July 27th where we will examine fact and fiction.

 
LVL 27

Expert Comment

by:Lukasz Chmielewski
ID: 34938048
Maybe the get_post_meta($post->ID, $key, true); ECHOES the string instead of returning a string value ?
0
 
LVL 27

Expert Comment

by:Lukasz Chmielewski
ID: 34938056
try doing it this way:
$str = echo get_post_meta($post->ID, $key, true);
0
 
LVL 27

Expert Comment

by:Lukasz Chmielewski
ID: 34938085
I was thinking about the whitespaces at the both ends of the returned string. This would cause the splitting function return incorrect results.

One more thing, what does this do:

<?php
$key="enclosure";
$str = get_post_meta($post->ID, $key, true);
echo 'str: '.$str; 

$split_at_space=explode(" ",$str);
$mimetype=array_pop($split_at_space);
$numbers=array_pop($split_at_space);

print_r($split_at_space);

$file=join(" ",$split_at_space);
echo "<br/>";

echo 'File:'.$file;
?>

Open in new window

0
 

Author Comment

by:Fernanditos
ID: 34938254
@Roads
Maybe the get_post_meta($post->ID, $key, true); ECHOES the string instead of returning a string value ?

Yes this has sense, I tried: $str = echo get_post_meta($post->ID, $key, true);  but I get syntax error, unexpected T_ECHO.

The second last solution you posted not woring, return this:
 result last solution by Roads
0
 
LVL 27

Expert Comment

by:Lukasz Chmielewski
ID: 34938404
Well, it does work. It points that the $split_at_space is an empty array after poping

$split_at_space=explode(" ",$str);
echo"after exploding: ";
print_r($split_at_space);
$mimetype=array_pop($split_at_space);
$numbers=array_pop($split_at_space);

echo"$mime_type, $numbers";

Open in new window


What does this show ?
0
 
LVL 27

Accepted Solution

by:
Lukasz Chmielewski earned 500 total points
ID: 34938416
If this shows Array () again, try like this

$str = echo "".get_post_meta($post->ID, $key, true);

but it seems very strange - assigning should work like this
$str = get_post_meta($post->ID, $key, true);
if this shows us that the $str is empty - then the function get_post_meta($post->ID, $key, true); returns nothing.
0
 

Author Comment

by:Fernanditos
ID: 34938478
I think you missunderstood something:

"you said: if this shows us that the $str is empty - then the function get_post_meta($post->ID, $key, true); returns nothing."

URL result that you see printed is the returned value of get_post_meta($post->ID, $key, true); SO this is not returning empty. See my code.

Notice that in my screenshot, I have 2 outputs: $str and $file

my problem is with $file that is returning EMPTY.

this is the most strange thing I have ever seen.
0
 

Author Comment

by:Fernanditos
ID: 34938513
I just can NOT capture the value returned by get_post_meta($post->ID, $key, true); into a variable.

I can just print it like: echo get_post_meta($post->ID, $key, true);

does this makes sense for you?
0
 

Author Comment

by:Fernanditos
ID: 34938534
I think this issue is relate to the nature of get_post_meta of wordpress: http://codex.wordpress.org/Using_Custom_Fields

no idea.
0
 
LVL 27

Expert Comment

by:Lukasz Chmielewski
ID: 34938594
You would have to find the file where the function is stored to see what it returns.
0
 
LVL 27

Expert Comment

by:Lukasz Chmielewski
ID: 34938647
Change true to false
get_post_meta($post->ID, $key, false);
0
 
LVL 110

Expert Comment

by:Ray Paseur
ID: 34938841
Please do this for us:

$str = get_post_meta($post->ID, $key, true);
var_dump($str);

And show us the var_dump() output.  It will tell the data type as well as the contents.  Use "view source" and copy the relevant lines, then post them here in the code snippet.  Thanks, ~Ray
0

Featured Post

What Is Blockchain Technology?

Blockchain is a technology that underpins the success of Bitcoin and other digital currencies, but it has uses far beyond finance. Learn how blockchain works and why it is proving disruptive to other areas of IT.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

When it comes to write a Context Sensitive Help (an online help that is obtained from a specific point in state of software to provide help with that state) ,  first we need to make the file that contains all topics, which are given exclusive IDs. …
Although a lot of people devote their energy toward marketing for specific industries, there are some basic principles that can be applied to any sector imaginable. We’ll look at four steps to take and examine how those steps were put into action fo…
This tutorial demonstrates how to identify and create boundary or building outlines in Google Maps. In this example, I outline the boundaries of an enclosed skatepark within a community park.  Login to your Google Account, then  Google for "Google M…
This tutorial walks through the best practices in adding a local business to Google Maps including how to properly search for duplicates, marker placement, and inputing business details. Login to your Google Account, then search for "Google Mapmaker…

636 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question