Created: 20 Dec 2024
Modified: 10 Jan 2025
Elements:
<?php
// Get the domain URL from the ACF field
$domain = get_field('template_url');
if ($domain) {
// Fetch the HTML content of the domain
$html = file_get_contents($domain);
if ($html !== false) {
// Use DOMDocument to parse the HTML
$dom = new DOMDocument();
// Suppress warnings from malformed HTML
libxml_use_internal_errors(true);
$dom->loadHTML($html);
libxml_clear_errors();
// Find the <main> tag
$main = $dom->getElementsByTagName('main');
if ($main->length > 0) {
// Get the first <main> tag
$mainTag = $main->item(0);
// Extract the inner content of the <main> tag
$innerHTML = '';
foreach ($mainTag->childNodes as $child) {
$innerHTML .= $dom->saveHTML($child);
}
// Neatly format the HTML (optional)
$formattedHTML = tidy_html($innerHTML);
// Output the inner HTML (formatted)
echo '<div id="main-content" class
"none">' . $formattedHTML . '</div>';
} else {
echo '<p>No <main> tag found on the domain.</p>';
}
} else {
echo '<p>Failed to fetch the domain HTML.</p>';
}
} else {
echo '<p>No domain specified.</p>';
}
/**
* Format HTML using Tidy
*
* @param string $html
* @return string
*/
function tidy_html($html) {
if (extension_loaded('tidy')) {
$tidy = new tidy();
$config = [
'indent' => true,
'output-html' => true,
'wrap' => 200,
'indent-spaces' => 4,
];
return $tidy->repairString($html, $config, 'utf8');
}
// Return raw HTML if Tidy is not available
return $html;
}
?>
<!-- Button to copy content -->
<button id="copy-content">Copy Content</button>
document.getElementById('copy-content').addEventListener('click', function() {
// Get the main content
var mainContent = document.getElementById('main-content').innerHTML;
// Create a temporary textarea element to hold the content
var textarea = document.createElement('textarea');
textarea.value = mainContent;
// Append it to the document, copy the content, then remove it
document.body.appendChild(textarea);
textarea.select();
document.execCommand('copy');
document.body.removeChild(textarea);
// Provide feedback to the user
alert('Main content copied to clipboard!');
});