Php inside php to process custom field in wordpress - php

I have a code which returns gallery number 1 with the set of images.
<?php echo photo_gallery(1); ?>
But I need to change number "1" to number "2", "3" and so on with custom fields in every post using this code
<?php the_field('number'); ?>
inside the first code
I need something like this:
<?php echo photo_gallery( <?php the_field('number'); ?> ); ?>
Result:
<?php echo photo_gallery(2); ?>
or
<?php echo photo_gallery(3); ?>

Something like this?
<?php
echo photo_gallery(the_field('images'));
?>

You don't need nested PHP tags. Omit the php tags inside and your code should run.
<?php
echo photo_gallery(get_field('number'));
?>
Note the difference between get_field and the_field. ACF docs explain which to use in which situation.
Essentially, the functions inside (to the right) get executed first, returning a value which can be used by the outer function (to the left).
In the background, a programming language takes these steps:
execute get_field('one') and return 1.
execute photo_gallery(1) and return (photo).
echo (photo)
There are some courses on PHP, many of which are free - that might help you sort through some of these issues.

Related

Insert data in HTML from php

I trying to put user data from PHP in HTML form, like name, email, address, etc. But i don't know what i have to do to work it.
Already tried:
<?php
session_start();
$test='some name';
$_SESSION['name']='some name';
?>
<!-- AND -->
and nothing happened.
SOLVED CODE:
<?php
session_start();
$test='some name';
?>
<a><?= $test ?></a>
The PHP manual has a page introducing the PHP tags:
When PHP parses a file, it looks for opening and closing tags, which are which tell PHP to start and stop interpreting the code between them.
Note that "interpreting the code" is not the same as displaying the result of that code. For instance, you could write <?php $test = 'hello'; ?> to assign a new value to a variable, and it won't cause anything to be output.
To output a string, you can use the echo and print keywords. Note that these are not functions, so do not have parentheses around their arguments; as noted in the manual, including redundant parentheses will sometimes work, but can be misleading. So the following output the variable $test into the page:
<?php echo $test; ?>
<?php print $test; ?>
As the PHP tags page I linked to earlier says:
PHP includes a short echo tag <?= which is a short-hand to the more verbose <?php echo.
So you can also use:
<?= $test ?>
However, you also have a second problem, which is nothing to do with PHP, and will happen if you hard-code a URL into your HTML like this:
Nothing will appear in the browser! Why? Because you've defined the destination of a link, but haven't defined any text or content to click on; you need it to look like this:
Click me if you dare!
So put that together, you might write this in PHP:
<?= $linkDescription ?>
Change this
to this
or to this if you want to see the data on the html page
<?php $test; ?>
<?php echo($test); ?>
Do note that there are better ways to display such data. Check this out:
https://www.w3schools.com/php/php_echo_print.asp

php syntax for big block of echo <?php if(1): ?> html<?php endif; ?> correct way

Well most is in the title. I wonder if it's supposed to be that way or i can do the same without an if(1) condition I'm doing this because my website pages are all as php includes.
Thank you all
Answer retained:
Okay basically the way to do it is simply to include('file.php') as it will be considered out of the current <?php ?> environment.
Putting
<?php if(1): ?>
...
<?php endif; ?>
around your HTML code in a PHP file will have no effect on the result. You will still be able to include the file without it.
You can think of it like the "default mode" for a PHP file is that it contains HTML content. You only need to add <?php ?> tags if you want to add PHP code. If you're just putting HTML code in a PHP file, they're unnecessary.
The beauty of PHP is that you can move "in" and "out" of PHP very easily. You can do the following without issues:
<?PHP
if(whatever) {
?>
your HTML
<?php
include('whatever.php');
?>
more HTML
<?PHP
}
?>
To build on Zak's answer:
You can also use PHP to echo out things that aren't PHP... as long as you quote it appropriately.
<?php
//HTML
while ($x < 5) {
echo "<p> this is html that you can wrap with html tags! </p>";
$x++;
}
//Javascript
echo "<script type='text/javascript'>
some javascript code
</script>"
?>
Although, it's less confusing to just end the php tag to keep things separate.
And you can even use php as you want within html or javascript as long as you put the tags, and as long as the file is saved as a .php file (so PHP can be processed on the server).
Ex:
<script type="text/javascript">
//set a javascript image array to a php value
var imgArray = [<?php echo implode(',', getImages()) ?>];
</script>
But if you want to do this the other way around (IE, assign a browser-compiled value, such as a javascript value to a php value), you'll need to use AJAX.

Conditional embed HTML between PHP code blocks?

I'm fairly new to PHP. I started learning it like 3 weeks ago. I cannot find the answer to this question on StackOverflow, Google or Youtube. The PHP documentation to this just confuses me. To get on with the question, how does PHP code mixed in with HTML work?
<?php if (something) { ?>
<p>Hello</p>
<?php } ?>
The p element will only display if something has a truthy value, how is this?... I thought for sure that the PHP engine ignored what was going on around the outside of the codeblocks (e.g. <?php ?>) and only parsed what happens on the inside.
The code below gets parsed by the PHP engine normally and sent to the browser without affecting any HTML elements (even though its clearly between 2 code blocks).
<?php echo $something; ?>
<p>Hello</p>
<?php echo $something; ?>
I hope I'm not going to get flamed for asking this question since a lot of people seem to understand how it works in like a tenth of second.
P.S. I asked this question in chat early and thought I understood it correctly but when I went to implement it my mind was still like, how does this work exactly? It just seems like some kind of hack to me.
Easy now. Definitely need a php tutorial for you to start on http://www.tizag.com/phpT/
Here is what your doing:
<?php
//Anything inside me php processes
if($something)
{
echo "<p>something</p>";
}
//About to stop processing in php
?>
<p>Anything outside of the php statement above will just be printed to the dom</p>
Quick Note: It is good practice to separate your PHP from your HTML
<?php if ($something) { ?> <-- where is the other {
<p>Hello</p>
<?php } ?> <-- oh I see it.
In your first example it is indeed true that <p>Hello</p> will be rendered if and only if 'something' returns true.
If you close a php tag with ?> but have an 'unclosed' execution, like if (blah) { ..., the PHP engine understands your desires and does accordingly.
Why?
The PHP engine is kept 'waiting' until the execution is closed with } and then the final result is evaluated and the browser continues on with the lines below.
Obviously if you leave out the final } you will see some errors, which tells you that PHP was expecting you to finish what you started and you did not
Both the php and html are parsed in-line. So, as it moves down your script it will run php scripts within tags, and display html in the order which they are placed. For example:
<? $someVar = "someVar string value"; ?>
<h1>This is a title</h1>
<? if(1 == 1){?>
<p>This paragraph will appear in between the header tags because 1 == 1 is true</p>
<? } ?>
<h3>Another header which will follow the paragraph</h3>
<p>The value of someVar is: <?=$someVar;?></p> // <?= is a short hand for echo
This will show as:
<h1>This is a title</h1>
<p>This paragraph will appear in between the header tags because 1 == 1 is true</p>
<h3>Another header which will follow the paragraph</h3>
<p>The value of someVar is: someVar string value</p>
Basically just think of it as the server reading down your script and parsing whatever it sees as it goes. If there is html, it will display it and if there is php which does some sort of calculation and then spits out html, it will show the spat out html.
You can write php code anywhere in HTML using php code block
<?php echo "whatever " ?>
or
<?php echo "<h1>Here everything will displayed in h1 </h1> "; ?>
and if you use control structure ( if, switch etc.. ) then it will behave like all other languages, means if something is true then it will execute the part written between { }.
so if you write an undefined variable in if condition then it will not execute code block of because undefined variable is treated as false condition .
additionaly you can check any variable value by var_dump($variable)
PHP's Alternative syntax for control structures
<!DOCTYPE html>
...
<div>
<?php if ( the_thing === true ) : ?>
<p>The thing is true! \o/</p>
<?php else if ( the_other_thing === true ) : ?>
<p>The other thing is true! meh</p>
<?php else : ?>
<p>Nothing is true :-(</p>
<?php endif; ?>
</div>
...

Drupal6: Append query string to primary links

I'm trying to add a query string "?device=mobile" onto all primary links programmatically.
In my theme's page.tpl.php file I've tried the following,
<?php if (is_array($primary_links)) : ?>
<?php foreach ($primary_links as $link): ?>
$link['href'] = $link['href'].'?device=mobile';
<?php endforeach; ?>
<?php endif; ?>
However this simply prints out the code onto the page. Currently I'm trying to use hook_menu_link_alter, but so far I haven't been successful. To test my code on just one primary link item I've tried the code below:
myModule_menu_link_alter(&$item, $map){
$items['photo_gallery']['href'] = 'photo_gallery?device=mobile';
}
Unfortunately there was no change in the link. I'm also going to investigate hook_menu_item_link() from my template.php file, but at this point I'd like it if someone could point me in the right direction, and let me know what I've done wrong.
Thanks.
The code you're putting in your page.tpl.php has the right idea, but you're missing a couple of things:
The body of the foreach loop should be surrounded by the PHP tags, so that PHP will interpret and execute the code. This is why you see that Drupal just "prints out the code": because you're putting it out of the PHP "world", so it simply becomes part of your template's HTML.
Even if you correctly execute the code, you will not see any changes, because by default, the $link variable in your foreach loops is a copy of the original item in the array, so doing $link['href'] = 'stuff' won't modify the original. To modify the original, you can use the reference syntax, like: foreach ($primary_links as &$link). (Ampersand prefixed to the variable name, see PHP docs on references).
And finally, even if you fix the previous two issues, it might still not work because the link's HREF attribute is probably going to be processed by theme('links') later, and your "?" and "=" are going to get encoded and it will break the link.
So, fixing those three issues, I'd say you should modify your page.tpl.php code to look like:
<?php if (is_array($primary_links)) : ?>
<?php foreach ($primary_links as &$link): ?>
<?php $link['query'] = array('device' => 'mobile'); ?>
<?php endforeach; ?>
<?php endif; ?>
Or, if it annoys you to have to open/close the PHP on every line, just use a normal block like:
<?php
if (is_array($primary_links)) {
foreach ($primary_links as &$link) {
$link['query'] = array('device' => 'mobile');
}
}
?>
Note 1. The &$link syntax (use reference instead of copy), and 2. The query array key of the $link array, which is one of those "special" array keys that Drupal will search for, and, if found, utilize to build a proper URL query to attach to the final link (see the docs for Drupal's url() function).
Also, remember to clear the caches whenever you see that "nothing changes", especially when working on a theme.

How do I drop a space generated by a function in PHP that makes my link broken?

I need to construct a link in Wordpress based on a variable out of the URL of that page.
Although in PHP this might be easy, in WP this seems to be difficult (I understood that WP does not allow it for security reasons).
So, I added a filter in my Wordpress installation (functions.php) according to this link in order to get that variable and use it programatically in my WP post page in the construction of my link.
However, the link forms with two spaces (one before and one after I call the relevant function).
So, the code behind the link is:
<?php echo ("<a href='www.mydomain.com/purchases/?refid="); ?> <?php echorefid(); ?> <?php echo ("'>download this amazing file</a>") ?>
and the output code (in HTML) when I try to access the webpage at www.mydomain.com/?refid=3267 is:
<a href='www.mydomain.com/purchases/?refid= 3267 '>download this amazing file</a>
Please note the space before 3267 string and the space after this string.
Obviously, the link is broken.
The function echorefid() appears in the following code I added to functions.php:
add_filter('query_vars', 'parameter_queryvars' );
function parameter_queryvars( $qvars )
{
$qvars[] = 'refid';
return $qvars;
}
function echorefid()
{
global $wp_query;
if (isset($wp_query->query_vars['refid']))
{
print $wp_query->query_vars['refid'];
}
}
Any help would be highly appreciated.
That's because you have spaces in your code! Change:
<?php echo ("download this amazing file") ?>
// ^ ^
// No needs for spaces here! ---------------------------------+---------------------+
To:
<?php echo ("download this amazing file") ?>
The cause of the spaces it the fact that you have separated multiple <?php ?> <?php ?> with spaces separating them. Those spaces outside <?php ?> tags are sent as output to the browser.
You only need one of them for all the PHP code. Combine them by concatenating the three groups and switch the outer quotes to single quotes or escape the inner quotes.
<?php echo 'download this amazing file'; ?>
<?php echo ("download this amazing file") ?>
The spaces between your PHP tags were showing up in the finished HTML.

Categories