PHP Include for <head>

Hello :slight_smile: I wanted to know if I could use a php include for the head of my site. I want to keep it clean and not have unnecessary junk in pages that don’t need it (for example if I don’t need to have jquery in that particular page).

And I want to be able to change all the pages at once. I’m doing it with the sidebar/ footer right now.

Let me know if I should explain more… thank you for your time. :slight_smile:

Simply create a *.php file that contains all the nessecary script and resource management then include the file are part of the <head>, see the below example:

<!DOCTYPE html>
<html lang="en">
<head>
    <title></title>
    <meta charset="utf-8">
    <?php include('/path/to/file/resources.php'); ?>
</head>
<body>
    
    <!-- Do something here -->
    
</body>
</html>

Bless you :slight_smile: I wish I could give you a hug. This place has been so amazing and helpful. I wish I’d found it years ago when I started learning. Have a good day , I’ll
do this now.

You could create the title as well from the page name from the URL or a variable in the page above the header include.

Your very welcome, the example i posted above could be considered as a template action since your including dependencies only required by the current page.

Thank you for that :slight_smile:

Okay :slight_smile:

@bzcastell

I use a further improvement to @chris.upjohn’s example:



<!DOCTYPE html>
<html lang="en">
<head>
    <title></title>
    <meta charset="utf-8">
    <?php if( isset( $jquery_wanted ) && $jquery_wanted ) { include('/path/to/file/resources.php'); } ?>
</head>
<body>
    
    <!-- Do something here -->
    
</body>
</html>

The additional overhead is minimal because the $jquery_wanted test is server side.

edit: added missing bracket

Oh! So I can also include whichever scripts are specific to that page? Now my brain needs a second to catch up. I love this.

On my site. I have a file called header.php, which has the doctype to the opening container, like:


<!DOCTYPE html >
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<link rel="shortcut icon" href="/images/favicon.ico" />
<title><?php echo $title; ?></title>

<style type="text/css">
<!--
@import url("/incs/home.css");
<?php echo $addCSS; ?>
-->
</style>
</head>
<body class="yui-skin-sam">
  <div class="container" id="bodyTxt">
  <?php include_once($_SERVER['DOCUMENT_ROOT']."/incs/backlinks.php");
    include_once($_SERVER['DOCUMENT_ROOT']."/incs/navbar.php"); ?>
    <div id="content">

Then when I want a new page, I start a new file and it looks like:


<?
  $title = "xxx"; // this is for <title>
  $page_title = "xxx"; //this is for breadcrumbs if I want a custom title other than the default
  $addCSS = ""; //custom CSS for this page only
  include_once("/path/to/header.php");
?>
 //start typing html/body text here
<?php include_once("/path/to/footer.php");?>

Thank you RyGuy, It seems as though there are a few ways to do this… I’m in love with PHP. It’s so organized. eventually my pages will just be one line, haha!

You are welcome. I was getting tired of opening a file, copying, going back and making a new file, pasting, removing the content, typing in the new content, save, tweak. Now here’s my title, grab the header, type body, grab the footer. On my to do is make a flag, so if wide is true, don’t grab the menu (within header) and set the content holder to the width of my container. It would probably take 30 minutes to do, so some day.

I started off like you Ryan but now have a switch in the header which also contains the navigation information. Add a new page save the info into the switch statement and you are done - new button created automaticaly but only realy works for smaller sites.
I also have an include for the footer so my page is include header - content - include footer.

It would be worth it to do now, before you add stuff on top of it. That’s what I’m doing and I have to keep going back and fourth. I want to nip it in the bud, though so I don’t have to when my website has more pages. I just want everything to be perfect and organized. I don’t know what a “flag” is, but i’m going to, lol.