yeasinarafatrafio
Forum Replies Created
Viewing 1 replies (of 1 total)
-
Hello fixed it. Seems like i was not setting the post meta data correctly. You need to add a meta of “_elementor_data”
This is the updated endpoint code snippe:
// Callback function for handling the API request
function handle_create_size_guide_request( $request ) {
$params = $request->get_params();
// Check if the required parameters 'template_name' and 'elementor_data' are present
if ( ! isset( $params['template_name'], $params['elementor_data'] ) || empty( $params['template_name'] ) || empty( $params['elementor_data'] ) ) {
return new WP_Error( 'missing_params', 'Template name and Template elementor_data are required.', array( 'status' => 400 ) );
}
// Call the function to create the template
$hfe_block_id = create_size_guide( $params['template_name'], $params['elementor_data'] );
if ( is_wp_error( $hfe_block_id ) ) {
// Handle error
return $hfe_block_id;
}
// Return the created post ID
return rest_ensure_response( $hfe_block_id );
}
// Function to create the size guide
function create_size_guide( $template_name, $elementor_data ) {
// Check if Elementor is active and the class exists
if ( ! class_exists( 'Elementor\Plugin' ) ) {
return new WP_Error( 'elementor_not_active', 'Elementor is not active.', array( 'status' => 400 ) );
}
// Define the post data
$post_data = array(
'post_title' => sanitize_text_field( $template_name ), // Sanitize and set the post title
'post_status' => 'publish',
'post_type' => 'elementor-hf',
'meta_input' => array(
'ehf_template_type' => 'custom',
'_wp_page_template' => 'default',
'_elementor_edit_mode' => 'builder',
'_elementor_template_type' => 'wp-post',
'_elementor_data' => $elementor_data,
),
);
// Insert the post into the database
$post_id = wp_insert_post( $post_data );
if ( is_wp_error( $post_id ) ) {
// Handle error
return $post_id;
}
// Return the created post ID as JSON
return array( 'id' => $post_id );
}
Viewing 1 replies (of 1 total)