Get wordpress page content via ajax request codes are very simple and you always need something like this to make some professional steps towards yor layout.

  1.  For admin backend

put below code in functions.php which is creating an ajax action ‘my_action’ which can be called from script which is  given right below this code.

Add below script along with functions.php or any admin js associated with it. You should also bind this with an admin page and user level.

 

But I needed to alert some content of a page in lightbox like term n conditions and  privacy policy.

2. For Front end user who an’t logged in.

First method wasn’t supporting me as It could only works with admin-logged in users . I tried some without logged in methods but that also let you access to the wp-admin which is not acceptable in terms of security reasons.

So create a separate file in your theme like ajax.php and put below code in it. It will allow access to total control over what kind of data you are giving to user.

First require file of wp-config will allow you to access some front end wordpress functions which are secures.

Here i am letting input integer type and fetching data according to it. Front end functions are already showing active posts so only published things can be fetched.

If you want to build a custom query you can initiate

global $wpdb

will allow access to custom queries for your ajax.php request.