php - wordpress passing parameter to custom template -
i using wordpress. have created page dealer-profile admin , assigned template it. want pass parameter like
site-url/dealer-profile/suv
i have added following .htaccess
rewriterule ^dealer-profile/([a-za-z0-9_-]+)(|/)$ index.php?pagename=dealer-profile&dealer=$1 [qsa]
i have tried following
add_rewrite_rule('dealer-profile/([^/]+)', 'index.php?pagename=dealer- profile&dealer=$matches[1]', 'top'); flush_rewrite_rules(false);
when requesting site-url/dealer-profile/suv
, automatically redirects site-url/dealer-profile
please suggest, wrong.
now doing following
function themeslug_query_vars( $qvars ) { $qvars[] = 'dealer'; return $qvars; } add_filter( 'query_vars', 'themeslug_query_vars' , 10, 1 ); function add_rewrite_rules($arules) { $anewrules = array('dealer-profile/([^/]+)/?$' => 'index.php?pagename=dealer-profile&dealer=$matches[1]'); $arules = $anewrules + $arules; return $arules; } add_filter('rewrite_rules_array', 'add_rewrite_rules');
but working digits - test/dealer-profile/234234/ working - test/dealer-profile/234234/ not working
solution
ref : http://codex.wordpress.org/class_reference/wp_rewrite
add_filter( 'rewrite_rules_array','my_insert_rewrite_rules' ); add_filter( 'query_vars','my_insert_query_vars' ); add_action( 'wp_loaded','my_flush_rules' ); // flush_rules() if our rules not yet included function my_flush_rules(){ $rules = get_option( 'rewrite_rules' ); if ( ! isset( $rules['(dealer-profile)/(.*)$'] ) ) { global $wp_rewrite; $wp_rewrite->flush_rules(); } } // adding new rule function my_insert_rewrite_rules( $rules ) { $newrules = array(); $newrules['dealer-profile/(.*)$'] = 'index.php?pagename=dealer-profile&dealer=$matches[1]'; return $newrules + $rules; } // adding id var wp recognizes function my_insert_query_vars( $vars ) { array_push($vars, 'dealer'); return $vars; }
thanks lot @@jnhghy - jantea alexandri
you need create function add query variables query string , hook function query_vars hook:
function add_query_vars($avars) { $avars[] = "dealer-profile"; // represents name of variable want pass } // hook add_query_vars function query_vars add_filter('query_vars', 'add_query_vars');
then need add rewrite rule: function add_rewrite_rules($arules) {
function add_rewrite_rules($arules) { $anewrules = array('dealer-profile/([^/]+)/?$' => 'index.php?pagename=dealer-profile=$matches[1]'); $arules = $anewrules + $arules; return $arules; } // hook add_rewrite_rules function rewrite_rules_array add_filter('rewrite_rules_array', 'add_rewrite_rules');
getting variable:
if(isset($wp_query->query_vars['dealer-profile'])) { $smsdscat = urldecode($wp_query->query_vars['dealer-profile']); }
and should trick, , here link "advanced rules": http://www.rlmseo.com/blog/passing-get-query-string-parameters-in-wordpress-url/ http://www.prodeveloper.org/create-your-own-rewrite-rules-in-wordpress.html
edit: let try steps have in second article: first let's create tag , add rewrite rules (this should work alpha-numeric values:
function createrewriterules() { global $wp_rewrite; // add rewrite tokens $dealertag = '%dealertag%'; $wp_rewrite->add_rewrite_tag($dealertag, '(.+?)', 'dealer-profile='); $keywords_structure = $wp_rewrite->root . "dealer-profile/$dealertag/"; $keywords_rewrite = $wp_rewrite->generate_rewrite_rules($keywords_structure); $wp_rewrite->rules = $keywords_rewrite + $wp_rewrite->rules; return $wp_rewrite->rules; } add_action('generate_rewrite_rules', 'createrewriterules');
then hook generate rules this:
function add_rewrite_rules( $wp_rewrite ) { add_rewrite_rule('^dealer-profile/([^/]*)/?','index.php?page_id=12&dealer-profile=$matches[1]','top'); } add_action('generate_rewrite_rules', 'add_rewrite_rules');
add variable query vars:
function query_vars($public_query_vars) { $public_query_vars[] = "dealer-profile"; return $public_query_vars; add_filter('query_vars', 'query_vars');
bonus: debug rules can use function:
function dev4press_debug_rewrite_rules() { global $wp_rewrite; echo '<div>'; if (!empty($wp_rewrite->rules)) { echo '<h5>rewrite rules</h5>'; echo '<table><thead><tr>'; echo '<td>rule</td><td>rewrite</td>'; echo '</tr></thead><tbody>'; foreach ($wp_rewrite->rules $name => $value) { echo '<tr><td>'.$name.'</td><td>'.$value.'</td></tr>'; } echo '</tbody></table>'; } else { echo 'no rules defined.'; } echo '</div>'; }
it's https://docs.dev4press.com/tutorial/wordpress/debug-wordpress-rewrite-rules-matching/ , should show rules in table format, include in of templates , check if above functions adding rewrite rules correctly if not, should give info didn't worked.
and can use known developer plugin inspect rules, you'll info plugin!!!
Comments
Post a Comment