Facebook  Twitter 

SMFHacks.com

+- +-

SMFHacks.com

+- User Information

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

+- Forum Stats

Members
Total Members: 12240
Latest: ptrcmres01
New This Month: 9
New This Week: 5
New Today: 1
Stats
Total Posts: 39383
Total Topics: 6933
Most Online Today: 41
Most Online Ever: 2482
(April 09, 2011, 07:02:45 pm)
Users Online
Members: 0
Guests: 38
Total: 38

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

0 Members and 1 Guest are viewing this topic.

Offline SMFHacks

  • Administrator
  • Hero Member
  • *****
  • Posts: 14715
    • 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
7 Replies
5399 Views
Last post October 07, 2006, 11:43:40 pm
by Nite
2 Replies
3026 Views
Last post October 14, 2007, 06:40:01 pm
by momoney163
0 Replies
1309 Views
Last post May 31, 2017, 10:49:05 pm
by SMFHacks
0 Replies
183 Views
Last post July 13, 2018, 04:23:01 pm
by SMFHacks
0 Replies
383 Views
Last post July 13, 2018, 04:24:23 pm
by SMFHacks

+- Recent Topics

condition de livraison by SMFHacks
December 14, 2018, 12:47:53 pm

Multiple Versions in SMF by Boomslanger
December 13, 2018, 06:13:31 pm

GDPR Helper free doubts by Rock Lee
December 08, 2018, 06:20:57 pm

Downloads Pro 5.0 Released by SMFHacks
December 05, 2018, 03:11:32 pm

SMF Hacks Black Friday Sale! by SMFHacks
November 30, 2018, 09:03:04 am

Rewrite rule for IPN by diegolyanky
November 26, 2018, 10:58:55 am

EDITOR PRO by SMFHacks
November 25, 2018, 03:12:19 pm

SMF Gallery Pro 8.0 / PHP 7.0 onwards by SMFHacks
November 25, 2018, 12:33:10 pm

5.0 beta log errors by tfs
November 24, 2018, 10:37:06 pm

Download button doesn't initiate a download the first time by SMFHacks
November 24, 2018, 09:08:55 pm

Powered by EzPortal