May 17, 2014

Where To Add New Code In A Php File

|
For instance, you read that in order to improve your page speed, you need to remove query string from URLs.

In order to do that you need to insert or paste a certain set of code inside the functions.php file before the closing php tag.

So where exactly is before the closing php tag ?

The closing php tag is:  ?> 

But the problem is there are functions.php come without the closing php tag at the end of the file.

According to what I have read from various forums, the functions.php doesn't need to have a closing tag at the end of a file.

That's why some of you cannot find this ?> at the end of the existing code in the file or template.

One reason why some omitted the closing tag is to avoid having white space or blank space and other characters at the end of file.

It is because any character which is accidentally added after the closing tag can cause your header to work.

1.  How To Paste New Code In Template With Closing Tag

If there is a closing tag, then you add your new code just BEFORE ?>.

Example:

<?php
    if (function_exists('register_sidebar'))
    register_sidebar();

PASTE YOUR NEW CODE HERE
?>

2. How To Paste New Code In Template Without Closing Tag

If there is NO closing tag, this is what you do:

You put in another opening php tag , paste the new code and add a closing php tag as shown below:

Example:

<?php
    if (function_exists('register_sidebar'))
    register_sidebar(); 

<?php

PASTE YOUR NEW CODE HERE


?>

Caution: Functions.php is a very sensitive file. If you paste any code wrongly in this file, you could be locked out of your own site (you cannot both log in or log out) and in addition, your site cannot be accessed

If this problem happens to you, then read this post on how to fix functions.php error.