Creating a custom generator in WordPress

⚠️ This article was created for developers!

To be able to follow the steps, you're required to have coding knowledge. If you're not a developer and can't achieve what you're looking for, consider hiring a developer.

Make sure to carefully follow the tutorial and write correct code. We're unable to provide help with custom coding, or the possible issues that are happening because of them.

With Smart Slider you can create a generator from any source, if you just write a customized code for it. We suggest creating a new plugin and write your custom codes in it.

☝️ Note: Custom Generators are only available in the Pro version of Smart Slider!
⚠️ Warning: Even if you write a custom generator, you can't get around Smart Slider's limitations! For example, you still won't be able to create a slider based on the content of the current page.
Also, please note that we won't be able to provide any kind of support for your custom generator.

Create a plugin

Step 1

Create a folder inside your plugins folder. I will call it "custom":
wp-content\plugins\ custom

Step 2

Create a PHP file with the same name you used for the folder:
wp-content\plugins\custom\ custom.php

and make it contain this code with a name of your choice:

<?php
/**
 * Plugin Name: Custom
 **/

Step 3

Your plugin is ready and you can activate it. Now any code written in it will run on your website!

Custom generator PHP codes

Step 1

Use this code as the starting point:

<?php
/**
 * Plugin Name: Custom
 **/
function add_example_generator($customGenerators) {

    $options = array();

    $customGenerators[] = array(
        'name'    => 'example_generator',
        'label'   => 'Example',
        'options' => $options,
        'records' => 'getRecords'
    );

    return $customGenerators;
}

function getRecords($data) {
    
    $result = array();

    return $result;
}

add_filter('smartslider3_custom_generator', 'add_example_generator');

The name of add_example_generator can be anything, just use the same name at the add_filter part too. This function's name has to be unique, in case you would like to create multiple custom generators.

This  add_example_generator function is the part, where you can create options, like on/off options or input areas, which can be used easily to modify the result. Check out step 2 to understand it better.

The getRecords function's name can also be anything, and this has to be unique too. Make sure you will use the same name next to 'records'.

This  getRecords function is where you are processing your code, so these will produce the datas, which will appear on the slides. Check out step 3 to understand it better.

Lastly the  add_filter adds your generator to our Custom generator.

Step 2

Let's focus on this part:

function add_example_generator($customGenerators) {

    $options = array();

    $customGenerators[] = array(
        'name'    => 'example_generator',
        'label'   => 'Example',
        'options' => $options,
        'records' => 'getRecords'
    );

    return $customGenerators;
}

This creates all the options you would like to have in the admin area.

If you want to add an on/off, add it to the $options array:

function add_example_generator($customGenerators) {

    $options = array();

    $options[] = array(
        'type'    => 'onoff',
        'name'    => 'published',
        'label'   => 'Published',
        'default' => 1
    );

    $customGenerators[] = array(
        'name'    => 'example_generator',
        'label'   => 'Example',
        'options' => $options,
        'records' => 'getRecords'
    );

    return $customGenerators;
}

so this is the only new part:

$options[] = array(
        'type'    => 'onoff',
        'name'    => 'published',
        'label'   => 'Published',
        'default' => 1
    );

The type defines what kind of option would you like to have. I will write a list about all options, but now check out this  onoff.

The name and label of this option can be anything. The name has to be unique, as this will be the identifier. The label is just the text written out into the admin area next to the on/off button.

The default value is either 1 or 0, to decide it should be on or off by default.

If you would like to have multiple on/off buttons in one row, create a multidimensional array:

    $options[] = array(
        array(
            'type' => 'onoff',
            'name' => 'example1',
            'label'   => 'First example on off',
            'default' => 1
        ),
        array(
            'type' => 'onoff',
            'name' => 'example2',
            'label'   => 'Second example on off',
            'default' => 0

        )
    );

The other types you have:

Select

    $options[] = array(
            'type'     => 'select',
            'name'     => 'list',
            'label'    => 'Test list',
            'options'  => array(
                1 => 'first',
                2 => 'second'
            ),
            'default'  => 2,
            'multiple' => true,
            'size'     => 5
        );

This is a selection list. The name has to be unique.

The options should contain an array of the selectable options, where the first value is the identifier, the second is the label.

In the default value can contain one of the identifiers from the previous options.

With multiple you can decide whether you want a selection list, where you can select more options together or you want a dropdown list, where only one option can be selected. This value can be true or false.

The size can be used, if multiple is set to true. This decides how many options you see together within the scrollable select. You can write a number in here.

Text

    $options[] = array(
            'type'    => 'text',
            'name'    => 'my_text',
            'label'   => 'Name test',
            'default' => ''
        );

This creates a simple text input. The name has to be unique.

Textarea

    $options[] = array(
        'type'    => 'textarea',
        'name'    => 'test_area',
        'label'   => 'test area',
        'default' => '',
        'width'   => 500,
        'height'  => 300
    );
Same as the text, just you are creating a bigger textarea, where you can write down a pixel value into the width and height options.

Step 3

Now we should discuss this part:

function getRecords($data) {
    
    $result = array();

    return $result;
}

This is where the entire processing happens. The $result should be an array containing the datas, for example:

function getRecords($data) {
    $result[0] = array(
        'first'  => 'first value',
        'second' => 'second value'
    );

    $result[1] = array(
        'first'  => 'xy',
        'second' => 'z'
    );

    return $result;
}

As you see, the keys should be the same for each arrays, as these will be the variables and their values should be unique, what you would like to see on the 1st slide, 2nd slide, etc.

The $data returns the step 2 options this way:

$data['options']['name']

so for example if you had an on/off, you can use it to decide what should happen:

function getRecords($data) {
    
    if($data['options']['usewords']){
        $result[0] = array(
            'first'  => 'first value',
            'second' => 'second value'
        );
    } else {
        $result[0] = array(
            'first'  => 'xy',
            'second' => 'z'
        );      
    }

    return $result;
}

Example

<?php
/**
 * Plugin Name: Custom
 **/
function add_example_generator($customGenerators) {

    $options = array();

    $options[] = array(
        'type'    => 'onoff',
        'name'    => 'published',
        'label'   => 'Published',
        'default' => 1
    );

    $options[] = array(
        array(
            'type' => 'onoff',
            'name' => 'published2'
        ),
        array(
            'type' => 'onoff',
            'name' => 'published3'
        )
    );

    $options[] = array(
        array(
            'type'     => 'select',
            'name'     => 'list',
            'label'    => 'Test list',
            'options'  => array(
                1 => 'first',
                2 => 'second'
            ),
            'default'  => 2,
            'multiple' => true,
            'size'     => 5
        ),
        array(
            'type'    => 'text',
            'name'    => 'name',
            'label'   => 'Name test',
            'default' => ''
        )
    );

    $options[] = array(
        'type'    => 'textarea',
        'name'    => 'test_area',
        'label'   => 'test area',
        'default' => '',
        'width'   => 500,
        'height'  => 300
    );

    $customGenerators[] = array(
        'name'    => 'example_generator',
        'label'   => 'Example',
        'options' => $options,
        'records' => 'getRecords'
    );

    return $customGenerators;
}

function getRecords($data) {
    $result = array();

    $result[0] = array(
        'first'  => $data['options']['name'],
        'second' => 'something'
    );

    $result[1] = array(
        'first'  => 'xy',
        'second' => 'z'
    );

    return $result;
}

add_filter('smartslider3_custom_generator', 'add_example_generator');

In a more real life example we are asking down datas for example from the database. With a foreach or for loop you can go trough all these datas and return your result:

function getRecords($data) {
    global $wpdb;
    $posts = $wpdb->get_results("SELECT * FROM " . $wpdb->base_prefix . "posts LIMIT 5");
    
    $result = array();
    
    foreach($posts AS $post){
        $helper = array();
        $helper['title'] = $post->post_title;
        $helper['description'] = $post->post_content;
        
        $result[] = $helper;
    }

    return $result;
}

Tip

Step 3 could be hard to debug, but not if you know where to look. For debugging purposes you can use var_dump and exit functions to write out what datas you have:

function getRecords($data) {
    $result[0] = array(
        'first'  => $data['options']['name'],
        'second' => 'something'
    );

    $result[1] = array(
        'first'  => 'xy',
        'second' => 'z'
    );
    
    var_dump($result);exit;

    return $result;
}

To see what this results, in Chrome if you press F12 -> click on Network, then by pressing our View records button a new call will appear. In this call's Response you will see the var_dump-ed datas.

Did this answer your question? Thanks for the feedback There was a problem submitting your feedback. Please try again later.