Presets API: Add A Preset To A WordPress Theme

Add import-ready layouts to your WordPress themes that your users can deploy.
← Back to Developers

The Presets API is a functionality that comes with the base Forge plugin. In a nutshell, it allows WordPress themes to create a simple interface, which users can then use to seamlessly import content.

The purpose of the Presets API is to provide an easy way to import content from a new WordPress theme, without having to use the traditional WordPress importer. It has the added advantage of not needing to use an XML demo file.

Adding A Preset To Your Theme

Setting up presets for a WordPress theme is fairly straight forward. All you need to do is add the following snippet to the WordPress theme code, and then customize it to your needs:

if(function_exists('forge_presets')){
    add_filter('init', 'mytheme_forge_presets');
    function mytheme_forge_presets(){
        forge_presets()->add_preset('PRESETNAME', array(
        'title' => __('Title Of The Preset', 'mytheme'),
        'description' => __('Description of the preset layout.', 'mytheme'),
        'elements' => 'PASTE EXPORT CODE HERE',
        'metadata' => array(
            'page_header' => 'normal',
            'page_title' => 'none',
            'page_footer' => 'normal',
        )));

    }
}

Keep in mind the following details:

  • Change mytheme to your theme’s textdomain.
  • Change PRESETNAME to a unique identifier for that preset. There is no need to add a prefix or textdomain, so something like ‘about’ or ‘homepage’ will work.
  • The code uses the forge_presets()->add_preset() function to add a single preset. You can add as many as you like by using the same function any number of times.

To set up the content of the preset, change the line that says ‘PASTE CODE HERE’ and add the exported content from a page of your liking. Simply use Forge to build your layout, and then use the Export function to get the code. Then paste all that code into the string.

Additionally, you can set custom post metadata in the ‘metadata‘ argument. This will help you set special data such as layouts or theme-specific options during the preset installation process, and make the overall experience much smoother. Simply add any number of key-value pairs to automatically set the post metadata to those values.

If you want to change the page template field, you can use ‘_wp_page_template‘ as the key and the filename of the template as the value.

 

Share on FacebookShare on Google+Tweet about this on TwitterShare on LinkedInShare on TumblrBuffer this pageEmail this to someone

Error: Please enter a valid email address

Error: Invalid email

Error: Please enter your first name

Error: Please enter your last name

Error: Please enter a username

Error: Please enter a password

Error: Please confirm your password

Error: Password and password confirmation do not match