Last active
February 18, 2022 09:00
-
-
Save janboddez/31a44dbf7fa952122c663b5e0606f059 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Adds a "copy" of your site's "blog" page at `/stream/`. Visit "Settings > | |
* Permalinks" to flush rewrite rules after adding this code. | |
*/ | |
add_action( 'init', function() { | |
add_rewrite_rule( '^stream/feed/?', 'index.php?feed=rss2', 'top' ); | |
add_rewrite_rule( '^stream/page/([0-9]{1,})/?', 'index.php?paged=$matches[1]', 'top' ); | |
add_rewrite_rule( '^stream/?', 'index.php', 'top' ); | |
} ); | |
/** | |
* Excludes a certain category from (only) the (actual) "blog" page. | |
*/ | |
add_action( 'pre_get_posts', function( $query ) { | |
global $wp; | |
if ( $query->is_main_query() | |
&& 0 !== strpos( $wp->request, 'stream' ) | |
&& ( is_home() || ( is_feed() && ! is_comment_feed() && ( empty( $query->query_vars['post_type'] ) || 'post' === $query->query_vars['post_type'] ) ) ) ) { | |
// Main query, not a `stream` URL, and either the "blog" page or a feed (but not a comment feed!) for which the post type is either `post` or not set at all. | |
$query->set( 'category__not_in', array( 1 ) ); | |
} | |
} ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Smart! On one of my blogs, I do it the other way around. All "standard" feeds except RSS2 (for which I use a custom template, that's why) lead to a 404 error. Then I only have to worry about that one anymore. :-)
Hmm, not sure if I understand it correctly, but this bit should actually prevent that:
You might wanna think of an alternative, then.
0 !== strpos( $_SERVER['REQUEST_URI'], '/stream' )
or something.Hm, that would also target, say, category or CPT feeds, and so on. You really want to target only the main feed, and that takes some knowledge on what is really included there. Like, if in another must-use plugin you include a CPT or something, you may want to explicitly check for
array( 'post', 'my_cpt_slug' ) === $query->query_vars['post_type']
instead. Only way, I think, to guarantee you're targeting the main feed only.Come to think of it, my version probably also targets category feeds. Better to explicitly check the URL, using, e.g.,
'feed' === $wp->request
. (Untested!)The best way to go about this is probably "debug log" the$wp
and$query
objects and see what parameters are actually present for the feeds you want to target.This is normal. WordPress doesn't know about the feed. Same reason it wasn't "auto-generated." Using rewrite rules like this is much more low-level than, e.g., custom post types, which give you a ton of functionality "for free." All we're doing is defining new "routes" and linking them to a "controller method." Nothing else ("magically") happens.
Look into the
wp_head
action, which will allow you to add markup to your site's<head>
.