در این صفحه از وردپرس در این مورد به طور کامل توضیح داده شده :
https://developer.wordpress.org/reference/functions/add_rewrite_rule/
شکل کلی دستور به صورت زیر هست :
add_rewrite_rule( string $regex, string|array $query, string $after = 'bottom' )
Parameters
$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’
نمونه کد برای اضافه کردن به فایل فانکشن وردپرس
add_action( 'init', function() { add_rewrite_rule( 'p/([a-z0-9-]+)[/]?$', '?p=$matches[1]', 'top' ); } ); add_filter( 'query_vars', function( $query_vars ) { $query_vars[] = 'p'; return $query_vars; } ); add_action( 'template_include', function( $template ) { if ( get_query_var( 'p' ) == false || get_query_var( 'p' ) == '' ) { return $template; } return get_template_directory() . '/new-single.php'; } );
کد زیر ابتدا آی دی صفحه رو بررسی می کنه و در صورتی که شرط برقرار باشه لینک صفحه رو تغییر میده
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' ); });
نمونه 2
add_filter('rewrite_rules_array', 'insert_custom_rules'); add_filter('query_vars', 'insert_custom_vars'); function insert_custom_vars($vars){ $vars[] = 'property_id'; return $vars; } function insert_custom_rules($rules) { $newrules = array(); $newrules=array( 'advanced-search/(.+)/?' => 'index.php?pagename=advanced-search&property_id=$matches[1]' ); return $rules+$newrules; }