Adding content between a PHP shortcode - php

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]');

Related

Is it possible to add css style to print_r function in codeigniter php?

I have to display my username from a database and it's done using the print_r method. Now I would like to add some CSS functions to make the output large and bold.
Is it possible to add such a style to print_r?
<?php print_r($this->session->userdata['userName']); ?>
In your controller, use this
$data['username'] = $this->session->userdata['userName'];
And in you template file, use this
<div class="your-class"><?php echo $username?></div>
Its preety simple. I suggest you to spend sometime about mvc work flow.
Try to do in this way...
<?php
$styleArray = array('apple','banana','carrot');
echo '<pre><h1>';
print_r($styleArray);
echo '</pre></h2>';
?>
You can also add whatever class u wish.
u can use function in php like this
<?php
pre($this->session->userdata['userName']);
function pre($str = null){
echo "<pre>";
print_r($str);
echo "</pre>";
}
?>

How to echo php code and use it?

<?php echo $row["html"]; ?>
Inside of the $row["html"] there's:
<?php $Site->Nav($owner); ?>
but when I echo it, it only echoes:
Nav($owner); ?>
How may I print the full and make it usable, which means that it will print the function Nav?
I've tried to replace <?php with [[// i the database, and just before echoing it, I change back with replace. But without success
I think you need to use eval function of php. See the example below.
$string = 'cup';
$name = 'coffee';
$str = 'This is a $string with my $name in it.';
echo $str. "\n";
eval("\$str = \"$str\";");
echo $str. "\n";
Might be it can help.
Use eval function. It might solve your problem like this:
<?php echo eval($row["html"]); ?>
Keep the code as is in DB as if you are writing it in PHP file but without PHP opening and closing tags i.e. <?php and ?>. I haven't checked this (as i am not sure what $Site->Nav($owner); will do) but hope it would work in this case.
If I understand correctly you are wanting to output the results of $Site->Nav($owner);
I have no idea what this is expected to output, but assuming it is a string of some kind that you wish to display (hence echo) - an example of achieving this would be calling your code and have that method return the value, so you can echo it out. Ie:
function Nav($owner){
// Do your stuff
return 'Your Desired Output';
}
Then on your page you would have
<?php echo $Site->Nav($owner); ?>
Which would echo "Your Desired Output".

PHP shortcode input variable

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]'); ?>

site_url with path and custom variable

I'm not very familiar with PHP and have been trying my hardest to figure out how to create this URL. So far, this is working:
<?php echo site_url($p->post_title) ?>
Where post title is defined by the Mapify.it Wordpress plugin. The result is:
http://siteurl.com/post_title
What I'd like to do is add a string before it, ideally ?s= or /search/, but when I try to add this before $p->post_title I'm still generating the above URL. Variations such as:
<?php echo site_url('?s=', $p->post_title) ?>
<?php echo site_url('/search/', $p->post_title) ?>
produce http://siteurl.com/?s= and ignore the variable. Nothing seems to do what I want.
What am I doing wrong?
Hope you need the following url format,
http://siteurl.com/?s=Here come the post title
So,
<?php echo site_url("?s=".$p->post_title) ?>
OR
<?php echo site_url("/search/".$p->post_title) ?>
should work.
Found it!
<?php echo site_url('?s='), $p->post_title ?>
Instead of adding custom URL Parameters directly, I'd suggest you to use WordPress built-in function add_query_arg(), it's more cleaner.
Here is an usage example:
$url = get_site_url();
$params = array(
's' => $p->post_title
);
echo add_query_arg($params, $url);
You can specify multiple parameters this way.
For ref: Check add_query_arg()

Is this possible to use a variable for an url in php?

I'm calling a function like this:
<?php print get_thumbnail('http://url.com/?skin=rss'); ?>
Being a php newbie, I'm wondering if there is a way to change the http://url.com part based on a custom metadata I have set up in Wordpress. So I guess it would look something like this:
<?php print get_thumbnail('<?=$video_src?>/?skin=rss'); ?>
Is something like this possible?
Yes, you have the right idea, you just don't need to re-open PHP tags since you're already inside some. You can use . to concatenate (join together) the value of $video_src and "?skin=rss".
<?php print get_thumbnail($video_src . "?skin=rss"); ?>
Try this:
<?php print get_thumbnail($video_src . '/?skin=rss'); ?>
Keep in mind that <?= $foo ?> is shorthand for <?php echo $foo; ?>. <?= ?> won't be expanded in strings, but you can achieve something similar using double quoted strings:
<?php print get_thumbnail("$video_src/?skin=rss"); ?>
Yes, except within PHP, you don't need to enter the PHP tags again.
<?php print get_thumbnail($video_src . '/?skin=rss'); ?>

Categories