Accessing $post after PHP is executed

I believe the AngularJS plugin has solved this problem by filtering the content with the_content1 filter. They are using post meta to determine when the content should be loaded client side, but you can write your own function to determine when this is appropriate. This way the content is filtered instead of creating a new $post object.

Here is an example from the plugin WordPress › AngularJS for WordPress « WordPress Plugins

function angularFilter( $content ) {
        global $post;

        $meta = get_post_meta($post->ID, 'angularjsLoad', true);
        $meta = intval($meta);

        if($meta){
                $content="<ng-post-content id="".$post->ID.'"></ng-post-content>';
        }

        return $content;
}
add_filter('the_content', 'angularFilter', 10, 3);

I imagine you will need to filter the content so that it is similar to this line, except using the appropriate directive for your project $content="<ng-post-content id="".$post->ID.'"></ng-post-content>';

Also, you should probably be calling your script with wp_enqueue_script2.

This is the recommended method of linking JavaScript to a WordPress
generated page.


If you are certain that you must make an AJAX request to PHP after the page has loaded, you will really need to study AJAX in Plugins « WordPress Codex. Note the variations in the wp_ajax action depending on whether the user is logged in (authenticated) and the ajaxurl for the viewer-facing side.

To summarize this process:

  • You must determine how to call your script. wp_enqueue_script() is recommended.
  • Your script needs to define the action, data to be passed (post ID, for example) and the AJAX url to pass it to.
  • In functions.php, add an action hook that is triggered by your AJAX request. You can do this with wp_ajax_(action)3. If the action in your script is called “foo”, this would look like add_action( 'wp_ajax_foo', foo_callback );
  • In functions.php, write your callback function “foo_callback”. Within this function you can include whatever PHP functions you need. For example, if you just need to display content from a single post and you got the post ID from your script, you could use get_post_field( 'post_content', $post_id ) to display it.

This selected answer by whiteletters and blankspaces is a good example with some explanation of how this process works: https://stackoverflow.com/a/15315024/2407742

Hopefully that points you in the right direction so that you can search or ask a more specific question if you are stuck.