Advertisement
Guest User

WP the_content and REST requests

a guest
Oct 14th, 2019
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 0.89 KB | None | 0 0
  1. <?php
  2.  
  3.     /**
  4.      * Assign class methods to hooks.
  5.      */
  6.     public static function init() {
  7.         if ( ! defined( 'REST_REQUEST' ) ) {
  8.             add_filter( 'the_content', array( __class__, 'filter_content' ), 999 );
  9.         }
  10.  
  11.         add_filter( 'rest_pre_dispatch', array( __class__, 'rest_check_referer' ), 10, 3 );
  12.     }
  13.  
  14.     public static function rest_check_referer( $result, $server, $request ) {
  15.         if ( null !== $result ) {
  16.             // Core starts with a null value.
  17.             // If it is no longer null, another callback has claimed this request.
  18.             // Up to you how to handle - for this example we will just return early.
  19.             return $result;
  20.         }
  21.  
  22.         $referer = $request->get_header( 'referer' );
  23.  
  24.         if ( 'www.test.com' !== $referer ) {
  25.             add_filter( 'the_content', array( __class__, 'filter_content' ), 999 );
  26.         }
  27.  
  28.         // Otherwise we are good - return original result and let WordPress handle as usual.
  29.         return $result;
  30.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement