How to add PHP in PHP - php

Forgive me, my PHP skills are rough to say the least, I am trying to pass PHP into PHP see below
<?php echo do_shortcode('[tab name="Info"]
<span>Address:<?php echo get_field('address'); ?></span>
[/tab][end_tabset]'); ?>
I don't know what I am doing wrong and I have been searching everywhere, but nothing seems to work.
I appreciate any help.

<?php
echo do_shortcode('[tab name="Info"]
<span>Address:' . get_field('address') . '</span>
[/tab][end_tabset]');

Related

Advanced Custom Fields in Wordpress - "if" statement troubles

I was hoping somebody may be able to help me. I'm having trouble with if statements with the Advanced Custom Fields plugin for Wordpress. I've got three options the user can choose from, all three can be chosen, but they can also choose just one if they wish.
The issue I'm having is the code I've written displays all of the HTML tags, even the empty ones. This is causing styling issues. I want to be able to just show HTML that has been populated. I've tried the solutions on the ACF forums but to no avail.
Link: http://www.advancedcustomfields.com/resources/getting-started/code-examples/
Here's the quick (newbie!) code I've got at the minute:
<?php the_sub_field('link'); ?>
<?php the_sub_field('doc'); ?>
<p><?php the_sub_field('cap'); ?></p>
I looked on the ACF forum and tried this, but it broke the theme:
<?php if(the_sub_field('link')) {
echo '' . the_sub_field('link') . '';
} ?>
<?php if(the_sub_field('doc')) {
echo '' . the_sub_field('doc') . '';
} ?>
<?php if(the_sub_field('cap')) {
echo '<p>' . the_sub_field('cap') . '</p>';
} ?>
I'm looking for some help to make this work. I don't think I'm too far away from the right answer, however I'm a bit of a rookie with anything beyond standard front-end stuff, any thoughts would be very much appreciated.
Thanks!
Try to use get_sub_field();
<?php if(get_sub_field('link')) {
echo '' . the_sub_field('link') . '';
} ?>
<?php if(get_sub_field('doc')) {
echo '' . the_sub_field('doc') . '';
} ?>
<?php if(get_sub_field('cap')) {
echo '<p>' . the_sub_field('cap') . '</p>';
} ?>
When looping through one of these fields, this function returns a sub field from the current row.
Like Dk-Macadamia said, try to use get_sub_field() in loops instead of the_sub_field()
the difference is get_sub_field() return the value as a string, and the_sub_field() print the data,
Also get_sub_field() only work under a repeater/ fluid field type otherwise wont work,
if its not a sub field of repeater/fluid fields just try get_field()

general php syntax

how do i put this <?php echo $cfs->get('exclude'); ?> within the double quotes in this <?php echo do_shortcode('[gallery exclude=""]'); ?>
meaning, what is the correct syntax to merge the two? i attempted to read through some of this to solve it, but have little php knowledge.
thank you.
sprintf('[gallery exclude="%s"]', $cfs->get('exclude'));
See sprintf()
Also readable:
$exclude = $cfs->get('exclude');
"[gallery exclude=\"$exclude\"]";
this is one way
echo do_shortcode('[gallery exclude="' . $cfs->get('exclude') . '"]'); ?>
<?php echo do_shortcode("[gallery exclude='$cfs->get('exclude')]'); ?>

Extracting basic PHP echo from IF statement

The following code is for a Wordpress plugin, it displays points and tank of a user:
<?php
if(function_exists('cp_displayPoints') && $authordata->ID){
echo '<span class="cubepoints_buddypress">'; cp_displayPoints($authordata->ID); echo '</span>';
if(function_exists('cp_module_ranks_getRank')) echo ' <span class="cupepoints_buddypress_rank">'.cp_module_ranks_getRank($authordata->ID).'</span>';
}
?>
I am trying to extract these two echo functions from the If statement but only succeeded with one of them. I can echo the points like this:
<?php cp_displayPoints($authordata->ID); ?>
Works fine. Now I tried doing the same with the second echo:
<?php cp_module_ranks_getRank($authordata->ID); ?>
But it did not work. Obviously, there is some basic thing that I am missing here. Do you know what it is?
The first one likely prints directly to output, while the second returns its value. So, you need to echo() the second one, just as they're doing in your sample code:
<?php echo cp_module_ranks_getRank($authordata->ID); ?>

Getting echo within echo to work together

In a little trouble with this, is they any way i can get to echo's working together see code below,
<?php $youtube_vimeo_player = get_post_meta($post->ID,'_youtube_vimeo_player',TRUE); ?>
<?php echo $video->embed(' <?php echo $youtube_vimeo_player['url']; ?> ', '', ''); ?>
I'm wanting the info brought from the vimeo_player url to be entered into the video->embed section.
Any help on this would much be appreciated : )
try it like this:
<?php $youtube_vimeo_player = get_post_meta($post->ID,'_youtube_vimeo_player',TRUE); ?>
<?php echo $video->embed( $youtube_vimeo_player['url'], '', ''); ?>
Replace
' <?php echo $youtube_vimeo_player['url']; ?> '
with
"{$youtube_vimeo_player['url']}"
You dont need echoing inside the php string. Note that { and } are the special way to embed array index, or object method call into the string, they are not present in final string.
Btw, it's sufficient to just do
echo $video->embed($youtube_vimeo_player['url'], '', '');
As $youtube_vimeo_player['url'] already shoud be the string
<?php echo $video->embed("'".$youtube_vimeo_player['url']."'", '', ''); ?>
PHP isn't that crazy, but thanks for remembering us it could seem to.
Thou shall write:
<?php
$youtube_vimeo_player = get_post_meta($post->ID,'_youtube_vimeo_player',TRUE);
$url=$youtube_vimeo_player['url'];
$video->embed($url, '', '');
?>
Educate yourself on the concept of variable, and remember, php has only one level of embedding (that is one level of <?php ... ?> — no nesting)
And believe it, it's better this way.

Make HTML tag in PHP

I have some php code $_SESSION['username'] and I need to display it using html with something like <p>the username here</p>. I heard you might be able to do this using echo in PHP but Im not sure how. Thanks for any help.
echo '<p>' . $_SESSION['username'] . '</p>';
or
<p><?php echo $_SESSION['username'] ?></p>
You should really run it through htmlspecialchars to avoid breaking your HTML syntax (for example if the user's name contains </p> or something, or if they attempt an XSS attack):
echo '<p>' . htmlspecialchars($_SESSION['username']) . '</p>';
If I understood you correctly, I believe you are looking for:
<p><?php echo $_SESSION['username']?></p>
<?php echo '<p>'.$_SESSION['username'].'</p>'; ?>
Check out the documentation:
http://www.php.net/manual/en/tutorial.useful.php

Categories