Support » Fixing WordPress » include files into header.php?

  • Resolved Marcc1


    I want to include a ads.php file into my header.php.

    ads.php determines which page is actually shown and uses a variable $adcode.

    header.php & sidebar.php are included in index.php

    in sidebar.php $adcode is echoed.

    but this does not work!? Any hints or other solutions?


Viewing 14 replies - 1 through 14 (of 14 total)
  • drmanry


    As long as ads.php is in the same directory as index.php, this should work:

    <?php include("ads.php"); ?>



    Thanks for this amazing hint. Sorry but i know how to include files =)

    I guess it’s not possible to use a variable through so many file!?

    – if is_page … $ads = ‘xxx’

    – echo $ads

    – include(‘ads.php’)

    – include(‘header.php’)
    – include(‘sidebar.php’)

    i get no output…

    Maybe you need to use PHP global variables.

    You can’t pass variables across php files like that…

    If you want to pass information along to another PHP file, you’ll need to include something in the query string that requests the file, use a session, or use globals, or any other way that does the job…

    Using a query string would be the most sensible option in my opinion…

    If you have no idea what i’m talking about, it’s time to get googling and reading up…

    If the files are required on the page, use require, or require_once … or if you need an include and you only need to include the file once in the page, then use include_once ….

    Quick example regarding the query string… (this should make sense although only an example).

    if(isset($something)) {

    Then in your file, you check the request to the file. Using the example above, you’d be checking the GET request..

    if(isset($_GET['example'])) {
     $somevar = $_GET['example'];
      do some code, referencing $somevar, etc etc.....

    This will not fully suit what you posted, but assuming you can understand what i’ve posted above i’m sure you’ll take something away from it…

    Regarding globals, i’d say avoid using them unless you absolutely need to. Not the same as REGISTER GLOBALS (incase anyone was reading thinking i had the wrong end of the stick), which is another kettle of fish, but i think using a global for the task you have in mind, is … perhaps overkill… and unrequired….



    I have several ad places in different php files, so my idea was to determine the actual page once and then set all adcodes.

    thanks @t31os_, I will give it a try.



    using a query string is not the perfect solution I dreamed of..
    with a query string I have to do the same query more than once..

    I want to determine which one is the actual page (just once) and then place the right ads..

    If you change the query string dynamically you avoid having to do it multiple times…

    My example shows a basic example of doing so…

    You’d use…


    In two places, once in the sidebar, once in the header…

    In your header check if you’re on a page, if you are grab some info about the page, be it the title, the ID, whatever you like, pass this along to the include as part of the include query string…

    Then in your ads file, check if a query string exists, if it does, then display your related set of ads, if not, include some default ones… The ads change depending on the query string, which itself will change depending on the value of the info passed into the string.

    Of course if you want to pass the variables around (to make it easier) then refer to globals posted earlier…



    Not exactly..

    My idea was do include the ads file once determine the page and save 3 variables (e.g. $adsleft, $adsright, $adsbottom)


    if page id = 1
    $adsleft = adsleft_code
    $adsright = adsright_code
    $adsbottom = adsright_code
    if page id = 2
    $adsleft = adsleft_code
    $adsright = adsright_code
    $adsbottom = adsright_code
    if page id = 3
    $adsleft = adsleft_code
    $adsright = adsright_code
    $adsbottom = adsright_code

    In your case with a query string it would do the code above about 3 or more times and maybe with some more loops to determine the ads location.

    I use ads on every single page so maybe its the best solution to use global variables even if its not as nice as other solutions..!?

    I think you’re misunderstanding what i’m saying, because what i’ve posted does not loop anything… you get one result… not multiple results…

    However this depends how you code your ads file…

    You seem to be over-complicated what should be relatively simple…

    Do your page detection in the file you include the ads.php in…

    If no value is passed to the include then have the ads file display a default set of information/code…

    Get rid of the > if page id < stuff in the ads file… and replace that with code to check the request to the file… as per my previous example…

    Do you want a more detailed example?



    I am going to do some test stuff and come back to you soon 🙂

    but you can also give me a more detailed example!



    Once more…

    I got those files:

    – includes header.php
    – includes sidebar.php
    – includes footer.php

    I use ads in the following files:
    index.php (1 ad)
    sidebar.php (2 ads)
    footer.php (1 ad)

    Your idea is now to include in these files the ads.php like:

    include(‘ads.php?page=category 1&adlocation=footerAds’);


    in ads.php I get “category 1” as actual page and the ad place “footerAds” with $_GET[‘page’].
    which looks like

    if(isset($page)) {
    $page = $_GET[‘page’];
    $adplace = $_GET[‘location’];

    if($page == “My Category #1”)
    if($adplace == “footerAds”)
    $footerAds = footer_ads_code
    if($adplace == “indexAds”)
    $indexAds = index_ads_code

    and this for every single page, ad and adplace!?



    tell me you icq/msn/skype and i will contact you… think its much faster…

    Essentially, yes ….

    but grab the page or category before the include…, then pass that along as part of the include path / query string…

    It really depends on exactly what you want to achieve…

    You could do…


    Before the includes, check if it’s a page, category, post, whatever then assign that to $somevarcheck …

    You only have 3 cases to match in your ads file.. (of course rename my examples where appropriate)…

    You can avoid huge amounts of IF/ELSE’ing by using switch and case…

    It’s for you decide which approach suits the task though. So you can proberly ignore me now, and just do what works best for you… 😉



Viewing 14 replies - 1 through 14 (of 14 total)
  • The topic ‘include files into header.php?’ is closed to new replies.