Programmatically add a custom url route “/category/index.json” to return a collection of posts in json

Turns out i was able to make it work by hooking into parse_request . add_action(‘parse_request’, ‘serve_index_json’, 0); function serve_index_json($wp) { // Exit early if not the right request if (‘my-category-slug/index.json’ !== $wp->request) { return; } $data = array(); $args = [ ‘posts_per_page’ => -1, ‘category_name’ => ‘my-category-slug’, ‘orderby’ => ‘date’, ‘order’ => ‘DESC’, ‘post_status’ => … Read more

Rewrite and replace url wp-admin/edit.php and wp-admin/post-new.php

The technical answer to this is to put an .htaccess file inside your wp-admin folder with this code: # Remove .php from pages so you can use /wp-admin/edit RewriteEngine on RewriteCond %{REQUEST_FILENAME} !-d RewriteCond %{REQUEST_FILENAME}\.php -f RewriteRule ^(.*)$ $1.php # Use /wp-admin/addnewpost RewriteRule addnewpost post-new.php The practical answer is that this is completely horrible, involves … Read more

URI rewriting: handling one page

You HAVE TO add a rewrite rule to WordPress. That can be done withou touching .htaccess. For example: add_action(‘init’, ‘cyb_rewrite_rule’); function cyb_rewrite_rule() { add_rewrite_rule( ‘toto/([^/]+)/?$’, ‘index.php?pagename=$matches[1]’, ‘top’ ); } If you need access to the second part of the URL as query var, you can do this: add_action(‘init’, ‘cyb_rewrite_rule’); function cyb_rewrite_rule() { add_rewrite_rule( ‘toto/([^/]+)/?$’, ‘index.php?pagename=$matches[1]&secondpart=$matches[2]’, … Read more

How to make custom WordPress page deliver search results

Maybe it´s easier to output some content from a different page/post in your search template which got a fixed slug. I for example use this: $page = get_posts(array(‘name’ => ‘welcome’)); if ($page) { echo ‘<h1>’.$page[0]->post_title.'</h1>’; echo ‘<p>’.$page[0]->post_content.'</p>’; } This fetches the post with the slug “welcome” and displays its content. As I don´t use a … Read more

Rewrite Page Parameters

If city belongs to the page post type, then your rule should either be: add_rewrite_rule( ‘^city/([^/]*)/?’, ‘index.php?pagename=city&id=$matches[1]’, ‘top’ ); or: add_rewrite_rule( ‘^city/([^/]*)/?’, ‘index.php?page_id=5&id=$matches[1]’, ‘top’ ); Using p, the query will assume post type is post, not page. Also, if your code uses $_GET[‘id’] to fetch the value, this will no longer work with a rewrite … Read more