For a Wordpress plugin, I made a function that contains a big HTML portion (following WP docs, I used ob_start and ob_get_clean to insert it):
function myShortcode() {
ob_start();?>
<!-- here a lot of HTML -->
<?php
return ob_get_clean();
}
I would like to put the HTML outside this function and include or require it.
Is this possible? Is there something I should be aware of? Is it preferable file_get_contents? Any other tip is appreciated, thanks in advance
You can move your html to separate file and then include it as simple php file after ob_start()... Yes it will work, just make sure your view.php template partial is Echoing that html, i.e. You may have html outside of php tags e.g.
File view.php
<?php //Template code starts ?>
ALL HTML HERE
<?php // Template code ends ?>
And your current function in current plugin php file will become:
function myShortcode() {
ob_start();
include(PLUGIN_DIR_PATH/templates/view.php);
return ob_get_clean();
}
Related
I am writing an anti-ddos php script, and I want to write a custom load page, but the html page of the orginal html page would be rendered. Is there anyway for me to prevent the html showing up while showing up my php load page
<?php require "antiddos.php"?>
#Samuel Wang, you can try check that out by one of this examples.
below assume code of antiddos.php and other file from where you include it.
1. antiddos.php :
E.g. before your php code or you can keep html inside the file that won't be an issue.
<?php
echo"<p>This is a test html content of antiddos.php</p>";
function testcall($TextReceiver ){
return $TextReceiver;
}
function MathMultiplication($Prt1,$Prt2){
return $Prt1*$Prt2;
}
?>
2. testprocess.php :
You can use the ob_clean(); function next to your include file statement that will discards the contents / html output from the antiddos.php.
<?php require_once("antiddos.php");ob_clean();
echo testcall("Hello this is print call from testprocess.php to antiddos.php");
echo "<br>";
echo "MathMultiplication= ". MathMultiplication(4,3);
?>
Conclusion by both the files output: First from the antiddos.php and second one from the testprocess.php
I included an html file like this, so that it is not displayed when the site loads:
<div id="menugrp0" class="menuhide">
<?php include 'menugrp0.html'; ?>
</div>
Now I want it to be shown at a specific spot. I am using this php code, to get some variables which are transported with the $_SESSION. I am using this kind of question for some simple html links, in which case it works perfectly:
if ($_SESSION['gruppe'] == $h['gruppe']) {
printf(' menugrp0.html');
}
I know that this is not working at all at the moment for this included html. I also tried to add the <?php [...] ?> tag inside the printf, which is also not working.
Is it possible to show a hidden included html file with a printf tag?
Try this one.
<?php
if($_SESSION['gruppe'] == $h['gruppe']){
echo 'Foo';
include ('/path/to/menugrp0.html');
echo 'Example: one';
}
?>
readfile('menugrp0.html'); // Reads a file and writes it to the output buffer. It is like read then "echo"
How to echo the whole content of an .html file in php?
Thanks to Daan Meijer, this works.
echo file_get_contents('...');
}
I have two files index.php and template.html. In the template file I have a div, which contains some PHP code inside. What I am trying to achieve is to pull the div from template including everything inside and insert it to my main index page. I managed to do so, but only if there is no PHP code inside the div. If however there is any PHP included I see something like this "saveHTML($snippet[1]) ?>;" instead of full PHP code block. Could you please explain the reason why I am not able to move the div including PHP codes.
index.php file
<?php
//some basic stuff such as new DOMDocument(); loadHTMLFile and so on
$post = $posts->query("//div[contains(#class, 'post')]");
?>
<body>
<?php echo $templates->saveHTML($post[0]);?>
</body>
template.html file
<div class="post">
<?php echo $examples->saveHTML($snippet[1]) ?>;
</div>
you can do it in a simpler manner
first in template.html please replace your dynamic content with %%posts%%
i.e,
template.html
<div class="post">
%%posts%%
</div>
then in your index.php get contents of template using file_get_contents
after that replace it with your dynamic code like as below
$htmlFile = 'template.html';
$yourDynamicContents = 'Replace your dynamice msg here';
$contents = file_get_contents($htmlFile);
$contents = str_replace('%%posts%%', $yourDynamicContents, $contents);
$contents has whole page...you can either echo or send mail with that template or even pass to print pdf etc
the above will do it simply and clean.
P.S you can replace anything in $yourDynamicContents whether its css,html,js
Hope the above answer helps
Thank you
Have you tried getting the content of your template file via file_get_contents? Then maybe you can extract your div with substr() using strpos()
I need to generate PDF files from HTML templates and plan on using wkhtmltopdf to do that. Inside the HTML templates, I need to be able to use PHP logic to adjust what the template will render. Take this HTML template for example:
<p>Dear <?php echo $firstname; ?>,</p>
<p>Thanks for signing up. You've invited these people along with you:</p>
<ul>
<?php foreach ($invitees as $invitee): ?>
<li><?php echo $invitee; ?></li>
<?php endforeach; ?>
</ul>
<p>Regards,</p>
<p>Chris White</p>
I have no problem being able to pass a HTML template file to wkhtmltopdf but I don't know how to get the PHP logic inside it to run correctly and be able to return the resulting template. I came across this blog post while Googling but the author uses Smarty as a template language: https://davejamesmiller.com/blog/php-html-pdf-conversion-using-wkhtmltopdf
Using Smarty would solve my problem but I don't want to bring in a library to do this when I can just use plain old PHP. Basically, I need a way to pass in variables to the HTML template (in this case $firstname and $invitees), have it execute the PHP code inside the template and then return the resulting template after the PHP has been executed.
Any ideas?
Just save your file as php (for example template.php) and implement there the logic you need.
I did the same also with wkhtmltopdf and it worked great.
I also passed some variables over GET to the template to really get the correct report as pdf.
To save the file I used the PHP session id and saved the file to a folder with write permissions for www-data (Linux) and started the download automatically via Javascript.
I had the same need and I did it like that. Don t really know if my code will help you but why not.
First I used composer to get https://github.com/mikehaertl/phpwkhtmltopdf.
lets say you have a php file " content.php "
<?php
echo "<html>";
echo "<h1>test</h1>";
echo "<html>";
?>
your index.php will be :
<?php
require "vendor/autoload.php";
ob_start();
require('content.php'); //The php file
$content = ob_get_clean();
$pdf = new \mikehaertl\wkhtmlto\Pdf($content);
if (!$pdf->send()) {
throw new Exception('OMG WHY : '.$pdf->getError());
}
If you're not using any template engine, can't u just call your template.php file with some params ?
Something like
$wkhtml2pdf->html2pdf('template.php?firstname=Foo');
(I have no idea how wkhtml2pdf works, this code is just for you to understand the logic)
and in your template.php file :
<p>Dear <?php echo $_GET['firstname']; ?>,</p>
I am relatively new to php and wordpress and I would like to know how I can render a php file without the include statement.
For example if I have two files plugin.php and component.php
plugin.php
<?php
add-shortcode('myshortcode', 'myshortcode-func');
function myshortcode-func()
// magic function that loads
$result = LOAD('component.php');
return $result;
}
?>
component.php
<div>
<img scr="<?php getimage() ?>" />
</div>
NB
I don't want to use include because I think it screws the rendering and insert the page in the flow when called.
Thanks for you help !
You can use an output buffer:
function myFunc(){
ob_start();
include('component.php');
return ob_get_clean();
}
How to:
$php = file_get_contents("component.php");
eval($php);
eval is very dangerous though and shouldn't be used in production.
If this is for production, I'd recommend using hooks/filters (see wordpress source code). This lets you execute blocks of code on the fly, but is more constrained.