Reputation: 15
I have 2 wordpress websites on different sub domains. The first site1.mydomain.com and site2.mydomain.com
What I would like to do is have the header and footer from site1 shown on site2
I tried this plugin: https://wordpress.org/plugins/external-header-footer/ but I don't think it works.
Does anyone know of any other solutions to this?
Thanks in advance.
Upvotes: 1
Views: 1275
Reputation: 3444
I accomplished this with wp_remote_get()
, Transients, and DOMDocument.
An example:
function get_site_html() {
$transient = get_transient('site_html');
if(false === $transient) {
$site_url = 'https://YOUR-SITE.com';
$response = wp_remote_get(esc_url_raw($site_url));
$response_code = wp_remote_retrieve_response_code($response);
$response_body = wp_remote_retrieve_body($response);
$response_error = null;
if(is_wp_error($response)) {
$response_error = $response;
} elseif(200 !== $response_code) {
$response_error = new WP_Error('html-error', sprintf(__('Invalid response code while retrieving HTML (%d)'), $response_code));
} elseif(empty($response_body)) {
$response_error = new WP_Error('html-response', isset($response_body['error']) ? $response_body['error'] : __('Unknown error retrieving HTML.'));
}
if(is_wp_error($response_error)) {
return $response_error->get_error_message();
} else {
set_transient('site_html', $response_body, DAY_IN_SECONDS);
}
}
$transient = get_transient('site_html');
return $transient;
}
Once you have the full site's HTML saved in a transient you can then get/add/remove specific elements with DOMDocument.
Upvotes: 1