Strange use of WordPress add_action()

I expect add_action() to specify a hook name and then a function to call when the hook fires, but I came across the text below in "WordPress Web Application Development, 2nd ed on page 29:

 “We need to define AJAX handler functions using the WordPress add_action function:
add_action( ‘wp_ajax_mark_answer_status’, ‘wpwa_mark_answer_status’ );

There's no existing hook with the name "wp_ajax_mark_answer_status" so could someone explain what's going on?

Thanks for your help.
stevaAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

William NettmannPHP Web DeveloperCommented:
The hook is actually "wp_ajax_<action passed as a request>" - see the Codex for a full explanation.

TLDR version: when you make an AJAX call, you need to add an "action" parameter, e.g. "mark_answer_status" as in
http://www.example.com/wp-admin/admin-ajax.php?action=mark_answer_status

Open in new window

and admin-ajax.php fires the hook "wp_ajax_mark_answer_status" as a result (admin-ajax.php line 80 onwards):
if ( is_user_logged_in() ) {
	/**
	 * Fires authenticated AJAX actions for logged-in users.
	 *
	 * The dynamic portion of the hook name, `$_REQUEST['action']`,
	 * refers to the name of the AJAX action callback being fired.
	 *
	 * @since 2.1.0
	 */
	do_action( 'wp_ajax_' . $_REQUEST['action'] );
} else {
	/**
	 * Fires non-authenticated AJAX actions for logged-out users.
	 *
	 * The dynamic portion of the hook name, `$_REQUEST['action']`,
	 * refers to the name of the AJAX action callback being fired.
	 *
	 * @since 2.8.0
	 */
	do_action( 'wp_ajax_nopriv_' . $_REQUEST['action'] );
}

Open in new window

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
stevaAuthor Commented:
Thank you!
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
WordPress

From novice to tech pro — start learning today.