Latest Register Log In

+ Advanced Search

Using the Modifications Directory

Using the Modifications Directory
By
01/02/07 (Edited 03/10/14)

You can create a subdirectory of WSN named 'modifications', and when you do so all files placed in that directory will be included at the end of start.php -- or at the start of end.php if you use -end in the file name (like modifications/mymod-end.php). Running at the start has the advantage of letting you change values before they're used, while running at the end lets you manipulate $template->text (the page body) after it has been generated and processed.

Only PHP files can be included in PHP files, so be sure you don't put any other sort of file in the modifications directory.

Usually you don't want your code to run on every page. Conditionalize it like this to make it only run in a particular page:

if (currenturlcontains('suggest.php?action=addlink'))
{
/* this code only runs on the 'suggest link' page
note that url rewriting should be ignored, currenturlcontains() will search the non-rewritten version of the url
*/
}


You can use template functions to call any function you may add to a modifications directory file, or follow a special procedure to add template variables to the objects.

By default these files run at the end of prestart.php, so you can change any values you want to affect how the main body of the page will run. The tradeoff is that since the main body hasn't been processed yet you can't manipulate the final page text or the like here. If you want to do processing in end.php instead, after the main body of the page has generated, name the modifications file end-yourname.php. Any modifications directory file that starts with end- will be included into end.php (as of 5.0.51).

If you have class files which you'd like to have autoloaded on demand only when needed using the __autoload magic function, place them in a modifications/classes/ subdirectory and __autoload will look there for a file named the same as the class name.

Writing a Setup Script

Suppose you have a complicated modification involving database tables, fields and lots of files. You need a setup process to make it easy to install.

To do this, just test for a condition that'll only be true if the modification hasn't been set up yet. For example, a mymod_file1.php you could use:

if (!file_exists($settings->filepath().'modifications/mymod_file2.php'))
{// we know now that modifications/mymod_file2.php hasn't been created yet
// download/extract/write files to /modifications/ and /plugins/
// add database table
// add fields to existing tables
// append new language items to all languages
// add new templates to all template sets
// do any other setup tasks
}


This way the mod can be installed with just one file and no work, it'll automatically run the setup bit on first view.




Description Including new PHP.
Rating
Views 3686 views. Averaging 1 view per day.