Forum Replies Created

Viewing 2 replies - 1 through 2 (of 2 total)
  • Thread Starter em2007

    (@em2007)

    Fixed, one of my rules was overriding the nsur’s rule.

    Thread Starter em2007

    (@em2007)

    Thanks for the quick reply.

    On the website I have rewritten some of the urls for categories, custom post types, custom taxonomies, posts and pages, I get a 404 error on this redirection : subsite.site.com/local-signup/.

    I am creating my custom links like this :

    class EDPRewriteLT implements PAPIActionHookInterface, PAPIFilterHookInterface {
    
      public function __construct() {
    
        register_activation_hook(__FILE__, [$this, 'plugin_activate']);
    
        register_deactivation_hook(__FILE__, [$this, 'plugin_deactivate']);
      }
    
      /**
       * Implements get_actions() from PAPIActionHookInterface.
       * 
       * Returns an array of actions that the object needs to be hooked to.
       * 
       * @return array
       */
      public static function get_actions() {
    
        return [
          'init' => ['custom_rewrite_rules', 9],
          'wp' => 'set_category_links'
        ];
      }
    
      /**
       * Implements get_filters() from PAPIFilterHookInterface.
       * 
       * Returns an array of filters that the object needs to be hooked to.
       * 
       * @return array
       */
      public static function get_filters() {
    
        return [
          'term_link'           => ['set_taxonomy_links', 10, 3],
          'post_type_link'      => ['set_fiche_produit_links', 10, 2],
          'post_link'           => ['set_post_links', 10, 2],
          'rewrite_rules_array' => ['remove_default_rewrite_rules', 10, 1]
        ];
      }
    
      public function plugin_activate() {
        $this->custom_rewrite_rules();
        flush_rewrite_rules();
      }
    
      public function plugin_deactivate() {
        flush_rewrite_rules();
      }
    
      /**
       * Adds custom rewrite rules
       * For ARTICLES, FICHES PRODUITS, CLASSIFICATION, UNIVERS, CATEGORY
       */
      public function custom_rewrite_rules() {
    
        // Rewrites rules for ARTICLES
        add_rewrite_rule('articles/([^/]+)-([0-9]+)/?$', 'index.php?post_type=post&name=$matches[1]&p=$matches[2]', 'top');
    
        //  Rewrites rules for FICHE PRODUIT
        add_rewrite_rule('([^/]+)-([0-9]+)/?$', 'index.php?post_type=fiche-produit&fiche-produit=$matches[1]&p=$matches[2]', 'top');
    
        // Rewrites rules for CLASSIFICATION
        add_rewrite_rule('articles/([^/]+)/feed/(feed|rdf|rss|rss2|atom)/?$', 'index.php?classification=$matches[1]&feed=$matches[2]', 'top');
        add_rewrite_rule('articles/([^/]+)/(feed|rdf|rss|rss2|atom)/?$', 'index.php?classification=$matches[1]&feed=$matches[2]', 'top');
        add_rewrite_rule('articles/([^/]+)/embed/?$', 'index.php?classification=$matches[1]&embed=true', 'top');
        add_rewrite_rule('articles/([^/]+)/page/?([0-9]{1,})/?$', 'index.php?classification=$matches[1]&paged=$matches[2]', 'top');
        add_rewrite_rule('articles/([^/]+)/?$', 'index.php?classification=$matches[1]', 'top');
        add_rewrite_rule('articles/?$', 'index.php?taxonomy=classification', 'top');
    
        // Rewrites rules for UNIVERS
        add_rewrite_rule('([^/]+)/feed/(feed|rdf|rss|rss2|atom)/?$', 'index.php?univers=$matches[1]&feed=$matches[2]', 'top');
        add_rewrite_rule('([^/]+)/(feed|rdf|rss|rss2|atom)/?$', 'index.php?univers=$matches[1]&feed=$matches[2]', 'top');
        add_rewrite_rule('([^/]+)/embed/?$ ', 'index.php?univers=$matches[1]&embed=true', 'top');
        add_rewrite_rule('([^/]+)/page/?([0-9]{1,})/?$', 'index.php?univers=$matches[1]&paged=$matches[2]', 'top');
        add_rewrite_rule('([^/]+)/?$', 'index.php?univers=$matches[1]', 'top');
    
        // Rewrites rules for CATEGORY 
        add_rewrite_rule('([^/]+)/(.+?)/feed/(feed|rdf|rss|rss2|atom)/?$', 'index.php?univers=$matches[1]&category_name=$matches[2]&feed=$matches[3]', 'top');
        add_rewrite_rule('([^/]+)/(.+?)/(feed|rdf|rss|rss2|atom)/?$', 'index.php?univers=$matches[1]&category_name=$matches[2]&feed=$matches[3]', 'top');
        add_rewrite_rule('([^/]+)/(.+?)/embed/?$', 'index.php?univers=$matches[1]&category_name=$matches[2]&embed=true', 'top');
        add_rewrite_rule('([^/]+)/(.+?)/page/?([0-9]{1,})/?$', 'index.php?univers=$matches[1]&category_name=$matches[2]&paged=$matches[3]', 'top');
        add_rewrite_rule('([^/]+)/(.+?)/?$', 'index.php?univers=$matches[1]&category_name=$matches[2]', 'top');
      }
    
      /**
       * Sets CLASSIFICATION & UNIVERS permalinks
       */
      public function set_taxonomy_links($link, $term, $taxonomy) {
        switch ($taxonomy) {
          case 'classification':
            return str_replace('classification/', 'articles/', $link);
            break;
    
          case 'univers':
            return str_replace('univers/', '', $link);
            break;
    
          default:
            return $link;
            break;
        }
      }
    
      /**
       * Sets FICHE PRODUIT permalinks
       */
      public function set_fiche_produit_links($link, $post) {
        if ($post->post_type === 'fiche-produit') {
          return site_url($post->post_name . '-' . $post->ID);
        } else {
          return $link;
        }
      }
    
      /**
       * Sets default post -> ARTICLE permalinks
       */
      public function set_post_links($link, $post) {
        if ($post->post_type === 'post') {
          return site_url('articles/' . $post->post_name . '-' . $post->ID);
        } else {
          return $link;
        }
      }
    
      /**
       * Sets default CATEGORY links to 404
       */
      public function set_category_links($wp) {
        if (preg_match('/category\//', $wp->request)) {
          global $wp_query;
          $wp_query->set_404();
          status_header(404);
        }
      }
      
      /**
       * Removes CLASSIFICATION & UNIVERS default slug rules
       */
      public function remove_default_rewrite_rules($rules) {
        foreach ($rules as $rule => $rewrite) {
          if (preg_match('/classification\//', $rule) || preg_match('/univers\//', $rule)) {
            unset($rules[$rule]);
          }
        }
        return $rules;
      }
    }
    
    PAPIHookManager::register(new EDPRewriteLT);

    Everything works, but the local-signup page of NSUR.

    I’m using Monkey Rewrite Analyzer to debug and the
    local-signup/?$ -> nsur_signup: true from add_rewrite_rule( 'local-signup/?$', 'index.php?nsur_signup=true', 'top' ); still appears.

    As mentioned previously, the redirection still works on my other subsites for which there are no url rewrites.

    Thanks in advance for your help!

    • This reply was modified 5 years, 9 months ago by em2007.
    • This reply was modified 5 years, 9 months ago by em2007. Reason: corrected mistakes
Viewing 2 replies - 1 through 2 (of 2 total)