Title: How to create a custom block programatically via API
Last modified: July 11, 2024

---

# How to create a custom block programatically via API

 *  Resolved [yeasinarafatrafio](https://wordpress.org/support/users/yeasinarafatrafio/)
 * (@yeasinarafatrafio)
 * [1 year, 11 months ago](https://wordpress.org/support/topic/how-to-create-a-custom-block-programatically-via-api/)
 *     ```wp-block-code
       Hello,I was looking for a way to automate the custom block formation in the plugin. So I wrote a api myself, but it is not working correctly. Although the block shows up in the plugin admin panel, when I hover over it, there is no option for "Edit with Elementor" and also the block is not showing anywhere in the website although referneced.Can you please tell me if you have any api to create a custom block with the plugin.Also this is my custom api endpoint I made (if you can correct it, it would be fantastic),I highly doubt there is some mistake in the post meta I am setting, can you please let me know what post meta I should set to create the custom block:
       ```
   
 *     ```wp-block-code
       php// Callback function for handling the API requestfunction handle_create_size_guide_request( $request ) {$params = $request->get_params();// Check if the required parameters 'template_name' and 'content' are presentif ( ! isset( $params['template_name'], $params['content'] ) || empty( $params['template_name'] ) || empty( $params['content'] ) ) {return new WP_Error( 'missing_params', 'Template name and Template content are required.', array( 'status' => 400 ) );}// Call the function to create the template$header_template_id = create_size_guide( $params['template_name'], $params['content'] );if ( is_wp_error( $header_template_id ) ) {// Handle errorreturn $header_template_id;}// Return the created post IDreturn rest_ensure_response( $header_template_id );}// Function to create the size guidefunction create_size_guide( $template_name, $content ) {// Check if Elementor is active and the class existsif ( ! class_exists( 'Elementor\Plugin' ) ) {return new WP_Error( 'elementor_not_active', 'Elementor is not active.', array( 'status' => 400 ) );}// Define the template content$content = $content; // Replace with your template content// Define the post data$post_data = array('post_title' => sanitize_text_field( $template_name ), // Sanitize and set the post title'post_content' => $content,'post_status' => 'publish','post_type' => 'elementor-hf','meta_input' => array('_elementor_template_type' => 'type_header','ehf_template_type' => 'custom'),);// Insert the post into the database$post_id = wp_insert_post( $post_data );if ( is_wp_error( $post_id ) ) {// Handle errorreturn $post_id;}// Return the created post ID as JSONreturn array( 'id' => $post_id );}// Add this code in your plugin's main PHP file or in your functions.phpadd_action( 'rest_api_init', function () {register_rest_route( 'my-utils', '/create-size-guide/', array('methods' => 'POST','callback' => 'handle_create_size_guide_request','permission_callback' => 'is_user_author_or_higher', // Allow users who can publish posts) );} );
       ```
   

Viewing 1 replies (of 1 total)

 *  Thread Starter [yeasinarafatrafio](https://wordpress.org/support/users/yeasinarafatrafio/)
 * (@yeasinarafatrafio)
 * [1 year, 11 months ago](https://wordpress.org/support/topic/how-to-create-a-custom-block-programatically-via-api/#post-17885403)
 * 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:
 *     ```wp-block-code
       // Callback function for handling the API requestfunction 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 guidefunction 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)

The topic ‘How to create a custom block programatically via API’ is closed to new
replies.

 * ![](https://ps.w.org/header-footer-elementor/assets/icon-256x256.gif?rev=3278750)
 * [Ultimate Addons for Elementor](https://wordpress.org/plugins/header-footer-elementor/)
 * [Frequently Asked Questions](https://wordpress.org/plugins/header-footer-elementor/#faq)
 * [Support Threads](https://wordpress.org/support/plugin/header-footer-elementor/)
 * [Active Topics](https://wordpress.org/support/plugin/header-footer-elementor/active/)
 * [Unresolved Topics](https://wordpress.org/support/plugin/header-footer-elementor/unresolved/)
 * [Reviews](https://wordpress.org/support/plugin/header-footer-elementor/reviews/)

 * 1 reply
 * 1 participant
 * Last reply from: [yeasinarafatrafio](https://wordpress.org/support/users/yeasinarafatrafio/)
 * Last activity: [1 year, 11 months ago](https://wordpress.org/support/topic/how-to-create-a-custom-block-programatically-via-api/#post-17885403)
 * Status: resolved