Display a Single Post via admin-ajax.php

Posted on

By Michael Fields

Need to use a simple WordPress loop via Ajax? You can use the following code to do just that using built-in functionality.

/**
 * Show a single post via admin-ajax.php
 *
 * Use a url like this to call this function:
 * /wp-admin/admin-ajax.php?action=mfields_show_post&p=514
 *
 * @since     2010-12-04
 */
function mfields_ajax_show_post() {
	$id = ( isset( $_GET['p'] ) ) ? (int) $_GET['p'] : false;
	query_posts( array( 'p' => $id ) );
	if ( have_posts() ) {
		while( have_posts() ) {
			the_post();
			the_title();
			the_content();
			comments_template( '', true );
		}
	}
	else {
		print '<p>No posts</p>';
	}
	exit;
}
add_action( 'wp_ajax_mfields_show_post', 'mfields_ajax_show_post' );
add_action( 'wp_ajax_nopriv_mfields_show_post', 'mfields_ajax_show_post' );

5 Comments Leave a comment

  1. Ben January 3, 2011 at 10:19 am

    Interesting idea but I am not sure why you would want to use admin-ajax.php instead of the init hook on the site url (ie website.com/?params-here)? Is there some benefit to it?

    Maybe it doesn’t do all the initialization stuff that is required on a normal site load, in which case I guess it would be faster.

  2. Michael Fields January 3, 2011 at 8:04 pm

    I started using Ajax with WordPress in the Administration Panels and this is the way that I learned to create a callback + I always like to hook into the appropriate place. This being said, I rarely hook into init anymore as there always seems to be a more appropriate place.

    Using the ajax hooks allows me to easily see and understand the intentions I had when creating my functions. If they are hooked into wp_ajax, I immediately know that I intended to create an ajax callback for administration purposes. In cases where a function is hooked into both wp_ajax and wp_ajaxnopriv I know that I intended the function to be used as a public resource that both visitors and logged-in users can access.

    I really don’t know if admin-ajax.php loads any less of WordPress than would be included before init.

  3. Ben January 4, 2011 at 6:48 am

    That’s really interesting, I don’t really know about either of these things. I’ve been thinking about it though and I think I will have to stick with the init hook for my themes. The reason is that I create premium themes so I don’t know who will use them or how.

    In turn this means that I don’t know what the wordpress setup is for different users, but I do know that often restricting access to the wp-admin directory is encouraged in security posts (generally through IP/ htpassword restrictions) so relying on the wp-admin directory for something public won’t work.

  4. Michael Fields January 4, 2011 at 7:44 am

    Very good point, but it is far from a deal breaker when it comes to using admin-ajax.php for public resources IMHO. I just found this article via the codex page titled Hardening WordPress which offers examples of how to allow admin-ajax.php to function while requiring authentication for others.

    I might add that before I posted this I asked the following question on Twitter: “Is it bad to use init to process Ajax on the front-end of WordPress?”. Within minutes I received 3 responses, from developers I trust, that admin-ajax.php using the nopriv hook was the way to go. I really don’t know if there’s anything wrong with using init – honestly, I don’t think there is, nor am I trying to dissuade you from using it. I just posted this code for reference and hopefully to help those who might need a quick copy-and-paste to get started on their own projects.

  5. Ben January 4, 2011 at 1:27 pm

    I totally understand where you are coming from with this. I am just trying to expand my knowledge. Like you, I don’t think there’s any one right or wrong way to go about things. I am always learning and through conversing and learning more perhaps I can improve my products a little bit.

    I am going to have a read of those links – thanks for the info! :)

Share your thoughts

*

Fork me on GitHub