Class Elements

Extending Elements class is done by adding the following filter and actions:

class Elements {
    public function __construct() {
        add_filter( 'zionbuilder/elements/categories', [ $this, 'add_elements_categories' ] );
        add_action( 'zionbuilder/elements_manager/register_elements', [ $this, 'register_elements' ] );
        add_action( 'zionbuilder/editor/before_scripts', [ $this, 'enqueue_scripts' ] );
    }
}

Registering a new element category

Registering a new category is done by applying the filter

'zionbuilder/elements/categories'

A category is an array with the following structure:

public function add_elements_categories( $categories ) {
   return [
     [
       'id'       => 'category_id',
       'name'     => __( 'My Category', 'text-domain' ),
       'priority' => 10,
     ],
   ]
}

Registering elements

The final function for registering the new elements should look as the following:

public $elements_manager = null;
public function register_elements( $elements_manager ) {
   $new_elements = [
     'MyElement',
   ]
    foreach ( $new_elements as $element_name ) {
          $file_path = Utils::get_file_path( 'includes/Elements/' . trailingslashit( $element_name ) . $element_name . '.php' );
 if ( is_file( $file_path ) ) {
                include $file_path;

                // Normalize class name
                $class_name = str_replace( '-', '_', $element_name );
                $class_name = __NAMESPACE__ . '\\Elements\\' . $class_name;
                $elements_manager->register_element( new $class_name() );
            }

    }
}

Was this article helpful ?

25 people consider this article helpful
Thank you! We will improve this article.

Didn’t find what you’re looking for? Please let us know

Submit a ticket