There is a time in every WordPress developer’s life when they must, on occasion, wear a suit and tie. They also may have to create Elementor posts or pages programmatically. Both are unfortunate situations, and while I can offer solutions to both, today we are going to focus just on the programming bit and leave your decision between tweed or corduroy up to you.
Pro-Tip: Elbow patches on a blazer will improve your TTIS (time to imposter syndrome) by at least 15 minutes, as the crow flies, during a corporate sales meeting or interview.
Why Would We Ever Need To Programmatically Make Elementor pages!? Doesn’t this defeat the purpose of using a page builder?!
No. It’s actually kind of awesome.
At Solid Digital we recently had a situation where a client wanted Elementor based pages automatically generated when a WooCommerce product was created. Imagine you need 3 custom Elementor pages created for each new instance of a product. These pages take in custom data supplied by different “vendors” for adding their own look and feel for how the products are displayed. Think custom fonts for headers, custom colors for text and buttons, etc. The client also wanted these pages to remain editable with Elementor.
Our designers were tasked with creating award-winning and crowd-pleasing designs for 3 different page templates that we saved as templates using the “Save as Template” functionality. These page templates were built from smaller section templates to create breathtaking user experiences with fully optimized SEO content. To read more about the different templates within Elementor, take a look at our deep dive into Elementor Templates.
We now have 3 Elementor page layout templates that we will use to create dynamically generated pages. Let’s grab a template to make a page. Elementor templates are stored with a post type of “elementor_library.” We can use this information and the template’s name to retrieve it:
'elementor_library', 'name' => 'my_template_name', 'posts_per_page' => 1 ]); // No need to set up The Loop - we just want one post $template = $query->found_posts ? $query->posts : false;
We now have the template we want to use in-memory; next, we need to create our page by copying the content of the template into a new page.
'page', 'post_title' => 'My Dynamic Page Title', 'post_name' => 'My Dynamic Page Title', // Copy the content from the template 'post_content' => $template->post_content, 'post_status' => 'publish', 'post_author' => get_current_user_id(), );
If we were to visit the new page at this point, it wouldn’t look like an Elementor page.
Once we have a post/page ID, we can set the metadata (this is where most of the magic happens) to flag this page as being managed by Elementor. There are multiple key metadata values that have to be set correctly either as hardcoded values or as copies from the values of the original template.
ID, '_elementor_page_settings', true); $data = json_decode(get_post_meta($template->ID, '_elementor_data', true), true); $assets = get_post_meta($template->ID, '_elementor_page_assets', true); $controls = get_post_meta($template->ID, '_elementor_controls_usage', true); // Copy the Elementor setting, data, assets, and controls into // the new page update_post_meta($pageId, '_elementor_page_settings', $settings); update_post_meta($pageId, '_elementor_data', $data); update_post_meta($pageId, '_elementor_page_assets', $assets); update_post_meta($pageId, '_elementor_controls_usage', $controls);
The most difficult part is making sure to update all the required meta fields.
The new page will now look the same as the template, and when visiting the page from the front end, you’ll be able to immediately edit it with Elementor.
Remember, it’s not about how good the page builder makes the page look, it’s about how good you look when you don’t have to use the page builder at all to create new pages.