AnsPress routing

4.16K viewsCore
1

Currently AnsPress routing implemented like this:

		if ( ! ap_opt( 'question_permalink_follow' ) ) {

			$new_rules[$question_slug.'/([^/]+)/page/?([0-9]{1,})/?$'] = 'index.php?page_id='.$base_page_id.'&question_name='.$wp_rewrite->preg_index( 1 ).'&paged='.$wp_rewrite->preg_index( 2 );

			$new_rules[$question_slug.'/([^/]+)/?$']  = 'index.php?page_id='.$base_page_id.'&question_name='.$wp_rewrite->preg_index( 1 );

		}

As you all can see, all routing is leading into the “base page” which is the page you have your [anspress] on, which you selected in AnsPress Options as “Base page”.

This raises few severe issues:

  1. All that ANSPRESS_TITLE story that was lasting through this Q&A.
  2. It is not possible to simply edit the question by pressting “Edit page” in wp-admin menu bar (it may be required to change a title, or embed some prohibited otherwise HTML; pressing Edit from the interface does not solve it, to begin with, it doesn’t work!)
  3. It is a source of failure of addons like Add Meta Tags.

This is fundamental. Not even wordpress knows current page is not the “base page” but one of custom posts, “question”.

 

WordPress supports this kind of routing out of the box, it could all just work, if implemented just a bit different way. Here, I look at the sources of Easy Affiliate Links (just a plugin which I know is capable of utilizing custom urls, like http://localhost/customslug/custom-post-name/) as far as I understand, registering custom routes/rewrites should be as easy as

    public function register_post_type()
    {
        $slug = EasyAffiliateLinks::option( 'link_slug', 'recommends' );

        $name = __( 'Affiliate Links', 'easy-affiliate-links' );
        $singular = __( 'Affiliate Link', 'easy-affiliate-links' );
        
        $args = apply_filters( 'eafl_register_post_type',
            array(
                'labels' => array(
                    'name' => $name,
                    'singular_name' => $singular,
                    'add_new' => __( 'Add New', 'easy-affiliate-links' ),
                    'add_new_item' => __( 'Add New', 'easy-affiliate-links' ) . ' ' . $singular,
                    'edit' => __( 'Edit', 'easy-affiliate-links' ),
                    'edit_item' => __( 'Edit', 'easy-affiliate-links' ) . ' ' . $singular,
                    'new_item' => __( 'New', 'easy-affiliate-links' ) . ' ' . $singular,
                    'view' => __( 'View', 'easy-affiliate-links' ),
                    'view_item' => __( 'View', 'easy-affiliate-links' ) . ' ' . $singular,
                    'search_items' => __( 'Search', 'easy-affiliate-links' ) . ' ' . $name,
                    'not_found' => __( 'No', 'easy-affiliate-links' ) . ' ' . $name . ' ' . __( 'found.', 'easy-affiliate-links' ),
                    'not_found_in_trash' => __( 'No', 'easy-affiliate-links' ) . ' ' . $name . ' ' . __( 'found in trash.', 'easy-affiliate-links' ),
                    'parent' => __( 'Parent', 'easy-affiliate-links' ) . ' ' . $singular,
                ),
                'public' => true,
	            'exclude_from_search' => true,
                'menu_position' => 20,
                'supports' => false,
                'taxonomies' => array(),
                'menu_icon' => EasyAffiliateLinks::get()->coreUrl . '/img/icon_16.png',
                'has_archive' => false,
                'rewrite' => array(
                    'slug' => $slug
                )
            )
        );

        register_post_type( EAFL_POST_TYPE, $args );
    }

Notice the  ‘rewrite’ => array(‘slug’ => $slug), and the wordpress function that registers it, register_post_type.

It should be completely possible to display AnsPress questions natively, as they are a custom posts already. It will solve all the current and future issues related to non-standard routing. I don’t see any reason to not use the default wordpress solution here.

Please comment, @AnsPressTeam, @Rahul.

1

Yes, it can be done like that. But we are not using that method and removing WordPress cpt rewrite rules for answer and question.

We are not using WordPress CPT rewrite rules cause we do not want to render question in single.php template, if we do so it will be hard to maintain our custom question template.

You may say that we can use ‘template_redirect’ or ‘template_include’ hook to override single page template for question. Yes it can be used but one issue will arise here if we do so, question page will not adapt from most of theme.

This is the reason we are not using your suggested method. Of course we tried it before (also implemented in 1.9 but have to revert it back).

Cheers.