How is the ask question page displayed?

5.14K viewsGeneral
0

Is there a function that brings up each page? For example the ask question page or the questions list page. I see that there is a register_common_pages function where you can register the page and that this function runs on the “init” action hook, but when do you actually call and load ask.php??

1

You probably mean this

	public function register_common_pages() {
		ap_register_page( 'base', __( 'Questions', 'ap' ), array( $this, 'base_page' ) );
		ap_register_page( ap_opt( 'question_page_slug' ), __( 'Question', 'ap' ), array( $this, 'question_page' ), false );
		ap_register_page( ap_opt( 'ask_page_slug' ), __( 'Ask', 'ap' ), array( $this, 'ask_page' ) );
		ap_register_page( 'edit', __( 'Edit', 'ap' ), array( $this, 'edit_page' ), false );
		ap_register_page( 'search', __( 'Search', 'ap' ), array( $this, 'search_page' ), false );
		ap_register_page( 'activity', __( 'Activity feed', 'ap' ), array( $this, 'activity_page' ) );
	}

The ask_page() method executes/outputs actual ask.php.

 

How/when is it called? Right below the source of ap_register_page (it’s only purpose is fill the $pages field) there is

/**
 * Output current AnsPress page.
 * @since 2.0.0-beta
 */
function ap_page() {
	$pages = anspress()->pages;
	$current_page = ap_current_page();

	if ( is_question() ) {
		$current_page = ap_opt( 'question_page_slug' );
	} elseif ( '' == $current_page && ! is_question() && '' == get_query_var( 'question_name' ) ) {
		$current_page = 'base';
	}

	if ( isset( $pages[ $current_page ]['func'] ) ) {
		call_user_func( $pages[ $current_page ]['func'] );
	} else {
		global $wp_query;
		$wp_query->set_404();
		status_header( 404 );
		include ap_get_theme_location( 'not-found.php' );
	}
}

How does it get the $current_page slug?

get_query_var( ‘ap_page’ ); in ap_current_page()
and
$new_rules[$slug. ‘ask/([^/]+)/?’] = ‘index.php?page_id=’.$base_page_id.’&ap_page=ask&parent=’.$wp_rewrite->preg_index( 1 ); in rewrites()

(on a first glance it looks like it shouldn’t work with changed “ask page” slug, as it’s hardcoded in rewrites, but that’s none of my business currently ^^)

 

And ap_page() is called in anspress_sc(), which is //Control the output of [anspress] shortcode

add_shortcode( ‘anspress’, array( AnsPress_BasePage_Shortcode::get_instance(), ‘anspress_sc’ ) );

The Ctrl+F’s.

Ugh sorry @DimaStefantsov…I’m new to web dev and I’m trying to learn by modifying an existing plugin

Was my explanation understandable, or is there anything else that needs clarification maybe?

Yea, I guess I’m still a little confused as to the purpose of ap_page(). For the ask question page, it seems to set $current_page to “base”, but why is that important? To me, the rookie, it looks all that would be needed is ask_page() to include the hugely necessary ask.php, so why is this other function needed?

@DimaStefantsov not sure if I need to include your name in the comment or not, but doing so just in case.

Because of this https://anspress.io/questions/question/anspress-routing/

Currently, everything is a base page. The code needs a way to somehow know what should be displayed on that base page.

@DimaStefantsov Ah ok. So if I wanted to add a page to Anspress, what would I need to do? I’ve designed the page to use an iFrame, and now I just need it to be displayed after submitting a question. so far, I’ve added my own ap_register_page() and placed it in register_common_pages(), which runs on the ‘init’ hook. However, I don’t quite understand where to go from here. I like what the author did with process_form() where depending on what $action is it calls the correct function. I’m hoping it’s the same case with which page to display.