Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add action that enables custom rewrite rules to be added #15

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

timrross
Copy link

This PR adds an action hook after the main page rewrite rules have been added, that allows setting up of extra rewrite rules based on the new rewrite slug that's defined in this plugin.

Use case 1:
I need to add an extra rewrite that allows the year to be appended to the end of the url to browse the 'portfolio' cpt archives by year.

function my_page_for_portfolio_rewrite_rules($archive_slug, $post_type, $args) {
    global $wp_rewrite;
    add_rewrite_rule( "{$archive_slug}/([0-9]{4})/?$", "index.php?post_type=$post_type" . 'year=$matches[1]' .  'top' );
    if ( $args->rewrite['pages'] ) {
        add_rewrite_rule( "{$archive_slug}/([0-9]{4})/{$wp_rewrite->pagination_base}/([0-9]{1,})/?$", "index.php?post_type=$post_type" . '&year=$matches[1]' . '&paged=$matches[2]', 'top' );
    }
}
add_action('page_for_portfolio_rewrite_rules', 'my_page_for_portfolio_rewrite_rules', 10, 3);

Thanks,
Tim

Copy link
Contributor

@roborourke roborourke left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Interesting use case. I had thought that date based archives should be automatically generated for non-hierarchical post types but if not it's something this plugin should add support for.

Happy to include this action hook anyway, just added a suggestion to apply then good to go.

@@ -259,6 +259,8 @@ public function update_post_type( $post_type, $args ) {
}
}

do_action("page_for_{$post_type}_rewrite_rules", $archive_slug, $post_type, $args);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Need to match the existing code style here, it's also good to add doc blocks to actions and filters.

Suggested change
do_action("page_for_{$post_type}_rewrite_rules", $archive_slug, $post_type, $args);
/**
* Action called after generating initial rewrite rules for a page as post type archive.
*
* @param string $archive_slug The slug for the post type archive page.
* @param string $post_type The post type name.
* @param WP_Post_Type $post_type_object The post type object.
*/
do_action( "page_for_{$post_type}_rewrite_rules", $archive_slug, $post_type, $args );

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants