How to adjust HTML when website is in PHP - php

I have a Wordpress theme for a new website. I want to make some HTML adjustments but I realized that the website has only PHP scripts and of course the CSS file. For instance, I need to adjust the website name but cannot find H1 tags. I do not have any PHP knowledge. Please advice. Many thanks!
Screenshot

<?php echo "<div>html here</div>"; ?>
You can as many php chunks as you want
example :
<?php echo "<h1>html here</h2>"; ?>
<?php echo "<span>html here</span>"; ?>

Wordpress is a bit different from vanilla PHP instead of echoing out HTML with the title:
<?php echo "<h1>The Title</h1>"; ?>
It uses a function/hook called wp_title(); that you can find in header.php.
But to modify it you would have to in to functions.php and edit the function and for that you need some basic PHP knowledge.
Here is some documentation on the wp_title() function/hook.
https://developer.wordpress.org/reference/hooks/wp_title/

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>

Generating PDFs from template files using wkhtmltopdf

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>

php excluding piece of html code?

I have a custom CMS here entitled phpVMS, and I want to exclude a piece of code, a banner for a single page. phpVMS is steered using templates, for instance, the main template that codes the general layout for all pages is entitled layout.tpl. So, like I said, this displays whatever is in the template, on all of the pages. I have however created a special control panel, and therefore require to exclude the banner, because it slightly destroys the theme of it. Is there any PHP code that excludes a piece of code on a single site? I need to remove a single div...
<div id="slideshow"></div>
...on a single page.
Basically, I could create a new template but this is a very long winded and unefficient way within this CMS, and the final result isn't that great - because I can't reinclude the mainbox div which is the box defining the content on the centre white bit of the theme - it's already in the layout.tpl.
I hope you can somehow help me, hope I've included enough information there.
Thanks.
I don't think you can do what you're asking in PHP, but you might be able to do this on the client-side, by either hiding the div (CSS display:none) or by removing it with JavaScript. You might be able to do something like:
<?php
include("layout.tpi");
if (condition)
{
// Javascript:
echo "<script>document.getElementById('slideshow').style.display = 'none';</script>";
// OR jQuery:
echo "<script>$('#slideshow').hide();</script>";
}
?>
If you use a variable to determine you don't want to include the div, you could do this:
<?php if ($include) { ?>
<div id="slideshow"></div>
<?php } ?>
OR
<?php
if (!$include)
echo "<!--";
?>
<div id="slideshow"></div>
<?php
if (!$include)
echo "-->";
?>
EDIT: Obviously, there is no good reason to use the second method. The second method will only comment out the HTML so it will still show up in the source.
I'm not sure if this is what you are looking for, but seems simple
<?
$template = true;
if($template) {
?>
<div id="slideshow"></div>
<?
}
?>
On the template, you could have some code that reads:
if($_SERVER['PHP_SELF'] == /*control panel file*/) {
//exclude
}else{
//include
}

Dynamic footer integration

I'ld like to insert the footer of my website dynamically so I can add it to multiply pages without copy and paste it every time.
I know an iframe can do it but I heard that's not a good solution and not working for seo.
Can it be done with PHP or JavaScript?
footer.php code as follows
<?php
//Write your custom footer here
?>
home.php code as follows
<?php
//All contents of Home page
include('footer.php');
?>
<?php include('path/to/footer.php'); ?>
PHP is the right tool for this job:
<?php include('footer.php'); ?>
from php use include function
e.g: <?php include('inc/footer.php');?>

Dynamic header with PHP?

I know this is a basic PHP question, and I'm trying to learn the stuff. I very familiar with HTML, CSS and familiar with the CONCEPT of PHP, but not with specifics.
I have always partnered with a back end developer to accomplish this stuff and to set up wordpress sites, etc.
I'm building a very basic four or five page website (a showcase for the client's custom fishing rods: http://www.tuscaroratackle.com/index2.php). I want to call the page header (as in logo, navigation, etc., not as in the head element) dynamically from a php file, and same thing with the footer, so I don't have to rewrite all the markup on every page for these bits.
I don't intend to use a database for this site, I was just thinking I could call those two bits from another file, as you would in a wordpress setup with the header.php in the wp-content directory.
Is there an easy explanation on how to do this? (I get the basics, just looking for help on the more specific PHP calls that need to be made)
Or, if this is not an answer somebody could easy give, can you point me to a good resource to research it further?
Thx
You betcha - include and require -
using include
in your page:
<body>
<?php include 'header.php'; ?>
in your header.php
<div id="header">
<!-- content -->
<?php echo "run php stuff too"; ?>
</div>
would result in:
<body>
<div id="header">
<!-- content -->
run php stuff too
</div>
You should put the header html code in some file such as header.php and then include it with php like:
include ('header.php');
You should specify the correct path there, for example, if you put the header.php file in includes folder, you can include it like:
include ('inclues/header.php');
More Info:
http://php.net/include
Put in a separate file and use include, require or require_once.
Eg
require_once("path/to/myfile.php");
Look into PHP includes.
The way I normally do it is to create a file called includes.php with two functions header() and footer(), then call them on each page like such:
includes.php:
<?php
function header(){
echo '<div id="header">Welcome</div>';
}
function footer(){
echo '<div id="footer">Goodbye</div>';
}
?>
index.php:
<?php
include_once('includes.php');
header();
echo '<div id="content">Main page body</div>';
footer();
?>

Categories