The Sample Content Extension allows you add multiple demo content or different sample settings to your themes and plugins. It allows your users to activate any of your sample content and change the look of their site. Each sample content is a collection of different option values that get saved upon activation.

You’ll need Titan Framework v1.11 for this extension to work.

extensionscreenshot-5Themes and plugins can benefit from the Sample Content Extension. Themes can give users sample designs and various theme setting combinations. Plugins can provide different plugin settings for different scenarios for users. Sample content are presented in its own admin page, along with a grid of screenshots, names, and descriptions.


Key Features

  • Use in WordPress themes or plugins,
  • Reset all Titan Framework admin and customizer options,
  • Set any Titan Framework admin and customizer options upon activation,
  • Create a new frontpage with your specified content,
  • Create new pages with your specified content,
  • Create menus containing links to pages or custom URL links,
  • Assign menus to different theme menu-locations, and
  • Call a PHP function prior to and after sample content activation.

How to Use the Sample Content Extension

Upon purchase, you will get a zip file containing the PHP script for the Sample Content Extension. Unzip this, then include the PHP file into your project. Require the script in your project:

require_once( 'path/to/sample-content.php' );​

After this, you will have access to 2 new items in Titan Framework:

  1. Sample Content Page – A container similar to the admin page where you place all your sample content it.
  2. Sample Content – Sample content that users can activate.

Similar to how you can add options into admin pages, now you can add sample contents, but only inside sample content pages.

1. How to Create a Sample Content Page

Creating a sample content page is done by calling the function createSampleContentPage.

You use the createSampleContentPage the same way you use createAdminPage. The sample content page is based on the admin page object, so all options available in admin pages are also available in a sample content page.

How to create a sample content page as a main admin menu item:

add_action( 'tf_create_options', 'my_theme_settings' );
function my_theme_settings() {
$titan = TitanFramework::getInstance( 'my-theme' );
// Create the admin sample page.
$sampleContentPage = $titan->createSampleContentPage( array(
'name' => 'Sample Content',
) );
// Then create sample content...
}​

The above will create the sample content page as a main menu item:

sample-contentcropOr, it can also be created as an admin sub menu item:

add_action( 'tf_create_options', 'my_theme_settings' );
function my_theme_settings() {
$titan = TitanFramework::getInstance( 'my-theme' );
// Our main admin menu page/item.
$adminPanel = $titan->createAdminPanel( array(
'name' => 'My Theme Settings',
) );
// Create the admin sample content page.
$sampleContentPage = $adminPanel->createSampleContentPage( array(
'name' => 'Demo Content',
) );
// Then create sample content...
}​

The above will create the sample content page as a submain menu item:

2. How to Create Sample Content

Creating sample content can be done by using the function createSampleContent.

On each sample content page you create, you can add multiple sample contents in them. This is just similar to adding options into a container.

Each sample content contains the different Titan Framework option IDs and values which will be saved upon activation. Here’s a basic example, let’s say I have 2 existing options with the IDs my_text and my_color, and I want to change those values for some demo content:

// Creates a sample content.
$sampleContentPage->createSampleContent( array(
'name' => 'My Demo Content',
'options' => array(
'my_text' => 'It will now have this value',
'my_color' => '#ffa500',
),
) );​

The above sample content will create this:

demo-contentcrop-1When a user clicks on the Activate button, all the theme options will be reset to their defaults, and then the options with the IDs my_text and my_color will be changed into It will now have this value and #ffa500 respectively.

Here’s what the options look like before activation:

demo-contentcrop-2Here’s what the options look like after activation:

demo-contentcrop-3You can change any or all admin options or customizer options you have in your project.

There are more things you can do with this function such as create a new sample front page or assign menus; refer to the parameters and examples below to learn how to do those.


Settings and Parameters

Parameters of createSampleContentPage

createSampleContentPage() inherits from the Admin page object so it also has the same parameters:

Parameter Type Description
name string The name of the admin page. This is also the name shown in the admin menu.
title string (Optional) The title displayed on the top of this admin page. Defaults to the name parameter.
desc string (Optional) A description displayed just below the title of this admin page.
id string (Optional) A unique slug for this admin page. Defaults to a generated slug from the name parameter.
parent string (Optional) If given, the admin page will become a submenu of this slug ID. Useful if you want your menu to be inside a core WordPress menu. The list of menu slug IDs can be found in the codex. Defaults to ''
capability string (Optional) The required capability of the user for this admin page. Defaults to manage_options
icon string (Optional) The menu icon for the admin menu for this page. This can either be a URL to the icon image file, or the name of a WordPress DashIcon. Defaults to dashicons-admin-generic
position int (Optional) The position where the menu should appear. This value corresponds to the core menu positions found in the codex. Defaults to null
success_message string (Optional) The message displayed after activating of Sample Content. Defaults to Successfully saved sample content.

Parameters of createSampleContent

Parameter Type Description
name string (Optional) The title of the sample content.
image URL string (Optional) A URL to an image displayed as a preview to the sample content. You can use the WP functions: get_template_directory_uri and plugins_url to get the URL of images included in your project.
desc string (Optional) A description displayed just below the title of sample content.
options array (Optional) An array containing the IDs of options as keys and the values to be saved upon activation.
frontpage_content string (Optional) If specified, a new page will be created with the title “Sample Frontpage” with the value here as the content, it will be assigned as the frontpage. This may contain HTML tags. Defaults to blank.
pages array (Optional) An array containing the title of pages as keys and content as the values. If a page already exists with the given title, the page won’t be created. Defaults to blank.
menu array (Optional) A multi-dimension array containing menu-location slugs as keys and menu compositions are values. Menu compositions are array that contain the items inside each menu. This array has page titles or URLs as keys, then menu item labels as values. Defaults to blank. (Refer to the examples below for a better explanation)
callback string (Optional) A callback function to call before a sample content is activated.
pre_callback string (Optional) A callback function to call after a sample content is activated.
reset_all_options boolean (Optional) If true, resets all options across all admin panels and customizer sections after a sample content is activated. Otherwise, the current options will be retained. Defaults to true.
button string (Optional) The label of the button. Defaults to Activate.

Examples

Example #1 – The basics

This is a complete example that you can paste in your project (assuming you have the Sample Content Extension of course). This creates 2 admin pages: one for a few options, and one for the sample content. We have a text option and a color option, and both of these change when you click on the activate button in the sample content admin page.

// All options should be created from within this action hook.
add_action( 'tf_create_options', 'my_theme_settings' );
function my_theme_settings() {
// Initialize Titan with my special unique namespace.
$titan = TitanFramework::getInstance( 'my-theme' );
// Our main admin menu page/item.
$adminPanel = $titan->createAdminPanel( array(
'name' => 'My Theme Settings',
) );
// Create a text option.
$adminPanel->createOption( array(
'name' => 'My Text Option',
'id' => 'my_text',
'type' => 'text',
'default' => 'This is my text value',
) );
// Create a color option.
$adminPanel->createOption( array(
'name' => 'My Color Option',
'id' => 'my_color',
'type' => 'color',
'default' => '#000',
) );
// Create save and restore dafault button.
$adminPanel->createOption( array(
'type' => 'save',
) );
// Create the admin sample content page.
$sampleContentPage = $adminPanel->createSampleContentPage( array(
'name' => 'Demo Content',
) );
// Creates a sample content.
$sampleContentPage->createSampleContent( array(
'name' => 'My Demo Content',
'options' => array(
'my_text' => 'It will now have this value',
'my_color' => '#ffa500',
),
) );
}
​

Example #2 – A Preview Image with Sample Front Page

This sample content has a preview image, and creates a new page with the title “Sample Frontpage” (the name is always like that), then assigns that as the front page upon activation.

// Creates a sample content.
$sampleContentPage->createSampleContent( array(
'name' => 'My Demo Content',
'desc' => 'Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.',
'image' => trailingslashit( get_template_directory_uri() ) . 'images/header.jpg',
'options' => array(
'my_text' => 'It will now have this value',
'my_color' => '#ffa500',
),
'frontpage_content' => 
'<h1>Sparkler Exposure Shots for the Holidays</h1>
<span><i>October 27, 2016</i></span>
<p>And if she hasn’t been rewritten, then they are still using her. Far far away, behind the word mountains, far from the countries Vokalia and Consonantia, there live the blind texts.</p>
<p><img src="http://local.wordpress.dev/wp-content/uploads/2016/10/image1.jpg"/></p>
<p><img src="http://local.wordpress.dev/wp-content/uploads/2016/10/image1.jpg"/></p>
<p>A small river named Duden flows by their place and supplies it with the necessary regelialia. It is a paradisematic country, in which roasted parts of sentences fly into your mouth.</p>
<p><img src="http://local.wordpress.dev/wp-content/uploads/2016/10/image1.jpg"/></p>
<p>But nothing the copy said could convince her and so it didn’t take long until a few insidious Copy Writers ambushed her, made her drunk with Longe and Parole and dragged her into their agency, where they abused her for their projects again and again. And if she hasn’t been rewritten, then they are still using her. bFar far away, behind the word mountains, far from the countries Vokalia and Consonantia, there live the blind texts. Separated they live in Bookmarksgrove right at the coast of the Semantics, a large language ocean. A small river named Duden flows by their place and supplies it with the necessary regelialia.</p>',
) );​

Example #3 – Create a Page and Create a Menu

This sample creates a menu in the theme’s secondary menu location. The menu contains 5 links, where the first 4 are pages, and the last one is a custom links that points to http://google.com.

A page called “Sample Page” is also created upon activation. Take note that this page has the same title as the 4th menu item in the menu that’s created, which means that that menu item points to the page we just created.

$sampleContentPage->createSampleContent( array(
'name' => 'My Demo Content',
'desc' => 'Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.',
'image' => trailingslashit( get_template_directory_uri() ) . 'images/header.jpg',
'options' => array(
'my_text' => 'It will now have this value',
'my_color' => '#ffa500',
),
'menu' => array(
'secondary' => array(
'Frontpage' => 'My Home Label',
'Blog' => 'My Blog',
'Contact Us' => 'My Contact Label',
'Sample Page' => 'My Sample',
'http://google.com' => 'My Tests Label',
),
),
'pages' => array(
'Sample Page' => '<h1>Sparkler Exposure Shots for the Holidays</h1>
<span><i>October 27, 2016</i></span> 
<p>And if she hasn’t been rewritten, then they are still using her. Far far away, behind the word mountains, far from the countries Vokalia and Consonantia, there live the blind texts.</p>
<p><img src="http://local.wordpress.dev/wp-content/uploads/2016/10/image1.jpg"/></p>
<p><img src="http://local.wordpress.dev/wp-content/uploads/2016/10/image1.jpg"/></p>
<p>A small river named Duden flows by their place and supplies it with the necessary regelialia. It is a paradisematic country, in which roasted parts of sentences fly into your mouth.</p>
<p><img src="http://local.wordpress.dev/wp-content/uploads/2016/10/image1.jpg"/></p>
<p>But nothing the copy said could convince her and so it didn’t take long until a few insidious Copy Writers ambushed her, made her drunk with Longe and Parole and dragged her into their agency, where they abused her for their projects again and again. And if she hasn’t been rewritten, then they are still using her. bFar far away, behind the word mountains, far from the countries Vokalia and Consonantia, there live the blind texts. Separated they live in Bookmarksgrove right at the coast of the Semantics, a large language ocean. A small river named Duden flows by their place and supplies it with the necessary regelialia.</p>',
),
) );​

Example #4 – Callbacks

This sample executes specified functions upon activation. On activation, before any options are changed or any pages/menus are created, an option named my_sample_content is updated to have the name of the sample content being activated. Then after the options are changed, and all pages/menus have been created, a caching plugin’s cache is cleared.

// All options should be created from within this action hook.
add_action( 'tf_create_options', 'my_theme_settings' );
function my_theme_settings() {
// Initialize Titan with my special unique namespace.
$titan = TitanFramework::getInstance( 'my-theme' );
// Our main admin menu page/item.
$adminPanel = $titan->createAdminPanel( array(
'name' => 'My Theme Settings',
) );
// Create the admin sample content page.
$sampleContentPage = $adminPanel->createSampleContentPage( array(
'name' => 'Demo Content',
) );
// Creates a sample content.
$sampleContentPage->createSampleContent( array(
'name' => 'My Demo Content',
'desc' => 'Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.',
'image' => trailingslashit( get_template_directory_uri() ) . 'images/header.jpg',
'options' => array(
'my_text' => 'It will now have this value',
'my_color' => '#ffa500',
),
'frontpage_content' => 
'<h1>Sparkler Exposure Shots for the Holidays</h1>
<span><i>October 27, 2016</i></span>
<p>And if she hasn’t been rewritten, then they are still using her. Far far away, behind the word mountains, far from the countries Vokalia and Consonantia, there live the blind texts.</p>
<p><img src="http://local.wordpress.dev/wp-content/uploads/2016/10/image1.jpg"/></p>
<p><img src="http://local.wordpress.dev/wp-content/uploads/2016/10/image1.jpg"/></p>
<p>A small river named Duden flows by their place and supplies it with the necessary regelialia. It is a paradisematic country, in which roasted parts of sentences fly into your mouth.</p>
<p><img src="http://local.wordpress.dev/wp-content/uploads/2016/10/image1.jpg"/></p>
<p>But nothing the copy said could convince her and so it didn’t take long until a few insidious Copy Writers ambushed her, made her drunk with Longe and Parole and dragged her into their agency, where they abused her for their projects again and again. And if she hasn’t been rewritten, then they are still using her. bFar far away, behind the word mountains, far from the countries Vokalia and Consonantia, there live the blind texts. Separated they live in Bookmarksgrove right at the coast of the Semantics, a large language ocean. A small river named Duden flows by their place and supplies it with the necessary regelialia.</p>',
'pre_callback' => 'my_save_sample_data',
'callback' => 'my_flush_cache',
) ); 
}
// Function for callback.
function my_flush_cache() {
if ( function_exists( 'w3tc_pgcache_flush_post' ) ) {
w3tc_pgcache_flush_post();
}
}
// Function for pre_callback.
function my_save_sample_data( $params ) {
// Store the name of the sample content which was activated.
update_option( 'my_sample_content', $params['name'] );
}