Is there a better way to show hyper links in php than using
<?php echo $link; ?>
To avoid repeating $link, you can use this:
<?php printf('%1$s', $link); ?>
<?=$link?>
<?php echo ''.$link.''; ?>
Related
I have a shortcode in a theme that displays only when a user is logged out. Usually, I would use the following shortcode to achieve this
[logged_out]Content[/logged_out]
How would I achieve this in PHP?
<?php echo do_shortcode("[logged_out]"); ?>
Can you just try the code like below,
<?php
$string = 'Hello world';
echo do_shortcode('[logged_out]'.$string.'[/logged_out]');
?>
you can assign any value to the variable $string ;
Or you can directly use like this,
<?php
echo do_shortcode('[logged_out]Hello world[/logged_out]');
?>
For more information please refer,
https://developer.wordpress.org/reference/functions/do_shortcode/
you can use following way in your php file.
echo do_shortcode('[logged_out] your content [/logged_out]');
I'm displaying a gallery on my wordpress site using the following code:
<?php echo do_shortcode('[satellite gallery=2 auto=off caption=off thumbs=on]'); ?>
Now I've added a custom post value/field called 'guitLink' where I can add the gallery number which I want to then use in the above shortcode, so something like this:
<?php echo do_shortcode('[satellite gallery=' .get_post_custom_values('guitLink'). 'auto=off caption=off thumbs=on]'); ?>
Any ideas how to achieve this?
Use:
<?php $guitLink = get_post_custom_values('guitLink');
echo do_shortcode('[satellite gallery=' .$guitLink[0]. ' auto=off caption=off thumbs=on]'); ?>
Instead of
<?php echo do_shortcode('[satellite gallery=' .get_post_custom_values('guitLink'). ' auto=off caption=off thumbs=on]'); ?>
Im trying to add a PHP variable to a href url
This is the code:
PHP
$uid= $_SESSION['userid'];
HTML
<a href=http://example.com/uid= <?php echo ".$uid."?> /a>
How do you add, when I do it, this is what it redirect too: http://example.com/uid=.
Try this,
<?php
#session_start();
$uid= $_SESSION['userid'];
?>
<a href="http://example.com/?uid=<?php echo $uid; ?>" >Your link text</a>
echo "link description";
Perhaps something like this:
print '<a href=http://example.com/?uid=' . $uid . '>Link</a>';
try this
<?php
$url = 'https://www.w3schools.com/php/';
?>
PHP 5 Tutorial
Or for PHP 5.4+ (<?= is the PHP short echo tag):
PHP 5 Tutorial
or
echo 'PHP 5 Tutorial';
This is one of the most helpful
echo "<td>".($tripId != null ? "<a target=\"_blank\"href=\"http://www.rooms.com/r/trip/".$tripId."\">".$tripId."</a>" : "-")."</td>";
It will work!
This will work
<a href=http://example.com/?uid= <?php echo $uid ?> Myurl</a>
Try this:
<a href=http://example.com/uid= <?=$uid?> /a>
You're doing a couple things wrong.
In HTML you should quote attribute values such as href
In PHP you're not concatenating anything, just echoing
You forgot the link text
For readability, you can (probably) use the <?= shorthand instead of <?php echo
<a /a> is broken tag syntax. Should be <a>...</a>
End result is this:
Link Text
Currently, as an example, I have this php code on one of my pages.
<?php include("codes/games/scripts/titles/angrybirds.php"); ?>
How would I change this to make my variable:
$gametitle = "angrybirds";
work like this:
<?php include("codes/games/scripts/titles/$gametitle.php"); ?>
Thanks in advance for your help!
That code should work as-is, since the . won't be interpreted as part of the variable name. You can see the output of it here: http://codepad.org/ZbtiOPgB
However, for readability, I would encourage you to either use clear concatenation:
<?php include("codes/games/scripts/titles/".$gametitle.".php"); ?>
Or wrap your variable name(s) in { and }:
<?php include("codes/games/scripts/titles/{$gametitle}.php"); ?>
<?php include('codes/games/scripts/titles/'.$gametitle.'.php'); ?>
Or:
<?php include('codes/games/scripts/titles/'.$gametitle.'.php'); ?>
<?php
$file = 'codes/games/scripts/titles/' . $gametitle . '.php';
if (file_exists($file))
require_once($file);
?>
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.