I have a custom post type News.

Also I have added page with front-end post edit.

At this moment I can edit my news by opening this url:

host.com/edit-news/?id=21231

I want to make a page with url looks like with dynamically changing id of post:

host.com/news/21231/edit

Where I need to make changes, to make an ability for adding this url to my specific page in WordPress?

1 Answer
1

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')

Tags:

Leave a Reply

Your email address will not be published. Required fields are marked *