Advertisement
MoonWatch

WordPress external content loading

Sep 13th, 2013
127
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.89 KB | None | 0 0
  1. /**
  2.  * Prints a specific part of the site when it's requested
  3.  *
  4.  * Listens to GET requests containing the "including_template_part" $_GET key and the "load_part"
  5.  * $_GET key. If they're both set and the request comes from the same IP, the existence of a function called
  6.  * "render_{$_GET['load_part']}" is checked($_GET['load_part'] gets all "-" replaced with "_").
  7.  * If that function exists, it's called with or without parameters, depending on the value of $_GET['params'].
  8.  */
  9. function print_requested_template_part() {
  10.     // Respond only to requests from the same address...
  11.     if ( $_SERVER['REQUEST_METHOD'] == 'GET' && isset( $_GET['including_template_part'] ) && isset( $_GET['load_part'] ) && $_GET['load_part'] != '' && $_SERVER['REMOTE_ADDR'] == $_SERVER['SERVER_ADDR'] ) {
  12.         $part = $_GET['load_part'];
  13.         $func = 'render_' . str_replace( '-', '_', $part ); // if you have declared a function called "render_footer_include", then "?load_part=footer_include"
  14.         if ( function_exists( $func ) ) {
  15.             // Allow for passing parameters to the function
  16.             if ( isset( $_GET['params'] ) ) {
  17.                 $params = $_GET['params'];
  18.                 $params = ( strpos( $params, ',' ) !== false ) ? explode( ',', $params ) : array( $params );
  19.                 call_user_func_array( $func, $params );
  20.             } else {
  21.                 call_user_func( $func );
  22.             }
  23.         }
  24.         exit; // if we don't exit here, a whole page will be printed => bad! it's better to have empty footer than a footer with the whole main site...
  25.     }
  26. }
  27. add_action( 'init', 'print_requested_template_part', 1 );
  28.  
  29. /**
  30.  * Retrieves the requested part of the main site
  31.  *
  32.  * Okay, well basically can be from any site, depending on the $url param, as long as it has the proper function that will display the requested content
  33.  *
  34.  * @param $url - the url of the site
  35.  * @param $key - part of the name of the function that will display the content
  36.  * @param $add_qs - any additional query string that will be appended, use "&params=param1,param2,param3" to pass "param1", "param2" and "param3"
  37.  * to the loading function
  38.  */
  39. function get_main_site_part( $url, $key, $add_qs = '' ) {
  40.    
  41.     // cache the result, so we don't make a request with each page load
  42.     $cache_key = md5( "main_site_{$key}" );
  43.     $cache_lifetime = 300; // 300 seconds = 5 minutes. Increase if necessary
  44.    
  45.     // just to make sure - try to remove the trailing slash in the $url
  46.     $url = add_query_arg( array( 'including_template_part' => '1', 'load_part' => $key ), $url ) . $add_qs;
  47.    
  48.     if ( ( $main_site_html = get_transient( $cache_key ) ) === false ) {
  49.         $main_site_html = wp_remote_get($uri);
  50.         if ( is_a( $main_site_html, 'WP_Error' ) ) {
  51.             // You can have some kind of error handling here
  52.             // For instance you can try again
  53.             return;
  54.         }
  55.         // Update the transient
  56.         set_transient( $cache_key, $main_site_html, $cache_lifetime );
  57.     }
  58.    
  59.     // Either freshly downloaded, or comes from a valid transient
  60.     return $main_site_html;
  61. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement