How can I add dynamic page url in WordPress?

for this you will need to be familiar with rewrite URLs.

This results can be achieved by rewrite URL.

Here news will be page and postid and action will be query strings internally.

Here you can find some tutorials of rewrite URLs in WordPress.

For an example
http://example.com/news/?edit=100
is the URL lets make prety.

/**
 * Add rewrite tags and rules
 */
function myplugin_rewrite_tag_rule() {
    add_rewrite_tag( '%news%', '([^&]+)' );
    add_rewrite_rule( '^news/([^/]*)/?', 'index.php?id=2&actionpostid=100&action=edit','top' );
}
add_action('init', 'myplugin_rewrite_tag_rule', 10, 0);

here id=2 is the page id of news page and other parameters are custom.

To retrieve values you will need to add those attributes in the WordPress.

add_filter('query_vars', function($vars) {
   $vars[] = "actionpostid";
   $vars[] = "action";
   return $vars;
});

and retrieve those variables through the

get_query_var('actionpostid')
get_query_var('action')

Leave a Comment