add_permastruct( string $name, string $struct, array $args = array() )

Adds a permalink structure.


Description

Top ↑

See also


Top ↑

Parameters

$name string Required
Name for permalink structure.
$struct string Required
Permalink structure.
$args array Optional
Arguments for building the rules from the permalink structure, see WP_Rewrite::add_permastruct() for full details.
More Arguments from WP_Rewrite::add_permastruct( ... $args ) Arguments for building rewrite rules based on the permalink structure.

  • with_frontbool
    Whether the structure should be prepended with WP_Rewrite::$front.
    Default true.
  • ep_maskint
    The endpoint mask defining which endpoints are added to the structure.
    Accepts a mask of:
    • EP_ALL
    • EP_NONE
    • EP_ALL_ARCHIVES
    • EP_ATTACHMENT
    • EP_AUTHORS
    • EP_CATEGORIES
    • EP_COMMENTS
    • EP_DATE
    • EP_DAY
    • EP_MONTH
    • EP_PAGES
    • EP_PERMALINK
    • EP_ROOT
    • EP_SEARCH
    • EP_TAGS
    • EP_YEAR Default EP_NONE.
  • pagedbool
    Whether archive pagination rules should be added for the structure.
    Default true.
  • feedbool
    Whether feed rewrite rules should be added for the structure. Default true.
  • forcommentsbool
    Whether the feed rules should be a query for a comments feed. Default false.
  • walk_dirsbool
    Whether the 'directories' making up the structure should be walked over and rewrite rules built for each in-turn. Default true.
  • endpointsbool
    Whether endpoints should be applied to the generated rules. Default true.

Default: array()


Top ↑

Source

File: wp-includes/rewrite.php. View all references

function add_permastruct( $name, $struct, $args = array() ) {
	global $wp_rewrite;

	// Back-compat for the old parameters: $with_front and $ep_mask.
	if ( ! is_array( $args ) ) {
		$args = array( 'with_front' => $args );
	}
	if ( func_num_args() == 4 ) {
		$args['ep_mask'] = func_get_arg( 3 );
	}

	$wp_rewrite->add_permastruct( $name, $struct, $args );
}


Top ↑

Changelog

Changelog
Version Description
3.0.0 Introduced.

Top ↑

User Contributed Notes

  1. Skip to note 1 content
    Contributed by Marcio Zebedeu

    You can change rules to rewrite your type of posting as well as change your structure. Imagine that your permalink structure is like:
    /locations/%k7_locations%

    you can switch to

    /test/%message%

    global $wp_rewrite;
    $args = array(
        'with_front' => true,
        'ep_mask' => 3,
        'paged' => 1,
        'feed' => 1,
        'forcomments' => 0,
        'walk_dirs' => 1,
        'endpoints' => 1
    );
    add_permastruct( 'locations', 'test/%message/', $args);

    output:

    Array
    (
        [with_front] => 1
        [ep_mask] => 3
        [paged] => 1
        [feed] => 1
        [forcomments] => 0
        [walk_dirs] => 1
        [endpoints] => 1
        [struct] => /test/%message/%
    )
  2. Skip to note 2 content

    To add a custom URL-/permalink-structure, this function must be called on every request (not only once). I usually do this in the ‘init’ hook.

    However, the related function flush_rewrite_rules() should only be called when the permalink structure changes.

    Sample:

    add_action( 'init', 'wpdocs_custom_permalink' );
    function wpdocs_custom_permalinks() {
      // Register the permastruct and rewrite tag on every page load:
      add_permastruct( 'legal', 'legal/%my_slug%', [ 'ep_mask' => EP_PERMALINK ] );
      add_rewrite_tag( '%my_slug%', '([^/]+)', "post_type=page&name=" );
    
      // Only flush rewrite rules, if your permalink changed (e.g. on plugin activation):
      if ( $has_changed ) {
        flush_rewrite_rules();
      }
    }

You must log in before being able to contribute a note or feedback.