add_rewrite_rule()

最后更新于:2021-11-25 19:27:28

add_rewrite_rule( string$regex, string|array$query, string$after=’bottom’)

Adds a rewrite rule that transforms a URL structure to a set of query vars.

参数

$regex

(string) (Required) Regular expression to match request against.

$query

(string|array) (Required) The corresponding query vars for this rewrite rule.

$after

(string) (Optional) Priority of the new rule. Accepts ‘top’ or ‘bottom’.

Default value: ‘bottom’

源文件

文件: gc-includes/rewrite.php

function add_rewrite_rule( $regex, $query, $after = 'bottom' ) {
	global $gc_rewrite;

	$gc_rewrite->add_rule( $regex, $query, $after );
}
add_action('init', function() {

	$page_id = 2; // update 2 (sample page) to your custom page ID where you can get the subscriber(s) data later
	$page_data = get_post( $page_id );

	if( ! is_object($page_data) ) { // post not there
		return;
	}

	add_rewrite_rule(
		$page_data->post_name . '/subscriber/([^/]+)/?$',
		'index.php?pagename=' . $page_data->post_name . '&my_subscribers=1&my_subscriber=$matches[1]',
		'top'
	);

});
// siteurl.com/gcdocs-product-category/edit-product-category/43 for edit product category
add_rewrite_rule(
	'^gcdocs-product-category/([^/]*)/([^/]*)/?',
	'index.php?pagename=gcdocs-product-category&gcdocs-pro-category-action=$matches[1]&gcdocs-product-cat-id=$matches[2]',
	'top'
);

// Product Category Add Page
// siteurl.com/gcdocs-product-category/add-product-category for add product category
add_rewrite_rule(
	'^gcdocs-product-category/([^/]*)/?',
	'index.php?pagename=gcdocs-product-category&gcdocs-pro-category-action=$matches[1]',
	'top'
);