How do I include php within php? - php

I want to include:
<?php the_field('200_200_1', 'option'); ?>
before the opening div tag in the line below...
$output .= '<div class="datebarcolor">'.$dates4.'</div>';
I am not sure how to insert the php tag in these circumstances. This is a php file, btw.
Can someone point me in the right direction?

If you're using ACF in wordpress you can use get_field() over the_field() in order to store the output in your $output variable:
$output .= get_field('200_200_1', 'option');
$output .= '<div class="databarcolor">' . $date4 . '</div>';

If you want to include the output of some other PHP code (for instance, if the_field does some echo calls) and you want to add that to the $output variable, use ob_start and ob_get_clean, like so:
ob_start();
the_field('200_200_1', 'option');
$output .= ob_get_clean(); //This appends everything to $output that was echoed since the call to ob_start
$output .= '<div class="datebarcolor">'.$dates4.'</div>';

I think you either mean:
Include the file before executing this code:
include 'yourfile.php';
// ... some code ...
$output .= '<div class="datebarcolor">'.$dates4.'</div>';
or include the file and add it's output to $output:
// start output buffer
ob_start();
include 'yourfile.php';
// get buffer contents and clean the buffer
$output .= ob_get_clean();
$output .= '<div class="datebarcolor">'.$dates4.'</div>';

Related

How to show previous_post_link () / next_post_link () in shortcode - Wordpress

I have this shortcode (I wrote it in functions.php) that shows the read previous and read next links in a single post:
<?php
function prev_next_buttons_post() {
$html = '';
$html .= '<div class="container-single-post-buttons">';
$html .= '<div class="prev-next-buttons">' . previous_post_link('%link', '< read previous') . '</div>';
$html .= 'back to blog';
$html .= '<div class="prev-next-buttons">' . next_post_link('%link', 'read next >') . '</div>';
$html .= '</div>';
return $html;
}
add_shortcode('prev_next_buttons', 'prev_next_buttons_post');
The problem I have is that when I add the shortcode:
<section>
<div class="container btd-container-sm">
<?php echo do_shortcode('[prev_next_buttons]') ?>
</div>
</section>
when inspecting it shows the different structure and the links are outside their containers losing their styles:
I need them to be in the same structure as defined in my shortcode.
How could I solve this? Please help.
This is because previous_post_link and next_post_link do not return a value, but write to the output buffer directly.
You need to use their counterparts that return the value, so that you can then concatenate those into your string - get_previous_post_link and get_next_post_link.
(If you check the source code for the former two functions, you’ll see that they are just wrapper functions that call the latter two, and echo their return values - https://developer.wordpress.org/reference/functions/previous_post_link/#source)

How to add a link to a list in php

Hi I have a Api list that is been generated using php here is the code that is been used to generate the list
<html>
<title> API LIST</title>
<body>
<?php
$jsondata = file_get_contents("api_link");
$json = json_decode($jsondata, true);
$output = "<ul>";
foreach($output['A'] as $schools){
$output .= "<li>".$schools['name']."</li>";
}
$output .="</ul>";
echo $output;
?>
the list is populated successfully but how can i add a link to the list so that when a user click on one of the items it opens a particular linked page here are the ways i have tried
$output .= "<li ".$schools['name']."</li>";
$output .= "<li>".$schools['name']. "</li>";
$output .= "<li>".$schools['name']."</li>";
I don't know where or how to add the a href code
Use
$output .= '<li>'.$schools['name'].'</li>';
You just need some changes in your code.You can try for
$output .= '<a href=https://www.google.com'.$schools['name'].'><li>'.$schools['name'].'</li></a>';
As per the comments you can use . to concatenate(join) the string and variable in php.

Echo / printr inside variable

I have inside a php file a variable called $html which is gathering lots of information like this:
$html .= something;
$html .= something else;
etc
and in another file its being echoed like this:
echo $this->html;
What i need is at first file that $html .= is used to echo something like this:
echo '<pre>';
echo 'printr($this->cart)';
echo '</pre>';
But i need those 3 lines to be included in $html variable in order to be echoed at second file through: echo $this->html;
Any ideas?
Thank you in advance
use var_export() function for including your array in to $html variable
In php file just assign what you want to assign to $html variable then you can print it anywhere by it's variable name .
for example :
$html="";
$html .="your text";
$html .="any dynamic values";
then echo this like
echo $html;
if you are using function then simply call a function and pass your all code within function then called function from anywhere.

How do I populate a variable using a function?

I have the code below on a page basically what I'm trying to do is fill $content variable using the function pagecontent. Anything inside pagecontent function should be added to the $content variable and then my theme system will take that $content and put it in theme. From the answers below it seems you guys think I want the html and php inside the actual function I don't.
This function below is for pagecontent and is what I'm currently trying to use to populate $content.
function pagecontent()
{
return $pagecontent;
}
<?php
//starts the pagecontent and anything inside should be inside the variable is what I want
$content = pagecontent() {
?>
I want anything is this area whether it be PHP or HTML added to $content using pagecontent() function above.
<?php
}///this ends pagecontent
echo functional($content, 'Home');
?>
I think you're looking for output buffering.
<?
// Start output buffering
ob_start();
?> Do all your text here
<? echo 'Or even PHP output ?>
And some more, including <b>HTML</b>
<?
// Get the buffered content into your variable
$content = ob_get_contents();
// Clear the buffer.
ob_get_clean();
// Feed $content to whatever template engine.
echo functional($content, 'Home');
As you are obviously a beginner here's a much simplified, working version to get you started.
function pageContent()
{
$html = '<h1>Added from pageContent function</h1>';
$html .= '<p>Funky eh?</p>';
return $html;
}
$content = pageContent();
echo $content;
The rest of the code you post is superfluous to your problem. Get the bare minimum working first then move on from there.
Way 1:
function page_content(){
ob_start(); ?>
<h1>Hello World!</h1>
<?php
$buffer = ob_get_contents();
ob_end_clean();
return $buffer;
}
$content .= page_content();
Way 2:
function page_content( & $content ){
ob_start(); ?>
<h1>Hello World!</h1>
<?php
$buffer = ob_get_contents();
ob_end_clean();
$content .= $buffer;
}
$content = '';
page_content( $content );
Way 3:
function echo_page_content( $name = 'John Doe' ){
return <<<END
<h1>Hello $name!</h1>
END;
}
echo_page_content( );

php evaluate code before getting file content

I have a file B590.php which is having a lot of html code and some php code (eg logged in username, details of user).
I tried using $html = file_get_content("B590.php");
But then $html will have the content of B90.php as plain text(with php code).
Is there any method where I can get the content of the file after it has been evaluated?
There seems to be many related questions like this one and this one but none seems to have any definite answer.
You can use include() to execute the PHP file and output buffering to capture its output:
ob_start();
include('B590.php');
$content = ob_get_clean();
function get_include_contents($filename){
if(is_file($filename)){
ob_start();
include $filename;
$contents = ob_get_contents();
ob_end_clean();
return $contents;
}
return false;
}
$html = get_include_contents("/playbooks/html_pdf/B580.php");
This answer was originally posted on Stackoverflow
If you use include or require the file contents will behave as though the current executing file contained the code of that B590.php file, too. If what you want is the "result" (ie output) of that file, you could do this:
ob_start();
include('B590.php');
$html = ob_get_clean();
Example:
B590.php
<div><?php echo 'Foobar'; ?></div>
current.php
$stuff = 'do stuff here';
echo $stuff;
include('B590.php');
will output:
do stuff here
<div>Foobar</div>
Whereas, if current.php looks like this:
$stuff = 'do stuff here';
echo $stuff;
ob_start();
include('B590.php');
$html = ob_get_clean();
echo 'Some more';
echo $html;
The output will be:
do stuff here
Some more
<div>Foobar</div>
To store evaluated result into some variable, try this:
ob_start();
include("B590.php");
$html = ob_get_clean();
$filename = 'B590.php';
$content = '';
if (php_check_syntax($filename)) {
ob_start();
include($filename);
$content = ob_get_clean();
ob_end_clean();
}
echo $content;

Categories