Custom Pretty Permalinks

WordPress Plugin Staff List – Custom Pretty Permalinks

You can extend built-in permalinks and:
  • Use other page names than the ones included in the plugin.
  • Add pretty permalinks to subpages.
Extending pretty permalinks require advanced PHP skills and custom coding.
Our support is limited to detailed instructions you’ll find below. It doesn’t include creating or debugging your custom extension.

How to add custom pretty permalinks

You have two options when it comes to adding custom permalinks:
  1. Add the code to your theme.
  2. Create a custom plugin.
First method is not recommended, since custom code can be overwritten by theme updates. A better option is to create a simple WordPress plugin.

Custom permalinks plugin.

An example of permalinks plugin that you can use in your own installation.
Modify it as needed to fit your requirements.
<?php
/**
* Plugin Name: Staff List Custom Permalinks
* Description: Adds custom rewrite rules for Staff List single page.
* Author: abcFolio.
* Version: 0.0.1
*/
add_filter('rewrite_rules_array', 'abcfslcp_rewrite_rules');

function abcfslcp_rewrite_rules( $rules ) {
    $newRules = array(
        'professore/profilo/([^/]+)/?$' => 'index.php?pagename=professore/profilo&staff-name=$matches[1]',
        'attorney/([^/]+)/?$' => 'index.php?pagename=attorney&staff-name=$matches[1]'
    );
    return $newRules + $rules;
}

function abcfslcp_is_single_pretty( $sPageUrl ){
    if( substr($sPageUrl, -8) == 'attorney' ) { return true; }
    return false;
}

function abcfslcp_custom_page_names(){
    $ppCustomPages = array( 'attorney' );
    return $ppCustomPages;
}

Plugin Parts

Filter rewrite_rules_array

add_filter('rewrite_rules_array', 'abcfslcp_rewrite_rules');
Filter section is used to refresh WordPress rewrite rules (to add your own rules).

Function abcfslcp_rewrite_rules

function abcfcpp_rewrite_rules( $rules ) {
    $newRules = array(
        'professore/profilo/([^/]+)/?$' => 'index.php?pagename=professore/profilo&staff-name=$matches[1]',
        'attorney/([^/]+)/?$' => 'index.php?pagename=attorney&staff-name=$matches[1]'
    );
    return $newRules + $rules;
}
Contains two rewrite rules:
  1. Subpage rule: professore/profilo – page profilo can be a subpage of professore.
  2. Page name rule: attorney – pretty permalink page can be named: attorney.
1. Subpage rule.
professore/profilo/([^/]+)/?$' => 'index.php?pagename=professore/profilo&staff-name=$matches[1]
Your profilo page can be a subpage of professore page.
Example: http://mydomain.com/professore/profilo/john-smith
2. Page name rule.
attorney/([^/]+)/?$' => 'index.php?pagename=attorney&staff-name=$matches[1]
Adds custom page name. Now you could use attorney as a page name for pretty permalinks.
Example: http://mydomain.com/attorney/john-smith
Rewrite section is not limited to a single rule. You can add as many subpage and page rules as required.

Function abcfcpp_is_single_pretty

function abcfslcp_is_single_pretty( $sPageUrl ){
    if( substr($sPageUrl, -8) == 'attorney' ) { return true; }
    return false;
}
This function is required only if you add custom page name e.g., attorney to rewrite rules.
Function name has to be: abcfslcp_is_single_pretty. It can’t be changed.
For each page name rule in rewrite rules there has to be matching entry in abcfslcp_is_single_pretty.
if( substr($sPageUrl, -8) == 'attorney' ) { return true; }

Refreshing rewrite rules.

To activate your own pretty permalinks settings you need to refresh WordPress rewrite rules.

  1. WordPress admin Settings > Permalinks.
  2. On the Permalink Settings page click Save Settings.

Don’t make any changes. Just open the page and click Save.

  • You need to do it only once, after you install and activate your plugin.
  • You have to run the refresh procedure again after any changes to the rewrite rules.

Rewrite Rules Inspector

WordPress plugin Rewrite Rules Inspector is a simple development tool for viewing all of the rewrite rules registered with your site. You can install it if you wish.
10655