PPWP Pro – Check if the content is password protected

Use the WordPress default API with the post endpoint to check if a post is password protected.

https://pda.com/wp-json/wp/v2/posts2/123

For the API to return the password form for protected content, add the following function into your (child) theme functions.php

if ( ! function_exists( 'ppwp_rest_prepare_post' ) ) {
	add_filter( 'rest_prepare_post', 'ppwp_rest_prepare_post', 10, 3 );
	function ppwp_rest_prepare_post( $response, $post, $request ) {
		if ( ! function_exists( 'ppw_core_render_login_form' ) ) {
			return $response;
		}
		if ( ! isset( $response->data['id'] ) || ! isset( $response->data['content'] ) ) {
			return $response;
		}
		$is_protected = post_password_required( $response->data['id'] );
		if ( $is_protected ) {
			$response->data['content']['protected'] = true;
			$response->data['content']['rendered']  = ppw_core_render_login_form();
		}
		return $response;
	}
}

The API returns the password form if the content is not protected

Otherwise, it will return the content as usual.