Skip to content

Instantly share code, notes, and snippets.

@nash-ye
Last active June 25, 2020 13:58
Show Gist options
  • Select an option

  • Save nash-ye/3d8e68d3df85b1759e39 to your computer and use it in GitHub Desktop.

Select an option

Save nash-ye/3d8e68d3df85b1759e39 to your computer and use it in GitHub Desktop.
bbPress (Topics and Repiles) Numeric Permalinks
<?php
if ( ! did_action( 'bbp_after_setup_actions' ) ) {
add_action( 'bbp_after_setup_actions', 'n5n_after_bbPress_setup' );
} else {
n5n_after_bbPress_setup();
}
/**
* After bbPress Setup.
*
* @author Nashwan Doaqan
*/
function n5n_after_bbPress_setup() {
if ( function_exists( 'is_bbpress' ) ) {
add_action( 'registered_post_type', 'n5n_bbp_add_post_types_rewrite', 1, 2 );
add_filter( 'rewrite_rules_array', 'n5n_bbp_filter_post_types_rewrite_rules', 1 );
add_filter( 'post_type_link', 'n5n_bbp_filter_post_type_link', 1, 2 );
}
}
/**
* Add the numeric permalink structure to bbPress topics and replies.
*
* @author Nashwan Doaqan
*/
function n5n_bbp_add_post_types_rewrite( $post_type , $args ) {
switch( $post_type ) {
case bbp_get_reply_post_type():
case bbp_get_topic_post_type():
add_rewrite_tag( "%{$post_type}_id%", '([0-9]+)', "post_type={$post_type}&p=" );
add_permastruct( $post_type, "{$args->rewrite['slug']}/%{$post_type}_id%", $args->rewrite );
break;
}
}
/**
* Change the bbPress topics and replies rewrite rules.
*
* @author Nashwan Doaqan
*/
function n5n_bbp_filter_post_types_rewrite_rules( $rules ) {
global $wp_rewrite;
$edit_rule = '/([^/]+)/edit/?$';
$edit_id = bbp_get_edit_rewrite_id();
$rules[ bbp_get_topic_slug() . $edit_rule ] = 'index.php?post_type=' . bbp_get_topic_post_type() . '&p=' . $wp_rewrite->preg_index( 1 ) . '&' . $edit_id . '=1';
$rules[ bbp_get_reply_slug() . $edit_rule ] = 'index.php?post_type=' . bbp_get_reply_post_type() . '&p=' . $wp_rewrite->preg_index( 1 ) . '&' . $edit_id . '=1';
return $rules;
}
/**
* Change bbPress post types links.
*
* @author Nashwan Doaqan
*/
function n5n_bbp_filter_post_type_link( $post_link , $_post ) {
global $wp_rewrite;
if ( empty( $_post ) )
return $post_link;
switch( $_post->post_type ) {
case bbp_get_reply_post_type():
case bbp_get_topic_post_type():
$post_link = $wp_rewrite->get_extra_permastruct( $_post->post_type );
$post_link = str_replace( "%{$_post->post_type}_id%", $_post->ID, $post_link );
$post_link = home_url( user_trailingslashit( $post_link ) );
break;
}
return $post_link;
}
@janfeng

janfeng commented Oct 5, 2014

Copy link
Copy Markdown

Hello , can u tell me how to use this file ? thanks

@nash-ye

nash-ye commented Oct 15, 2014

Copy link
Copy Markdown
Author

@janfeng Create your own hacks plugin, or use Code Snippets plugin. and put the codes above in it.. and don't forget to update the Permalinks settings to fire the Rewrite Rules actions

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment