This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // Redirect to the query URL if the URL matches the regex | |
| function cfqar_register_rewrite_rule() | |
| { | |
| add_rewrite_rule('^(([0-9]{1,2})([a-z]|[A-Z])([0-9]))/?$', 'index.php?qr=$matches[1]', 'top'); | |
| } | |
| add_action( 'init', 'cfqar_register_rewrite_rule' ); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // Try to get the post with the queried custom field value and do the actual redirect (or throw an 404) | |
| function cfqar_redirect() | |
| { | |
| if( get_query_var( 'qr' ) ) | |
| { | |
| $posts = get_posts([ | |
| 'post_type' => 'product', | |
| 'meta_key' => 'qr-code', | |
| 'meta_value' => get_query_var( 'qr' ) | |
| ]); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // Add custom query variable | |
| function cfqar_register_query_var( $vars ) | |
| { | |
| $vars[] = 'qr'; | |
| return $vars; | |
| } | |
| add_filter( 'query_vars', 'cfqar_register_query_var' ); |