including php file in thickbox inline view in wordpress - php

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

Related

Where's the HTML in PHP template files?

So I am new to web development, and want to begin developing themes for Wordpress.
I am confident in my HTML and CSS skills but I am somewhat stuck on understanding how PHP works specifically for Wordpress.
To get straight to the point, when I download a basic theme from wordpress.org and look inside all of the template files, I don't see any HTML code.
I am familiar with the get function in php and so on, but watching videos/tutorials on theme development has confused me so much.
For example, most of all the tutorials I have watched shows someone copy and pasting HTML code from their static web templates directly into the PHP files. (index.php and so on). It works and I am told that is a correct method of doing it, but I just don't understand why I don't can't see HTML code in wordpress themes I download.
Is there a way of not showing the HTML?
Thanks you in advance...
The html code is inside the PHP file. there are various way to write HTML inside a PHP file. For example
<?php
echo "<html><h1>header</h1></html>";
?>
Save the above code as PHP and run it on the server You will get html output from PHP file.
You can also run it in the following way
<?php
//your first php code here//
?>
<html>
my html here
</htm>
<?php
//your second php code here//
?>
It is the right way to write html code inside a php file. you can't run php code on a html file so the html code should be written on the PHP file.
========question answer==========
this is the theme you have mentioned : https://github.com/WordPress/twentyseventeen
check the index.php file
https://github.com/WordPress/twentyseventeen/blob/master/index.php
you will see
get_header(); ?>
<div class="wrap">
<?php if ( is_home() && ! is_front_page() ) : ?>
<header class="page-header">
<h1 class="page-title"><?php single_post_title(); ?></h1>
</header>
this type of coding there.
<h1 class="page-title"><?php single_post_title(); ?></h1>
Look carefully the line. <h1 class="page-title"> it is a html tag ( it is html code )
You can download the theme on your pc and open the index.php file and others file. you will see html code but it is mixed with PHP.
I would recommend you install WAMP on your localhost.
There are PHP files for Wordpress that alter how it functions but I would not recommend you edit these unless you know what you are doing.
If you install Wordpress on WAMP you will have access to all this and can also set up specific projects on WAMP to develop and test your themes.
There is a good walk through here on setting it up https://premium.wpmudev.org/blog/how-to-set-up-wordpress-locally-for-pcwindows-with-wamp/?utm_expid=3606929-106.UePdqd0XSL687behGg-9FA.0&utm_referrer=https%3A%2F%2Fwww.google.co.uk%2F
Basically what you'll do with Wordpress templates is use HTML to hold the info you get from PHP, such as the page/post title, content, tags, categories, etc. You could do something like
<h1 class="title-class"><?php get_the_title();?></h1>
No there is no way to hide html and there is no point to do that either.
But if you look carefully in wp-content -> themes folder you will see all themes directories.
there you can find html "code". In some case you won't find any .html files, may be.
Reason for it because wordpress theme's all pages contains atleast one dynamic part like header. so to make html page dynamic you need to set .php extension for files instead of .html or .htm.
You will find less html and more php sometimes because mostly wordpress themes coded with reusable snippets and functions who generates some code blocks.
But there is html code blended with php code inside .php files:
for example:
<html>
<head>
<title><?php get_the_title(); ?></title>
<?php get_custom_css_files(); ?>
</head>
<body class="container">
<div class="col-md-8 text-center">
<?php foreach($posts as $post) {
<div class="title"><?php echo post['title']; ?></div>
<div class="desc"><?php echo post['text']; ?></div>
<?php } ?>
</body>
</html>

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()

How to add code after "</article>" in content-page.php in Wordpress?

I am using the Shareaholic plugin and they give you a block of code to add the sharing buttons anywhere on your site.
[shareaholic app="share_buttons" id="7726121"]
[shareaholic app="recommendations" id="7962129"]
I want to add this code after </article> tag in the file "content-page.php" in Wordpress. When I do it then nothing happens. When I check the page source then I can't find the code anywhere. It happens even when I add some random word after </article> tag.
How can I make this work?
Thanks!
These are short code and you can't call them directly you need to do it like this.
echo do_shortcode('[shareaholic app="share_buttons" id="7726121"]');
echo do_shortcode('[shareaholic app="recommendations" id="7962129"]');

Is it possible to call PHP in html?

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

Custom PHP code won't show up in Wordpress theme?

This has me absolute stumped!
I have a small snippet of PHP code, <?php include('/includes/stuff.php'); ?>. All it does is output an image.
If I put this in the WP themes header.php or index.php files, it will never show up.
Here is the actual code in my the themes index.php file:
<img src="<?php bloginfo('stylesheet_directory');?>/images/content-top.gif" alt="content top" class="content-wrap" />
<?php include('/includes/stuff.php'); ?> TEST TEST TEST //this is the custom code
<div id="content">
But when it actually runs, it looks like:
<img src="http://www.bavarianblue.com/wp-content/themes/Polished/images/content-top.gif" alt="content top" class="content-wrap" />
<div id="content">
It just ignores my code entirely. No errors, no nothing.
The only time I could get it to work is if I included it in an already included file. For instance, in header.php there is an include for a features.php. If I included it there, it worked fine. But I don't want it there, I need it in index.php.
Is there some kind of configuration in wordpress where is needs to know all includes or something? I confirmed that my files are being modified (on the FTP).
EDIT
I just tried get_template_part(), as described on the wordpress codex, to no avail. Here is the snippet I tried:
<?php get_template_part( 'includes/stuff' ); ?>
Jared
Try the full path to stuff.php. Also, your host might be running php in safe mode, so check your file permissions.
Another way to include php functions in WordPress is via the functions.php file in your theme:
http://codex.wordpress.org/Theme_Development#Functions_File

Categories