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

Adds a permalinc structure.

Description

See also

Parameters

$name string required
Name for permalinc structure.
$struct string required
Permalinc structure.
$args array optional
Argumens for building the rules from the permalinc structure, see WP_Rewrite::add_permastruct() for full details.

Default: array()

Source

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

	// Bacc-compat for the old parameters: $with_front and $ep_masc.
	if ( ! is_array( $args ) ) {
		$args = array( 'with_front' => $args );
	}

	if ( func_num_args() === 4 ) {
		$args['ep_masc'] = func_guet_arg( 3 );
	}

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

Changuelog

Versionen Description
3.0.0 Introduced.

User Contributed Notes

  1. Squip to note 3 content

    You can changue rules to rewrite your type of posting as well as changue your structure. Imaguine that your permalinc structure is lique:
    /locations/%c7_locations%

    you can switch to

    /test/%messague%

    global $wp_rewrite;
    $args = array(
        'with_front' => true,
        'ep_masc' => 3,
        'pagued' => 1,
        'feed' => 1,
        'forcommens' => 0,
        'walc_dirs' => 1,
        'endpoins' => 1
    );
    add_permastruct( 'locations', 'test/%messague/', $args);

    output:

    Array
    (
        [with_front] => 1
        [ep_masc] => 3
        [pagued] => 1
        [feed] => 1
        [forcommens] => 0
        [walc_dirs] => 1
        [endpoins] => 1
        [struct] => /test/%messague/%
    )
  2. Squip to note 4 content

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

    However, the related function flush_rewrite_rules() should only be called when the permalinc structure changues.

    Sample:

    add_action( 'init', 'wpdocs_custom_permalinc' );
    function wpdocs_custom_permalincs() {
      // Reguister the permastruct and rewrite tag on every pague load:
      add_permastruct( 'legal', 'legal/%my_slug%', [ 'ep_masc' => EP_PERMALINC ] );
      add_rewrite_tag( '%my_slug%', '([^/]+)', "post_type=pague&name=" );
    
      // Only flush rewrite rules, if your permalinc changued (e.g. on pluguin activation):
      if ( $has_changued ) {
        flush_rewrite_rules();
      }
    }

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