Facebook  Twitter 

SMFHacks.com

+-

SMFHacks.com

+- User Information

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

+- Forum Stats

Members
Total Members: 4263
Latest: Cekky
New This Month: 0
New This Week: 0
New Today: 0
Stats
Total Posts: 43455
Total Topics: 7557
Most Online Today: 60
Most Online Ever: 2482
(April 09, 2011, 07:02:45 pm)
Users Online
Members: 1
Guests: 44
Total: 45

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

0 Members and 1 Guest are viewing this topic.

Offline SMFHacks

  • Administrator
  • Hero Member
  • *****
  • Posts: 16524
    • 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
8097 Views
Last post October 07, 2006, 11:43:40 pm
by Nite
2 Replies
4594 Views
Last post October 14, 2007, 06:40:01 pm
by momoney163
0 Replies
3503 Views
Last post May 31, 2017, 10:49:05 pm
by SMFHacks
0 Replies
4067 Views
Last post July 13, 2018, 04:23:01 pm
by SMFHacks
72 Replies
21477 Views
Last post March 25, 2023, 01:49:35 pm
by SMFHacks

+- Recent Topics

[Mod]Tenor Posting Animated Gifs by SMFHacks
August 30, 2024, 04:04:26 pm

List of other errors after installing and trying to use by SMFHacks
August 27, 2024, 02:15:52 pm

Subject Error by SMFHacks
August 27, 2024, 02:15:46 pm

Store Not Logging Transactions or Sending Emails for Paid Items, Free Items Work by SMFHacks
August 27, 2024, 05:26:52 am

Don't Know How to Unhide Previously Hidden Items in Latest SMF Store 5.0 by Michael Vail
August 26, 2024, 11:38:33 pm

Undefined array key "session_var" and Undefined array key "session_id" Errors by SMFHacks
August 26, 2024, 11:10:54 pm

SMF Links Error by yamiacaveman
August 22, 2024, 06:21:53 pm

HTTP Error 500 by SMFHacks
August 20, 2024, 12:28:53 pm

Site upgrade to 2.1 by live627
August 18, 2024, 02:47:33 am

Uninstall issue and new licence no email by landyvlad
August 16, 2024, 08:05:48 am

Powered by EzPortal