• Resolved yeasinarafatrafio

    (@yeasinarafatrafio)


    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:

    php

    // 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 'content' are present

    if ( ! 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 error

    return $header_template_id;

    }

    // Return the created post ID

    return rest_ensure_response( $header_template_id );

    }

    // Function to create the size guide

    function create_size_guide( $template_name, $content ) {

    // 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 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 error

    return $post_id;

    }

    // Return the created post ID as JSON

    return array( 'id' => $post_id );

    }

    // Add this code in your plugin's main PHP file or in your functions.php

    add_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

    (@yeasinarafatrafio)

    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)
  • You must be logged in to reply to this topic.