I'm working on a WordPress site – I have a static page created with a page builder of a theme that I've purchased (slug: 'custom-page') and a category page (slug: 'news').
What I want to achieve is when a user navigates to the url /news
, WP should render the content of the 'custom-page' as if the user had navigated directly to it, but without performing any visible redirects. The url should still display /news
.
I'm working on a plugin that should do it - I've tried using the template_redirect
hook - it partially worked, the 'custom-page' content displayed, but an additional header from the theme also appeared, which is not part of this page, and is not displayed if I navigate directly to '/custom-page'.
Could anyone suggest a solution or point out what I might be missing?
Thanks
If custom-page
is not from a custom post type you could try add_rewrite_rule.
add_action( 'init', function() {
add_rewrite_rule( 'news[/]?$', 'index.php?pagename=custom-page', 'top' );
} );
The only visible change you might see is the addition of a trailing slash to a URL that didn't have one. How trailing slashes are handled is up to you.
WordPress Documentation says that permalinks will need to be refreshed [...] before the rewrite changes will take effect. To refresh, go to the admin Settings -> Permalinks page and click Save Changes
.