Facebook  Twitter 

SMFHacks.com

+-

SMFHacks.com

+- User Information

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

+- Forum Stats

Members
Total Members: 4253
Latest: Ineedsmfhelp
New This Month: 1
New This Week: 0
New Today: 0
Stats
Total Posts: 43242
Total Topics: 7516
Most Online Today: 126
Most Online Ever: 2482
(April 09, 2011, 07:02:45 pm)
Users Online
Members: 0
Guests: 97
Total: 97

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

0 Members and 1 Guest are viewing this topic.

Offline SMFHacks

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

+- Recent Topics

is it possible to add support for odysee.com by SMFHacks
March 13, 2024, 10:53:28 pm

Request for admin notification by davejo
March 10, 2024, 01:31:59 am

I need help with torrent upload by Ineedsmfhelp
March 09, 2024, 10:01:13 pm

an idea for new mod (( content type with different display )) by SMFHacks
February 27, 2024, 01:36:27 pm

[Mod] RSS Feed Poster by SMFHacks
February 27, 2024, 11:57:18 am

find duplicate pictures by fvlog19
February 14, 2024, 02:22:40 pm

Error uploading video. by SMFHacks
February 08, 2024, 02:04:16 pm

Gallery icon as last added image by fvlog19
February 01, 2024, 01:04:56 pm

User Gallery Feature: move / bulk move images by SMFHacks
January 30, 2024, 05:48:25 pm

In the future it may be for smf 2.1.x? by smithloo
January 30, 2024, 12:55:34 am

Powered by EzPortal