Get a variable from another file in wordpress - php

I'm kind of new to wordpress coding and I've been trying to get a variable from another file.
I have this variable $final_cat_url in /custom/last-category.php that I want to reuse in customtemplate.php.
I've read lots of explanations and the codex, but it's still not working.
I've tried to use the following code in customtemplate.php
get_template_part( 'custom/last-category', null, array('my_final_cat_url'=> $final_cat_url));
echo $args['my_final_cat_url'];
Can you help me with that? Thanks a lot.

Add this function to your functions.php file:
function includeWithVariables($filePath, $variables = array(), $print = true){
$output = NULL;
if(file_exists($filePath)){
// Extract the variables to a local namespace
extract($variables);
// Start output buffering
ob_start();
// Include the template file
include $filePath;
// End buffering and return its contents
$output = ob_get_clean();
}
if ($print) {
print $output;
}
return $output;
}
Instead of using get_template_part(), use this:
<?php includeWithVariables('file_to_include.php', array('final_cat_url' => $final_cat_url)); ?>
In the file you included:
<?php echo $final_cat_url; ?>

Related

Include a php file, but return output as a string instead of printing

I want to include a file, but instead of printing output I want to get it as string.
For example, I want include a file:
<?php echo "Hello"; ?> world!
But instead of printing Hello world! while including the file I want to get it as a string.
I want to filter some elements from the file, but not from whole php file, but just from the html output.
Is it possible to do something like this?
You can use php buffers like this:
<?php
ob_start();
include('other.php');
$script = ob_get_contents(); // it will hold the output of other.php
ob_end_clean();
EDIT: You can abstract this into a function:
function inlcude2string($file) {
ob_start();
include($file);
$output = ob_get_contents(); // it will hold the output of other.php
ob_end_clean();
return $output;
}
$str = inlcude2string('other.php');

Call a php function in another php file?

I need to call a function from index.php in another file example.php. If I use include it also takes all the html from index.php. I just want the result of the function. Any way to do this?
$rs = odbc_exec($con, $sql);
if (!$rs) {
exit("There is an error in the SQL!");
}
$data[0] = array('D','CPU_Purchased_ghz');
$i = 1;
while($row = odbc_fetch_array($rs)) {
$data[$i] = array(
$row['D'],
$row['CPU_Purchased_ghz']
);
$i++;
}
//odbc_close($con); // Closes the connection
$json = json_encode($data); // Generates the JSON, saves it in a variable
echo $json;
Basically that piece of code in index.php takes info from a file which queries a db and encodes it in json. Instead of echoing I wanted to make a function that echos the json and call it in a new file to only get the json displayed on the page
create a functions.php file. Add the function to that file and include the file in your example.php file
Include the file before you call the function.
See below examples:
index.php
<?php
function myFunction() { //function .
return "FirstProgram"; //returns
}
?>
Now Using include http://php.net/include to include the index.php to make its content available for use in the second file:
example.php
<?php
include('index.php');
echo myFunction(); //returns myFunction();
?>
index.php
function myFunction() {
return "It works!";
}
example.php
include('index.php');
echo myFunction();

Executing another PHP file and return the output from a method, is that possible?

Suppose if I have a PHP script test.php which have a method
<?php
function execute($filename){
//do something
return $output;
}
?>
and I also have another PHP script executable.php
<?php
echo "I am executed";
?>
then can i run any code to excute the second file and return the output from the first method execute when i call echo execute('executable.php'); ?
I guess you guys can understand what I meant.
You can use output buffering, as long as the file being included doesn't already do that:
ob_start();
require $filename;
$content = ob_get_contents();
ob_end_clean();
return $content;
Use ob_Start and ob_get_contents to capture the output of the script. Something like this should work:
<?php
function execute($filename){
ob_start();
include $filename;
$output = ob_get_contents();
ob_end_clean();
return $output;
}
<?php#test.php
include 'executable.php';
echo $test;
?>
<?php#executable.php
$test = "I am executed";
?>
function execute($filename){
include_once($filename);
}
$filename is the name of your file to be included.. I think this will help you...
This is the function call..
execute('abc.php');

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