Using the Modifications Directory
By Paul
5/5 based on 1 vote. The median rating is 5.
By Paul
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
*/
}
If you want to make a template variable available for use, take advantage of how all $_GET and $_POST values are available by setting a fake one. Setting
You can also 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).
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.
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
*/
}
If you want to make a template variable available for use, take advantage of how all $_GET and $_POST values are available by setting a fake one. Setting
$_GET['something'] = 'blah';
makes {SOMETHING} show blah when used in a template (note: remember that if you don't set $_GET['something'] the template var will show literally as {SOMETHING} -- it won't be replaced with a blank value unless you specify one).You can also 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).
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.
Rating:
5/5 based on 1 vote. The median rating is 5.
Submitted: 01/02/07 (Edited 09/29/09)
Description: Including new PHP.
Views:
1449 views. Averaging 1 views per day.
In the most recent 30 day period, there've been 0 views.

Print
E-Mail