Facebook  Twitter 


+- +-


+- User Information

Welcome, Guest.
Please login or register.
Forgot your password?

+- Forum Stats

Total Members: 12399
Latest: SizofErsin
New This Month: 4
New This Week: 0
New Today: 0
Total Posts: 40596
Total Topics: 7131
Most Online Today: 104
Most Online Ever: 2482
(April 09, 2011, 07:02:45 pm)
Users Online
Members: 0
Guests: 82
Total: 82

Author Topic: prettyurls - Filters.wiki  (Read 1163 times)

0 Members and 1 Guest are viewing this topic.

Offline SMFHacks

  • Administrator
  • Hero Member
  • *****
  • Posts: 15284
    • View Profile
prettyurls - Filters.wiki
« on: July 13, 2018, 04:22:02 pm »
Pretty URLs is organized around filters, which are sets of instructions about what to do to with a page's URLs. If you want to change how Pretty URLs operates, or to add extra functionality, you will need to understand how a filter is constructed.

The filters array
The data for each filter Pretty URLs uses is all contained in a single array, which is serialized and stored in the pretty_filters database setting. You can edit this database setting yourself, however to make it easier to do so, a hidden admin tool was created which can be accessed at index.php?action=admin;area=pretty;sa=filters or admin/?area=pretty;sa=filters if the actions filter is enabled. This tool displays the array in the JSON format which should be easier to edit, however the parser is still rather strict.

So what does the array contain? Well the best way to explain is with an example filter. Here is a filter in the PHP array format which, for example, you might add with a package:

Code: [Select]
$prettyFilters = array( 'example' = array( // The filter must have an ID which is also the array's key 'description' => 'A new example filter', // This description will be displayed in the admin panel 'enabled' => 1, // Set enabled to 1 to enable and 0 to disable it 'filter' => array( // Filter callbacks are functions which are run to process each URL in a page 'priority' => 5, 'callback' => 'pretty_example_filter', // Name of the function to run ), 'rewrite' => array( // Most filters will add rewrite rules which will change a pretty URL into a less pretty, but more useable, internal URL 'priority' => 5, 'rule' => 'RewriteRule ^example\.html index.php?action=example [L]', // The RewriteRule to add to the .htaccess file, if there are multiple rules for a filter this can be an array of strings ), 'test_callback' = > 'pretty_example_test', // A function which returns an short array of links which will be rewritten by this filter 'title' => 'Example', // This title will be displayed in the admin panel ), );

Each filter may have filter or rewrite sections. If it does, then each must have unique priority numbers, though a mod may use the same number for both it's filter callback and rewrite rules. These priority numbers are sorted in ascending order. Custom filters should use numbers less than 20 if they want to catch URLs before any of the standard filters do, or numbers more than 80 if they want to catch URLs only if the other filters have ignored them.

Here is the above array in JSON format:

Code: [Select]
{ "example": { "description": "A new example filter", "enabled": 1, "filter": { "priority": 5, "callback": "pretty_example_filter" }, "rewrite": { "priority": 5, "rule": "RewriteRule ^example\.html index.php?action=example [L]" }, "test_callback": "pretty_example_test", "title": "Example" } }

Filter callbacks

Filter callback functions do the work of rewriting a page's URLs. The list of URLs is passed to the function which can then change them however it likes. Usually various regexs will be used to search for query string fragments to replace. Here is an example function which would produce the URL used in the above filter:

Code: [Select]
function pretty_example_filter($urls) { global $boardurl, $scripturl;

$pattern = '`' . $scripturl . '?action=example$`S';  // The pattern to search for, in this case action=example, use ` so that ~s in URLs will work.
$replacement = $boardurl . '/example.html';   // The replacement URL
foreach ($urls as $url_id => $url)
    if (!isset($url['replacement']))   // This is very important: unless you are very certain of what you're doing, do not rewrite URLs that have already been rewritten!
        if (preg_match($pattern, $url['url']))
            $urls[$url_id]['replacement'] = preg_replace($pattern, $replacement, $url['url']);
return $urls;

Test callbacks
Test callback functions return a short array of links which will be written by the filter.
Code: [Select]
function pretty_example_test() { global $scripturl, $txt;

return array('<a href="' . $scripturl . '?action=example">' . $txt['example_link'] . '</a>');

Get your Forum Ranked! at https://www.forumrankings.net - find out how your forum compares with others!

Like What I do? Support me at https://www.patreon.com/vbgamer45/


Related Topics

  Subject / Started by Replies Last post
2 Replies
Last post October 14, 2007, 06:40:01 pm
by momoney163
3 Replies
Last post September 26, 2014, 12:41:17 pm
by Jonas1975
0 Replies
Last post May 31, 2017, 10:49:05 pm
by SMFHacks
0 Replies
Last post July 13, 2018, 04:23:01 pm
by SMFHacks
23 Replies
Last post December 08, 2020, 12:58:49 pm
by SMFHacks

+- Recent Topics

Forum ToolBox by Monocero
Today at 01:55:20 am

[Mod] Tagging System by SMFHacks
January 13, 2021, 08:43:33 pm

Using Alternate PayPal Address by KKOG
January 13, 2021, 07:24:46 pm

Assigning a BuyNow Button to a product by SMFHacks
January 12, 2021, 04:59:32 pm

SMF Links by Nicole
January 10, 2021, 12:02:39 pm

Error on the latest version by davejo
January 06, 2021, 08:04:05 am

Recent topics on board index by eugen
January 01, 2021, 11:08:16 am

Parser by swayforth
December 31, 2020, 12:24:23 am

Show Gallery in Users Profile by swayforth
December 29, 2020, 10:37:17 pm

Hang after upgrade MariaDB to version 10.5 by SMFHacks
December 27, 2020, 12:56:07 pm

Powered by EzPortal