I have an include
include ('myfile.php');
Now, I am using wordpress and to get the template path I use:
<?php echo get_template_directory_uri(); ?>
My question is:
How can I use both together?
Like:
<?php
include('<?php echo get_template_directory_uri(); ?>/myfile.php');
?>
Thanks
include(get_template_directory_uri() . "/myfile.php");
The include() function just takes a string parameter. Nothing special about it.
You can use the result of the function in the include, but not by echo:ing it. Just use it directly.
<?php
include(get_template_directory_uri() . '/myfile.php');
Related
How can I get expected output from example below?
Note: I'm using $content = file_get_contents('content.php'); to use content where and when possible so it is not a direct output on screen. include() breaks the pages.
content.php
<p>Hello <?php echo 'World!'; ?></p>
reader.php
<b>Message from another file:</b> <?php echo file_get_contents('content.php'); ?>
Output of code above is:
Message from another file: Hello <?php echo 'World!'; ?>
Instead of (expected):
Message from another file: Hello World!
I think you are looking for <?php include('content.php');
file_get_contents — Reads entire file into a string
PHP.net file_get_contents - manual
The include statement includes and evaluates the specified file.
PHP.net include - manual
Try making content.php into a file that has a function that returns the content you want (you may want to have parameters). Simply require the file then call the function and save the output.
Example:
content.php
function get_content($world){
return '<p>Hello ' . $world . '</p>';
}
reader.php
<?php
require('content.php');
$content = get_content('world');
?>
<b>Message from another file:</b> <?php echo $content; ?>
Since you cannot use include (though I don't understand fully why), but want the file to be parsed and executed as PHP code, you can use eval
<b>Message from another file:</b> <?php eval(file_get_contents('content.php')); ?>
But the file content.php should not contain <?php and ?> tags, as stated at http://php.net/eval.
I have been trying to get the current directory link and come up with something like this as:
<?php echo "http://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]paypal_success.php"; ?>
but really that didn't work as I got the output as :
http://localhost/php-login-new/invoice.phppaypal_success.php
so really I want to trim the invoice.php from it so if you can guide me please how I can do that..it will be great..!
Try to use dirname()
<?php
echo "http://$_SERVER[HTTP_HOST]".dirname($_SERVER[REQUEST_URI])."/paypal_success.php";
?>
$path = test/invoice.phppaypal_success.php
you should use basename($path), result is invoice.phppaypal_success.php
or you can use basename($path,".html"), result is invoice.phppaypal_success
$result_path = basename($path)
then you can use substr() in php
echo substr($result_path,5,-22);
Maybe this can help you
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 include an external file inside an echo?
This is what I am trying:
echo 'stuff'.(include($_SERVER["DOCUMENT_ROOT"].'/theme/button.php')).'morestuff';
I could simply write 3 lines but I wanted to know for simplification purposes.
You should just use include.
<div>
<?php include $_SERVER["DOCUMENT_ROOT"].'/theme/snippets/follow-button.php'; ?>
</div>
I tested this and it works... I only put HTML code in the 'button.php' file...
echo 'stuff'; include($_SERVER["DOCUMENT_ROOT"].'/theme/button.php'); echo 'morestuff';
No. echo implicity calls __toString() on whatever is contained within the statement to be echo'd.
What you should do is include the file and have the HTML in the included file. No need to echo.
You certainly can. If the included file is returning anything, that will be printed. Includes basically evaluate the file and can be used as a normal function.
It would probably be cleaner for the include file to define a function that returns the value you want. Then you would do:
include($_SERVER["DOCUMENT_ROOT"].'/theme/button.php');
echo echo 'stuff'.button_func().'morestuff';
There's no need to do this. Just break the echo. Example below...
<?php
echo "
$value1 <div id='1'></div>
$value2 <div id='2'></div>
// I want my include here
$value3 <div id='3'></div>
";
?>
Change above to;
<?php
echo "
$value1 <div id='1'></div>
$value2 <div id='2'></div>
";
include 'some_file.php';
echo "
$value3 <div id='3'></div>
";
?>
I'm trying to substring the title which it is called via the method the_title() .
Here are 2 things I did to do it but they both failed.
first try: <?php echo substr(the_title(),1,15) ?>
second try: <?php $new_title = the_title() ; echo substr($new_title, 1,15) ?>
They both didn't work. when I used the second try I still get the full title.
Note: I'm trying to implement this on a Wordpress script, also it's for my php practice.
Thanks in Advance.
The function the_title() does not return the title as string but outputs it by default.
Use the_title('','', 1) which makes it return the title or the alternative function get_the_title() instead.