Monday, January 18, 2010

include files using Exec PhP

If you are using Exec-PhP, it is because you have a WordPress blog, and you need to be able to run php code in your pages or posts.

If you try to put this in your file

include(ABSPATH.'yourfile.php');
functionname1();
some php command referring to function();
functionname2();
some php commands referring to function();

assuming that 'yourfile.php' is something like:

function functionname_1() {}
function functionname_2() {}

...you get error messages or something breaks down in different views.
You are probably tired of reading those mysqli error codes, too.

You can save yourself a lot of heartache by following the advice in the readme of the Exec-Php plugin.

They say
As a general rule I would advise to separate all definitions into a file and reference to it by calling require_once(). So the above example would be split into two parts, your article and a file.

Then, you new post or page looks like

require_once(ABSPATH.'yourfile.php');
functionname_fixed();

And, in 'yourfile.php', you have something like:


function functionname_fixed() {
stuff previously included in functionname1();
stuff previously included in functionname2();
echo stuff;
}

No comments:

Post a Comment