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
Related posts:























21 comment(s)
Track this comments via RSS 2.0 feed. Feel free to post the comment, or trackback from your web site.
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.
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().
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.
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...
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
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
I learn a lot from your post. I am still learning how this things works and your post is a big help. Thanks!
Looks like it doesn’t work for me. Where the problem can be ?
Hi, I'm not sure - Please let me know how you did it...
Cheers :)
Mark, Yes - Unfortunately not all are adept in modifying .htaccess :) Good tips, by the way...
Hi, PHP is excellent because many CMS, including Joomla! and Wordpress, and other scripts based on it.
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!
@Alloy it works for me just fine. maybe describe your problem?
I use the php include alot actually. I own a number of vBulletin forums and the software is built on php. Good post.
PHP include saves time in code editing - Having a number of forums like you do will emphasise the need for the includes... :)
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.
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
Nice post.. I have learn more from your post..
I need some more tips to improve my knowledge..
Please some more tips..
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.
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?