Support » Developing with WordPress » Preventing the_content filter to run for specific REST requests

  • Resolved Allyson Souza

    (@allysonsouza)


    Hi, I want to prevent my the_content filter to run for specific REST requests, from a specific origin. The case is, I’m adding some ads using the_content filter, but for a third party app who consumes our content I don’t want to inject those ads in the content. Any ideas how to accomplish it?

    Thanks!

Viewing 5 replies - 1 through 5 (of 5 total)
  • Moderator bcworkz

    (@bcworkz)

    Conditionally add your filter only if certain values in the $_SERVER array do not indicate the third party app made the request. Possible things to check for are IP address, referer URL, or user agent.

    But, in which hook I can make that check? I’m trying something like that now:

    if ( ! defined( 'REST_REQUEST' ) ) {
        add_filter( 'the_content', array( __class__, 'filter_content' ), 999 );
    }
    add_filter( 'rest_pre_dispatch', array( __class__, 'rest_check_referer' ), 10, 3 );

    If REST_REQUEST is not defined, I add my filter to the_content, and then I’m trying to check the referer in rest_pre_dispatch, and if it’s from the specific referer, I try to add my calback to the_content, but I guess it’s too late to fire the filter.

    Moderator bcworkz

    (@bcworkz)

    Pre dispatch ought to be early enough to add content, but your code logic is faulty. REST_REQUEST is always defined when this filter fires, it is never NOT defined if and when it fires. Your code says add content filters if REST_REQUEST is not defined, so your content filter will never be added.

    It will also never be added for normal front end requests. Isn’t your intent to add content for all non-admin requests, front end and REST, except those from one particular app? There’s no action common to both front end and REST where the decision to define REST_REQUEST has already occurred, so you cannot use one action hook to decide to add content. You should hook a common action like “init”, then decide whether to add content or not right in “the_content” callback, not in the “init” callback.

    I assumed in my last reply that the no ads app would not be an authenticated user. If it is an authenticated user, that is how you decide not to add content — by the current user. Otherwise, as I stated, use data from $_SERVER.

    Thaks, you have clarified my question, I forget I could simply use $_SERVER to check the referer and determine if I have to put something or not into the_content callback.

    Solved:

    add_filter( 'the_content', 'filter_content', 999 );
    function filter_content( $content ) {
        if ( 'www.test.com' !== $_SERVER['HTTP_REFERER'] ) {
            return $content;
        } else {
            // Insert my ads
        }
        
        return $content;
    }
    Moderator bcworkz

    (@bcworkz)

    Awesome! Well done.

Viewing 5 replies - 1 through 5 (of 5 total)
  • You must be logged in to reply to this topic.