1

My Question, is it possible to restrict the content from a post (shwo e.g. teaser/excerpt only) but still let access the content fully by the REST Api interface?

My scenario is that i would like to force my page viewer to my app, where the content is showing via rest api fully.

I tried couple content restriction plugins like content control, but all of the also restrict the access via REST Api.

Thanks for any hints on this.

2
  • You can check to see if the constant REST_REQUEST is defined (and true) to determine if you're in a REST request, and then not apply your content lockdown filter(s) there. See this answer (and note that REST_REQUEST will be defined after the wp hook has fired, but it's not available yet on the init hook).
    – Pat J
    Sep 6, 2023 at 13:50
  • Thanks for the reply. It seems like it goes in the right direction. Do you have an example hook for this? I would prefer to use an exsiting content restriction plugin and exclude the function only for the WP-Request. Is it possible in this way?
    – RestAPPDev
    Sep 6, 2023 at 14:49

2 Answers 2

3

You can use the REST_REQUEST constant to detect if this is a REST request, and if it's not, apply your filter(s).

Example:

add_filter( 'the_content', 'wpse418531_use_excerpt' );
/**
 * Replaces the content with the excerpt, unless this is a REST request.
 *
 * @param  string $content The post content.
 * @return string          The filtered post content.
 */
function wpse418531_use_excerpt( $content ) {
    if ( is_admin() ) {
        // If this is an admin page, returns the content unchanged.
        return $content;
    }
    if ( defined( REST_REQUEST ) && true === REST_REQUEST ) {
        // If this is a REST request, returns the content unchanged.
        return $content;
    }
    $content = get_the_excerpt();

    // If you want to add a note about your app, you can do it here.
    // If not, delete these lines.
    $content .= '<p>See more on <a href="https://example.com/download/my/app/">my app</a>.</p>';

    return $content;
}

Note: This code is untested. Don't use it on a production site till you've ensured it works properly. It's also meant as a starting point for you, not a finished product.

References

2

I am not sure why, but the get_the_excerpt() function not work for me. The if-clause for checking if it's a rest api request or not also not work as needed, i add a new function for that check. I take some adjustment for the excerpt part and it is working for me know as i needed. I limit the content to 55 words and for the rest api it provides the full content.

Pat J thanks for your help and pushing me to the right direction. Here is my working code:

add_filter( 'the_content', 'wpse418531_use_excerpt' );
function is_rest() {
    return ( defined( 'REST_REQUEST' ) && REST_REQUEST );
}
function wpse418531_use_excerpt( $content ) {
    if ( is_admin() ) {
        // If this is an admin page, returns the content unchanged.
        return $content;
    }
    if ( is_rest() ) {
        // If this is a REST request, returns the content unchanged.;
        return $content;
    }
    $content = wp_trim_words(get_the_content(), 55);
    $content .= 'Continue via App';
    $content .= '<p><a href="https://example.org">AppStore</a> | <a href="https://example.org">PlayStore</a></p>';

    return $content;
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.