The Benefit of Using PHP Include Function in Optimizing Your WordPress Blog Theme

I’m always a fan of PHP’s Include function, especially in tweaing WordPress theme.

<?php include (‘file-to-include.php’); ?>

Aside from core WordPress theme functions and common use of Include in PHP-based programming, I found some use of it in term of safe-tweaking your WordPress blog theme’s codes.

Here’s an example. Suppose you want to have your Google AdSense code embedded into the top of your WordPress blog’s index.php. Sure, you can add the code right away into index.php – However, I enjoy using Include function to embed the AdSense code. No WordPress plugin necessary…

Here’s how:

  • I create, for example, adsense-code-top.php file containing your Google AdSense code.
  • I then include the file into index.php by putting <?php include (‘adsense-code-top.php’); ?> in the place I desire, for example, below the header.

Here are some benefits of using your custom Include function in your WordPress blog.

  • If you happen to crash your WordPress blog due to coding errors, you can easily check what and where the errors are by looking into your custom PHP files, instead of your WordPress theme files.
  • You can edit your codes faster by looking at the right custom PHP files you create, instead of scanning hundreds lines of WordPress codes.
  • You increase your WordPress blog’s coding usability – If you have someone tweak your blog’s theme codes, you can make her life easier by having your add-on organized in custom files (of course, don’t use obscure file name for your custom files – e.g. adsense-code.php is way much better than custom169236.php.

If you have other tips, please share by commenting on this post :)

Share and bookmark:
  • RSS
  • Twitter
  • StumbleUpon
  • Technorati
  • Digg
  • del.icio.us
  • Facebook
  • Mixx
  • Reddit
  • Propeller
  • Google Bookmarks

Related posts:

  1. Using Tab Panel Div in WordPress
  2. What is SSI?
  3. WordPress Top Post Script
  4. Creates your adsense templates for wordpress
  5. Optimize your blog ads

21 comment(s)

  • Jun 5, 2009 by Bill from PHP code snippets and discussion

    To elaborate on this and make it even more streamlined, you could make the include file contain

    1) an array containing IDs-> AdSense javascript variables (channel, width, height), i.e.;

    $ads = array(
    ‘vertical’ => “google_ad_slot = \”7756841182\”;\ngoogle_ad_width = 160;\ngoogle_ad_height = 600;”,
    ‘banner’ => “google_ad_slot = \”5182807122\”;\ngoogle_ad_width = 468;\ngoogle_ad_height = 60;”
    );

    2) A simple function that accepts an ID and displays a full AdSense javascript code block with the respective javascript variables, i.e.;

    function displayAdsense ($id)
    {
    if(!empty($ads[$id]))
    {
    echo”

    “;
    }
    else
    {
    echo”[error: called ad isn't in array]“;
    }
    }

    Then you could include the one PHP file anywhere/everywhere and simply state to display one of the ads you have defined in the array (by referencing the appropriate ID).

    NOTE: You need to edit in your own values/Adsense codes in the above if you decide to use it. Also, no warranty etc etc legaleese.

  • Jun 5, 2009 by Bill from PHP code snippets and discussion

    It appears the blogging software redacted some of the code, specifically the echo part that displays the full dynamically generated AdSense javascript. I’m not all that familiar with blog commenting so I’m not sure how to fix/avoid this. But I can explain how to fix the code:

    Copy your AdSense javascript into the empty line in this statement;

    echo”

    “;

    Replace any quotes (“) in the javascript with a backslashed quote (\”). Then replace the javascript variables with simply $ads[$id] which will make PHP paste the content from the relevant array part that is retrieved during the function call to displayAdsense().

  • Jun 5, 2009 by Bill from PHP code snippets and discussion

    The blogging software also removed some other things, I see now. Why it strips out content rather than converting it for harmless display like vbulletin etc is beyond me. But I give up. Sorry for messing up your comment section, admin.

  • Jun 7, 2009 by admin

    Hi Bill, Not a problem - Thanks for sharing your codes. However, I'm not sure I understand what you meant by "stripping out content" - Did you mean excerpts, or other things? But overall I agree with you - Wordpress is powerful, but not as appealing to PHP Programmer as to Non-programmer user...

  • Jun 12, 2009 by rosarionemia from Joomla Components

    I just started my html and css coding this first quarter of the year and still struggling on some area of this codes – learning stage.
    I guess I also need to like PHP as its one way of database communication language and as I read also not so difficult to learn.

    thanks for sharing

  • Jun 25, 2009 by Mark from phone sleuth

    One other trick you may need to know is to use .htaccess to adjust your include path if you are including common stuff from deep paths under your doc root. Put the .htaccess file in the doc root so it affects the entire site.

    Example:
    php_value include_path /path/to/common/libs

  • Jun 26, 2009 by Jan from internet marketing

    I learn a lot from your post. I am still learning how this things works and your post is a big help. Thanks!

  • Jun 27, 2009 by Alloy Wheel Repair

    Looks like it doesn’t work for me. Where the problem can be ?

  • Jun 29, 2009 by admin

    Hi, I'm not sure - Please let me know how you did it...

  • Jun 29, 2009 by admin

    Cheers :)

  • Jun 29, 2009 by admin

    Mark, Yes - Unfortunately not all are adept in modifying .htaccess :) Good tips, by the way...

  • Jun 29, 2009 by admin

    Hi, PHP is excellent because many CMS, including Joomla! and Wordpress, and other scripts based on it.

  • Jul 1, 2009 by bethany from makeup artist in san antonio

    I’ve used the custom include function in WordPress, but I didn’t know you could check the errors in your PHP files instead of your WordPress theme files!

  • Jul 26, 2009 by Semi from Zeichnung Doodle Community

    @Alloy it works for me just fine. maybe describe your problem?

  • Sep 3, 2009 by vBulletin FAQ

    I use the php include alot actually. I own a number of vBulletin forums and the software is built on php. Good post.

  • Sep 14, 2009 by admin

    PHP include saves time in code editing - Having a number of forums like you do will emphasise the need for the includes... :)

  • Dec 15, 2009 by chakkravarthi from seo your blog

    Wow that makes my job a lot easier.. you can embed anything anywhere by using this method I think. you won’t ever need to install some plugins for simple tasks.

    Thank you for pointing out this.

  • Jan 10, 2010 by kazoo banner stands

    the include function is great – makes life so much easier when editing wordpress functions – i also use this function alot in web development – normally split my html into header, main, footer and include those elements within the code – its great – saves alot of time if a menu item needs adding

  • Jan 25, 2010 by Ashley

    Nice post.. I have learn more from your post..
    I need some more tips to improve my knowledge..
    Please some more tips..

  • Feb 28, 2010 by Martijn van Turnhout from Webdesign Roosendaal

    Nice post! What’s your opinion about Smarty though? I think it”d perfectly suit WordPress: more understandable code and easier to implement. I build websites using CMS Made Simple and WordPress and I often go back to Smarty instead of PHP.

  • Mar 25, 2010 by Codex from PHP Developer Blog

    This function will surely save a lot of time in the PHP Development and updating of files. For example, say you have a lot of PHP scripts in your server that uses the connection string to MySQL database. Without the include function, you will have to put the lines of the PHP connection string to MySQL database in every PHP scripts; this might seem OK but what if you will change your database password? This means you will have to edit each of those files and edit the password. This looks very tedious and inefficient.

    Instead, what I will do is use include function, for example I place my PHP connection string to MySQL separately in a file called “connect.php” , so in every PHP script instead of putting MySQL connection string, I use the include function pointing to connect.php:

    include ‘connect.php’;

    Then its done. If I update my MySQL password, I won’t have to open each of those files in the server, instead I will only edit connect.php and will automatically be used by all files with the include function in it.

  • Any feedback from you?

    This site uses KeywordLuv. Enter YourName@YourKeywords in the Name field to take advantage.

Jun 4, 2009 by admin

WEB HOSTING COUPON CODES:

HostGator 1-cent web hosting - Code: hgc25

HostGator 20% OFF initial order - Code: GREEN

GreenGeeks 10% OFF - Code: 10off

Want to set up your own web server at home? Read this guide

Super Green Hosting HostGator GreenGeeks WP Web Host green web hosting ad