I have Created A Single Page Template in this I have added a feature image widget, Post title widget, social media widget, and post content image and I applied that template to all post. now I have the REST API in which I am using [elementor-template id=”‘.$post_id.'”] shortcode to get the particular blog page as HTML. now I can get post content but not able to get other widgets like feature image widget, Post title widget, and social media widget. Please if you guys can help me to solve this. below is the code
<?php
function get_blog_post_html_test( $request ) {
$permalink = $request->get_param( 'slug' );
$args = [
'post_type' => 'post',
'posts_per_page' => 1,
'post_name__in' => [$permalink],
'fields' => 'ids'
];
$posts = get_posts( $args );
$post_id = $posts[0];
$post = get_post($post_id);
if (!$post || $post->post_type !== 'post') {
wp_send_json_error( 'Resource not found', 404 );
}
// Make sure Elementor is loaded
if (class_exists('ElementorPlugin')) {
// Get the Elementor instance
$elementor = ElementorPlugin::instance();
// Get the post content (main content)
$content = get_the_content($post_id);
$header_content = get_raw_header($post_id);
$footer_content = get_raw_footer($post_id);
// Create a DOMDocument
$dom = new DOMDocument;
$dom->loadHTML('<?xml encoding="UTF-8">' .$content);
// Create a DOMXPath
$xpath = new DOMXPath($dom);
// Find elements with class 'elementor-widget-video'
$videoElements = $xpath->query("//*[contains(concat(' ', normalize-space(@class), ' '), 'elementor-widget-video')]");
// Iterate through the video elements
foreach ($videoElements as $videoElement) {
// Extract the 'data-settings' attribute value
$dataSettingsValue = $videoElement->getAttribute('data-settings');
// Decode the JSON string into a PHP object
$dataObject = json_decode($dataSettingsValue);
if ('youtube' === $dataObject->video_type) {
$video_url = $dataObject->youtube_url;
// Parse the URL to get the query string
$urlParts = parse_url($video_url);
parse_str($urlParts['query'], $queryParameters);
// Check if 'v' parameter exists
if (isset($queryParameters['v'])) {
$videoId = $queryParameters['v'];
$youtubeUrl = "https://www.youtube.com/embed/{$videoId}";
// Assuming 'iframeCode' contains the iframe HTML code you want to add
$iframeCode = "<iframe class='elementor-video' frameborder='0' allowfullscreen='' allow='accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share' width='640' height='360' src='{$youtubeUrl}?controls=1&rel=0&playsinline=0'></iframe>";
// Find the parent div with class 'elementor-video'
$parentDiv = $xpath->query("//*[contains(concat(' ', normalize-space(@class), ' '), 'elementor-video')]", $videoElement)->item(0);
// Create a DOMDocumentFragment for the iframe code
$fragment = $dom->createDocumentFragment();
$fragment->appendXML($iframeCode);
// Import the iframe fragment to the main document
$importedFragment = $dom->importNode($fragment, true);
// Add the iframe under the div
$parentDiv->appendChild($importedFragment);
}
}
}
// Updated HTML
$updatedHtml = $dom->saveHTML();
$src_info = 'blog-details elementor-kit-10 elementor-page elementor-page-'.$post_id;
// Create an array with content
$response = array(
//'seo_information' => $seoData,
'classes' => $src_info,
//'content_html' => json_decode(get_post_meta( $post_id, '_elementor_data', true )),
'content_html' => do_shortcode('[elementor-template id="'.$post_id.'"]')
//'content_html' => get_full_elementor_content_with_assets($post_id)
//'footer_html' => $footer_content,
);
echo json_encode(array('statusCode'=>200,'message'=>'Blog Detail','result'=>$response));
}
//return new WP_REST_Response( $html_content, 200 );
}