Hello,
Please can you take a look at this code if its safe to use. I want to add the (article) pre slug in my blog posts but wordpress does not allow this in the permalink settings without it affectig other custom post types.
Post post type should have (article) pre slug e.g https://www.website.com/article/fdfghfgf/
Advert post type should have (advert) pre slug e.g https://www.website.com/advert/drdrtytf/
Pages should not have any pre slug https://www.website.com/ytrytytyu/see code below
/** * Rewrite WordPress URLs to Include /article/ in Post Permalink Structure * * @author Golden Oak Web Design <info@goldenoakwebdesign.com> * @license https://www.gnu.org/licenses/gpl-2.0.html GPLv2+ */ function np_generate_rewrite_rules( $wp_rewrite ) { $new_rules = array( '(([^/]+/)*article)/page/?([0-9]{1,})/?$' => 'index.php?pagename=$matches[1]&paged=$matches[3]', 'article/([^/]+)/?$' => 'index.php?post_type=post&name=$matches[1]', 'article/[^/]+/attachment/([^/]+)/?$' => 'index.php?post_type=post&attachment=$matches[1]', 'article/[^/]+/attachment/([^/]+)/trackback/?$' => 'index.php?post_type=post&attachment=$matches[1]&tb=1', 'article/[^/]+/attachment/([^/]+)/feed/(feed|rdf|rss|rss2|atom)/?$' => 'index.php?post_type=post&attachment=$matches[1]&feed=$matches[2]', 'article/[^/]+/attachment/([^/]+)/(feed|rdf|rss|rss2|atom)/?$' => 'index.php?post_type=post&attachment=$matches[1]&feed=$matches[2]', 'article/[^/]+/attachment/([^/]+)/comment-page-([0-9]{1,})/?$' => 'index.php?post_type=post&attachment=$matches[1]&cpage=$matches[2]', 'article/[^/]+/attachment/([^/]+)/embed/?$' => 'index.php?post_type=post&attachment=$matches[1]&embed=true', 'article/[^/]+/embed/([^/]+)/?$' => 'index.php?post_type=post&attachment=$matches[1]&embed=true', 'article/([^/]+)/embed/?$' => 'index.php?post_type=post&name=$matches[1]&embed=true', 'article/[^/]+/([^/]+)/embed/?$' => 'index.php?post_type=post&attachment=$matches[1]&embed=true', 'article/([^/]+)/trackback/?$' => 'index.php?post_type=post&name=$matches[1]&tb=1', 'article/([^/]+)/feed/(feed|rdf|rss|rss2|atom)/?$' => 'index.php?post_type=post&name=$matches[1]&feed=$matches[2]', 'article/([^/]+)/(feed|rdf|rss|rss2|atom)/?$' => 'index.php?post_type=post&name=$matches[1]&feed=$matches[2]', 'article/page/([0-9]{1,})/?$' => 'index.php?post_type=post&paged=$matches[1]', 'article/[^/]+/page/?([0-9]{1,})/?$' => 'index.php?post_type=post&name=$matches[1]&paged=$matches[2]', 'article/([^/]+)/page/?([0-9]{1,})/?$' => 'index.php?post_type=post&name=$matches[1]&paged=$matches[2]', 'article/([^/]+)/comment-page-([0-9]{1,})/?$' => 'index.php?post_type=post&name=$matches[1]&cpage=$matches[2]', 'article/([^/]+)(/[0-9]+)?/?$' => 'index.php?post_type=post&name=$matches[1]&page=$matches[2]', 'article/[^/]+/([^/]+)/?$' => 'index.php?post_type=post&attachment=$matches[1]', 'article/[^/]+/([^/]+)/trackback/?$' => 'index.php?post_type=post&attachment=$matches[1]&tb=1', 'article/[^/]+/([^/]+)/feed/(feed|rdf|rss|rss2|atom)/?$' => 'index.php?post_type=post&attachment=$matches[1]&feed=$matches[2]', 'article/[^/]+/([^/]+)/(feed|rdf|rss|rss2|atom)/?$' => 'index.php?post_type=post&attachment=$matches[1]&feed=$matches[2]', 'article/[^/]+/([^/]+)/comment-page-([0-9]{1,})/?$' => 'index.php?post_type=post&attachment=$matches[1]&cpage=$matches[2]', ); $wp_rewrite->rules = $new_rules + $wp_rewrite->rules; } add_action( 'generate_rewrite_rules', 'np_generate_rewrite_rules' ); function np_update_post_link( $post_link, $id = 0 ) { $post = get_post( $id ); if( is_object( $post ) && $post->post_type == 'post' ) { return home_url( '/article/' . $post->post_name.'/' ); } return $post_link; } add_filter( 'post_link', 'np_update_post_link', 1, 3 );
Hi Tosin,
Thank you.
It seems fine but most of the code is not even necessary. There is no need to rewrite the attachment/feeds etc.also, if you change the permalink in WordPress, the front value only affects other post types(custom post types) when with front is true. If you have only limited number of custom post types and you have control over how they are being registered, you can simply add rewrite=>array( ‘with_front’ => false)
This approach will be much suited if you have control over registered post type.
If the post types are being registered by 3rd party plugins, you may keep the above code insead.
Regards
Brajeshok. In that canse, Pleasde keep using the code.
Regards
BrajeshHello Brajesh,
Im currently testing buddypress version 11.0 beta 2 and I discovered an issue/conflict with the code I shared above.
After publishing an article, the activity link is being displayed in this format (https://www.site.com/article//) instead of (https://www.site.com/?p=40926). This (https://www.site.com/article//) leads to 404 error.
This issue started when this feature was added https://buddypress.trac.wordpress.org/ticket/8052
Can you help simply the code I shared with you so it does not affect the links in activity update output.
Thanks
Can you take a look at this https://gist.github.com/dzeroone/4cb02ad04ac8309e7fd7
Hi Tosin,
I am not seeing any reason for the BP changes to cause it.
Also, I am not sure why do you need the last snippet.Regards
Brajesh
You must be logged in to reply to this topic.