WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] Add rewrite rule with one unicode character (3 posts)

  1. moradwan
    Member
    Posted 1 year ago #

    Hi,

    I'm having a hard time forcing a rewrite rule with certain criteria in wordpress, and I'm really hoping that someone can help...

    The URL structure for the category is as follows
    http://mydomain/mycategory/

    I want to add a rewrite rule by adding add_rewrite_rule to functions.php to allow one Arabic character like the following
    http://mydomain/mycategory/أ/

    But the regex must be for one and only one character because there are articles inside that category, thus, if there is anything else that is not a single character it will be a post.

    I already implemented that but for the English language using the following code

    $wp->add_query_var('letter');
    
    add_rewrite_rule(
      '(mycategory)(/s*[a-zA-Z])?/?$','index.php?category_name=$matches[1]&letter=$matches[2]','top'
    );

    Thanks in advance.

  2. moradwan
    Member
    Posted 1 year ago #

    Resolved using the following:

    add_rewrite_rule(
    		'(stars-a-z/)([أ-ي]+)/?$','index.php?category_name=$matches[1]&letter=$matches[2]','top'
    	);
  3. moradwan
    Member
    Posted 1 year ago #

    Update

    I found a bug in the code which caused the letters after غ not to be matched.
    After some researches, I found that there are two ranges of characters... From أ to غ and from ف to ي.

    http://www.unicodemap.org/range/12/Arabic/

    So I added both ranges and adjusted the code to be as below:

    add_rewrite_rule(
        		'(mycategory/)([أ-غa-zف-ي]+)/?$','index.php?category_name=$matches[1]&letter=$matches[2]','top'
    );

    Note that I added a-z in the middle just to split the letters, however, the following code is also working properly.

    add_rewrite_rule(
        		'(mycategory/)([أ-غف-ي]+)/?$','index.php?category_name=$matches[1]&letter=$matches[2]','top'
    );

Topic Closed

This topic has been closed to new replies.

About this Topic