BuddyDev

Search

[Resolved] change in post permalink structure

  • Participant
    Level: Guru
    Posts: 657
    Tosin on #44182

    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 );
  • Participant
    Level: Guru
    Posts: 657
    Tosin on #44183

    I have tested it and it works but I just want to be sure if the code is safe and will not cause any security issue.

  • Keymaster
    (BuddyDev Team)
    Posts: 21488
    Brajesh Singh on #44195

    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
    Brajesh

  • Participant
    Level: Guru
    Posts: 657
    Tosin on #44249

    Thanks for the feedback, the (advert) post type is registered by a 3rd party plugin

  • Keymaster
    (BuddyDev Team)
    Posts: 21488
    Brajesh Singh on #44255

    ok. In that canse, Pleasde keep using the code.

    Regards
    Brajesh

You must be logged in to reply to this topic.

This topic is: resolved