I am trying to interpolate a string from an external file but I am having issues using complex curly braces. The variables do not populate as expected? If i take the code from the external template file it successfully returns a value for each variable.
index.php
function getIncludeContents($included_file_path, $post){
if ( !file_exists( __DIR__ . $file ) ) {
return 'nofile';
}
if ( is_array( $post ) ){
extract( $post );
}
ob_start();
include($included_file_path);
return ob_get_clean();
}
$navigation = getIncludeContents('layouts/test.php', $post);
var_dump($navigation);
test.php
<div id="post-{$post['id']}"></div>
this returns...
'<div id="post-${post['id']}"></div>'
when it should return...
'<div id="post-201"></div>
any ideas why? thanks
Since test.php doesn't have <?php, it's being treated as fixed text, not PHP code to execute. It needs echo statements to echo variables, or you can use the short echo tag.
<div id="post-<?= $post['id'] ?>"></div>
Try this way
<div id="post-<?php echo $post['id']?>"></div>
Related
To organize better my code, I would like to split some of my HTML in templates.
I have a loop inside a shortcode to get the posts:
function nh_shortcode_func($atts) {
$posts = get_posts_by_category(.......);
$html .= '';
foreach ($posts as $post) {
$post_id = $post->ID;
$post_title = $post->post_title;
$post_image = get_field('image', $post_id);
$html .= **HERE THE CODE OF HTML WITH DIVS AND CLASSES**
}
return $html
}
add_shortcode('nh-content', 'nh_shortcode_func');
Instead, put my HTML code inside a variable, I would like to use another file with the HTML structure that I'm going to use to build the posts. Also, I would like to pass the dynamic data to it.
Any ideas?
Thanks.
Just use an include file and it will have access to the variables. Something like:
function nh_shortcode_func($atts) {
ob_start();
include dirname( __FILE__ ) . '/templates/nh_shortcode.php';
return ob_get_clean();
}
add_shortcode('nh-content', 'nh_shortcode_func');
Suppose this code prints Youtube:
<?php ytio_empt(); ?>
I want a dynamic way to echo the content of the above function in the place of 'YouTube' in the following xml data:
$xmlData = file_get_contents( 'http://gdata.youtube.com/feeds/api/users/'. 'YouTube' );
I have tried:
$xmlData = file_get_contents( 'http://gdata.youtube.com/feeds/api/users/'. ytio_empt() );
But in vain, the file_get_contents() always fails to open stream.
P.S: Perhaps using HTML will work: to put <?php ytio_empt(); ?> in the place of ytio_empt() in $xmlData. I just don't know how to end PHP function and resume it later..
So as you posted your function in the comments:
function ytio_empt() {
if(empty(get_option('ytio_username'))) {
echo esc_attr( get_option('ytio_id') );
//^^^^
} else {
echo esc_attr( get_option('ytio_username') );
//^^^^
}
}
You will see you don't return the values you just print them! So in order to return them you simply have to change echo -> return.
And if you want to read more about return values see the manual: http://php.net/manual/en/functions.returning-values.php
I am using wordpress. I am using the plugin social locker.
I have figured out how to display the shortcode via PHP:
<?php echo do_shortcode('[sociallocker id="3071"] My content Here [/sociallocker]'); ?>
However I want to display this button that will allow for a print popup in between there.
This code is:
<?php if ( function_exists( 'pdf_print_popup' ) ) pdf_print_popup(); ?>
So my question is can I do this:
<?php echo do_shortcode('[sociallocker id="3071"] <?php if ( function_exists( 'pdf_print_popup' ) ) pdf_print_popup(); ?> [/sociallocker]'); ?>
You have opening / closing PHP tags inside your echo statement.
Try this instead:
<?php
ob_start();
if ( function_exists( 'pdf_print_popup' ) ) {
pdf_print_popup();
}
$content = ob_get_clean();
echo do_shortcode( '[sociallocker id="3071"]' . $content . '[/sociallocker]' ); ?>
You'd be better modifying pdf_print_popup to return instead of output a value as well removing the need for output buffering.
I have a class with a method called paint() so i call $object->paint(); and it returns the html code to display the object the way i like it.
In a function, i have an array of those objects, so i do something like this:
$code = '<p class="wrapper">';
foreach( $object_arr as $object ){
$code .= $object->paint();
}
$code .='</p>';
echo $code;
but the result iḿ getting is this:
<p class="wrapper"></p>
<figure id="f1">Figure 1</figure>
<figure id="f2">Figure 2</figure>
...
<figure id="fn">Figure n</figure>
The function paint() returns the code to paint the object, i was expecting to see:
<p class="wrapper">
<figure id="f1">Figure 1</figure>
<figure id="f2">Figure 2</figure>
...
<figure id="fn">Figure n</figure>
</p>
What am i doing it wrong?
The result you're looking at might be corrected by your browser. p does not accept figure as a child, so the browser might correct it by closing your first <p class="wrapper">.
Some more code would be helpful, but this might put you on the right track.
What is $object_arr an array of. Can you post the example of the object in the object array?
With my example you would have to pass in the index as well to get the id="fnn".
If you post some more code I can clarify my answer.
<?php
function paint($str){
return '<figure id="fn">'.$str.'</figure>';
}
$object_arr =Array('happy','beans');
$code = '<p class="wrapper">';
foreach( $object_arr as $object ){
$code .= paint($object);
}
$code .='</p>';
echo $code;
If you're using single quotes, then put a tab before the method call:
$code .= ' ' . $object->paint();
But this would be easier:
$code .= "\t" . $object->paint();
OK.
I have a very long and pretty complicated function.
It looks almost like this one:
<?php
function hello() {
echo 'My Function!' ?>
<ul>
<li> Blablabla </li>
<ul>
(...)
<?php } ?>
The HUGE problem here is that I'm UNABLE to echo anything.
My function HAVE to return it's contents instead of echoing or direct outputting (it has to be that way, it's a Wordpress shortcode and when I echo - the contents are getting displayed at the top of the page - ALWAYS, not in the place where I want them):
<?php
function hello() {
$output .= 'My Function!';
$output .= '<ul>';
$output .='<li> Blablabla </li>';
$output .='<ul>';
(...)
return $output;
} ?>
I hope it's easy till now.
Now, the real problems are:
I have tons of direct input code like:
?>
<div>
<span>
<p>Smth</p>
<a>smth</a>
</span>
</div>
<?php
Adding $output everywhere kills the nice paragraphs/whitespace and code is getting VERY HARD to read and understand (and all HTML elements are parts of variable now, so even my php editor is not treating them well and coloring them as PHP elements).
And another thing, I have tons of lines like this one:
<a href="<?php bloginfo('template_directory'); ?>/includes/php/timthumb.php?src=<?php echo $url; ?>&h=<?php if($items=="one") echo 320; elseif($items=="two") echo 230; elseif($items=="three") echo 180; elseif($items=="four") echo 130; ?>&w=<?php if($items=="one") echo 600; elseif($items=="two") echo 420; elseif($items=="three") echo 277; elseif($items=="four") echo 203; ?>" title="<?php the_title(); ?>" class="link">
(yes, this is a single line)
And I have absolutely no idea how to add such lines to $output.
$output .= '<a href="<?php bloginfo('template_directory'); ?>/includes/php/timthumb.php?src=<?php echo $url; ?>&h=<?php if($items=="one") echo 320; elseif($items=="two") echo 230; elseif($items=="three") echo 180; elseif($items=="four") echo 130; ?>&w=<?php if($items=="one") echo 600; elseif($items=="two") echo 420; elseif($items=="three") echo 277; elseif($items=="four") echo 203; ?>" title="<?php the_title(); ?>" class="link"> ';
Doesn't work of course (even with \'s before ' and ").
I believe there MUST be an easier way to attach all the code to return, but how?
I've tried with ob_start(); before code and return ob_get_clean(); after, but it outputs shortcode name instead of contents.
Its very hard to imagine what the problem you are trying to solve here is - although I'm not familiar with wordpress. Can't you just call the function where the output is supposed to go?
You could use output buffering - use echo/print as usual but...
ob_start();
hello();
$output=ob_get_contents();
ob_end_clean();
But that doesn't solve the problem that you still need to send to the browser at the right place in the page - and if you can do:
print $output;
in the right place, then you can surely do:
hello();
in the same place.
I agree with symcbean, but this might be a more practical approach at integrating with Wordpress: if you now have a single function called hello( ) which displays HTML, you might want to consider renaming that function to hello_content( ) (or something similar) and replace the hello( ) function with the suggestion symcbean gave you:
function hello_content( ) {
echo "foo";
}
function hello( ) {
ob_start( );
hello_content( );
return ob_get_clean( );
}
That should fix your immediate issue.
PHP Heredoc syntax will keep things looking neat and tidy and you can return the output to a variable. As long as you don't require any constants it will work fine.
You use it in this fashion:
function bar() {
$var = <<<EOV
anything here
anything there
EOV;
return $var;
}