Is it possible to call PHP in html? - php

I have a PHP that returns some HTML code, for example, if I execute this in the browser,
http://www.myserver.com/myscript.php?size=300
this will return some html code like this,
'<div><img src="..." /></div>'
I am wondering if it's possible I can call this php script in my html code directly? For example, if I want to use the output from the PHP script in my Wordpress sidebar widget?

You can use file_get_contents() like this:
<div class="sidebar">
<?php
$html = file_get_contents("http://www.myserver.com/myscript.php?size=300");
echo $html;
?>
</div>

If your Current page's extension is not php, you cannot call php in it directly.
You have to use some javascript/jquery/ajax to run the php file externally and load the data into a class or id using one of those javascript based code.
If your current page is already a php file than,you can use include(); or require(); functions and it does the job.

above answers are correct, but that might be not concerned for wordpress, that are of core php functionality.
i have a different way for sidebar use, that's provide by wordpress already.
it is an inbuilt functionality, please take a look towards my answer.
Show active sidebar in Wordpress
How to make wordpress sidebar
you can found ::
try this one, it is different way of using sidebar
[1] put your all "left_bar"(sidebar) code in a new file named "sidebar-left_bar"
[2] save it with header.php, function.php and all files (saving location)
[3] now just use <?php get_sidebar( 'left_bar' ); ?> where you want to use
Thanks

Related

including php file in thickbox inline view in wordpress

In one of my wordpress plugins, I am using thickbox function to call another php file in the same plugin directory. The thickbox is displaying content as long as the content is from the same file, if I include another php file in the thickbox, it won't work. Please help.
Here is my code.
<?php
add_thickbox();
include plugin_dir_path(__FILE__) . '/my_php_file.php'; ?>
<div id="cnt-id" style="display:none;">
<h1><?php _e('Select an Item', 'txt-domain'); ?></h1>
<?php print_r($tpl_list);?>
</div>
The thickbox anchor link
<a href="#TB_inline?width=600&height=550&inlineId=cnt-id" class="thickbox">
<?php _e('Open thickbox', 'txt-domain'); ?>
</a>
So when the thickbox page is loaded, it only shows the h1 heading. the array which I am trying to print is just static array created in that included php file.
Any help will be highly appreciated.
Thanks
Just for someone in the similar situation. This is what I found as solution.
I used the absolution URL of the file on my the same location for thickbox with TB_iframe parameter. And I called a separate php file which is including another one with the array data.
That file which I didn't want to be directly accessible, I added the http_referer check via PHP to check if the ref url has wp-admin/post.php in there. That is not a rock solid hack proof but still helps somewhat.
Thanks

Execute the Code in a PHP Page And Echo the HTML Output

I absolutely don't post a question here in SO unless I really can't find a way to solve my problem myself. I did a lot of googling and was not able to find a solution for this one problem I am about to describe.
Here is the problem. I am creating a templated php website. With templated I mean something like below:
<?php include("header.php");?>
<div id="content">
<div id="main">
<h2><?php echo($page_title);?></h2>
<?php
echo ($page_content);
?>
</div>
<?php include("sidebar.php");?>
</div>
<?php include("footer.php");?>
As you can see here page template ECHOES the content of the $page_content variable between header and footer sections to build the page.
To keep the code clean and separated (in my own way) I have been placing the html content in .txt files (let's say page1_content.txt) and assigning the txt content to this variable ($page_content) as below:
$page_content = file_get_contents("page1_content.txt");
My problem starts when I place some php code in page1_content.txt, lets' call this file page2_content.php (yes, I change the file from .txt to .php). Then I assign the content of this file to $page_content variable as below as usual:
$page_content = file_get_contents("page2_content.php");
Now, when the page template ECHOES page2_content.php contents the php code in it is also echoed as string and not executed, but I am trying to query a database and do some stuff in this file with some php code. I mean, I want the php code inside page2_content.php to be executed and the cumulative html code to be echoed by the "echo" line inside the template file.
How can I achieve this?
Please ask me any questions if you need more info/clarification.
Thanks
EDİT:
As many people here suggested the solution was including the file. Actually, I tried including the file before but it didn't look like it was working, it broke my template, so I though I was on the wrong track and quit the "include" way of doing this. Since everybody here is advising to use include I tried that again. I replaced the php code in "page2_content.php" with a basic 1-line code just to see if it gets executed before adding generated html code without breaking the template and it worked. Apparently my php code had a problem at first place and hence broke my template execution.
Now I have changed the template structure slightly and pages using the template, and it seems to work nicely. Thanks a lot everybody. I have up-voted every answer suggesting that I use include :)
As #Ali suggested, you could include the files. The other option which I highly suggest you do not use is the eval() function.
I think what you want to do is to include your content PHP file, not echo it (as you are doing with header.php and footer.php).
echo($page_content);
Would become as below:
include("page2_content.php");
You've already done this in your footer and sidebar, just use include()

Store PHP code in a text file and retrieve it using file_get_contents?

Is there any way to store PHP code in a text file (for a navbar that may change) and access this text file via file_get_contents and add it into a div in a PHP/HTML document?
What you have to do is
<div class="navbar">
<?php include("nav.php"); ?>
</div>
You can use the built in include() function in PHP.
See docs here: http://www.php.net/manual/en/function.include.php
Using this means you essentially read in the contents of the file, and insert it directly where you are making the call. In your case it might be something like:
<div id="menu">
<?php include('partials/menu.php'); ?>
</div>
Whatever you have in the menu.php file (located in partials folder) will be inserted inside the menu div.
As a side note, since it's basically injecting whatever is in the file, you can also use any variables that you set in the menu.php file if need be
Use one of these
www.php.net/include
www.php.net/require
www.php.net/file_get_contents and www.php.net/eval

How do I call onto a separate header.php?

To be honest I do not understand PHP just yet. But what I am trying to achieve sounds easy in my head.
Here is the code at the top of my wordpress single.php file:
<?php get_header(); ?>
I have created a separate header i.e header2.php.
I want to know what do I need to write at the top of my single.php template to call header2.php instead of header.php?
If you want to have header-2.php (and not header2.php) you have to call:
<?php get_header('2'); ?>
See this page: http://codex.wordpress.org/Function_Reference/get_header
Just create a header file like
header-myHeader2.php
and call it like
get_header('myHeader2');
Syntax is header-name.php and you can call using the name part.
Read more here.

Can PHP be inserted into a javascript call in my header?

I have a wordpress theme that I like to duplicate. To make things easier on myself, I'm trying to use the bloginfo function to call my jquery file..like this:
<script type="text/javascript" src="<?php echo bloginfo("template_url"); ?>/js/jquery-1.4.2.min.js"></script>
I just can't get it to work. When I check my source file it looks exactly like the code above.
Can this even be done, or should I just forget it? Thanks guys!
Are you sure the above code is actually in a PHP-file and gets parsed by the server? I can't think of a different reason why PHP-code should just be printed and not executed.
See the Referencing Files From a Template chapter in http://codex.wordpress.org/Theme_Development
Whats the name of this file? It should end in .php.
What does the source look like when you view it from the browser?
Are there other places in the file where you use and is that data correct?
Make sure there are no blank lines at the end of the source file. PHP doesn't like blanks lines at the end of the code.
Two problems: No need for the echo in the WP template tag; should be
<?php bloginfo("template_url"); ?>
Template Tags/info « WordPress Codex
And, why are you trying to include jQuery from the template directory, when it resides in wp-includes/js? You should be using Function Reference/wp enqueue script, if your theme doesn't already include jQuery.
You should try to edit your theme's header.php file directly because custom fields of your theme may not be executed by PHP interpreter.
As I've never used PHP, this is only a guess...
PHP is a server side language, and you're tyring to use it on the client side?

Categories