Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- * Prints a specific part of the site when it's requested
- *
- * Listens to GET requests containing the "including_template_part" $_GET key and the "load_part"
- * $_GET key. If they're both set and the request comes from the same IP, the existence of a function called
- * "render_{$_GET['load_part']}" is checked($_GET['load_part'] gets all "-" replaced with "_").
- * If that function exists, it's called with or without parameters, depending on the value of $_GET['params'].
- */
- function print_requested_template_part() {
- // Respond only to requests from the same address...
- if ( $_SERVER['REQUEST_METHOD'] == 'GET' && isset( $_GET['including_template_part'] ) && isset( $_GET['load_part'] ) && $_GET['load_part'] != '' && $_SERVER['REMOTE_ADDR'] == $_SERVER['SERVER_ADDR'] ) {
- $part = $_GET['load_part'];
- $func = 'render_' . str_replace( '-', '_', $part ); // if you have declared a function called "render_footer_include", then "?load_part=footer_include"
- if ( function_exists( $func ) ) {
- // Allow for passing parameters to the function
- if ( isset( $_GET['params'] ) ) {
- $params = $_GET['params'];
- $params = ( strpos( $params, ',' ) !== false ) ? explode( ',', $params ) : array( $params );
- call_user_func_array( $func, $params );
- } else {
- call_user_func( $func );
- }
- }
- 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...
- }
- }
- add_action( 'init', 'print_requested_template_part', 1 );
- /**
- * Retrieves the requested part of the main site
- *
- * 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
- *
- * @param $url - the url of the site
- * @param $key - part of the name of the function that will display the content
- * @param $add_qs - any additional query string that will be appended, use "¶ms=param1,param2,param3" to pass "param1", "param2" and "param3"
- * to the loading function
- */
- function get_main_site_part( $url, $key, $add_qs = '' ) {
- // cache the result, so we don't make a request with each page load
- $cache_key = md5( "main_site_{$key}" );
- $cache_lifetime = 300; // 300 seconds = 5 minutes. Increase if necessary
- // just to make sure - try to remove the trailing slash in the $url
- $url = add_query_arg( array( 'including_template_part' => '1', 'load_part' => $key ), $url ) . $add_qs;
- if ( ( $main_site_html = get_transient( $cache_key ) ) === false ) {
- $main_site_html = wp_remote_get($uri);
- if ( is_a( $main_site_html, 'WP_Error' ) ) {
- // You can have some kind of error handling here
- // For instance you can try again
- return;
- }
- // Update the transient
- set_transient( $cache_key, $main_site_html, $cache_lifetime );
- }
- // Either freshly downloaded, or comes from a valid transient
- return $main_site_html;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement