Welcome to our new Help Center. We’re still adding articles, and if you can’t find an answer yet, we’re happy to help.Contact Us

Actions and filters

With LayerSlider’s actions and filters, your code can run when the plugin is installed, updated, loaded, or deleted, and can change a project’s settings, HTML, and script before they reach the page.

On this page

    Add your add_action() and add_filter() calls to your theme’s functions.php or to a plugin of your own. Your functions are called only while LayerSlider is running.

    Actions

    LayerSlider has 8 actions, and none of them passes an argument to your function.

    Action When it runs
    layerslider_ready Once on every request, on visitors’ pages and in the WordPress admin, after LayerSlider has loaded: during WordPress’s after_setup_theme action, before init
    layerslider_activated When LayerSlider is activated on the Plugins screen
    layerslider_installed On a new installation, in the first request to the WordPress admin after the activation, when LayerSlider has finished its own first-run steps
    layerslider_updated In the first request to the WordPress admin after LayerSlider is updated, when it has finished its own update steps
    layerslider_deactivated When LayerSlider is deactivated
    layerslider_uninstalled When LayerSlider is deleted on the Plugins screen
    layerslider_before_slider_content When LayerSlider starts building a project’s HTML
    layerslider_after_slider_content When LayerSlider has built a project’s HTML

    To wrap a project or change its HTML, use the layerslider_slider_markup filter: what layerslider_before_slider_content and layerslider_after_slider_content print doesn’t go around the project.

    Deleting LayerSlider on the Plugins screen keeps its projects and settings in the database. When the same version is installed again, layerslider_activated runs, but layerslider_installed doesn’t.

    Filters

    With LayerSlider’s filters, your code can change these values:

    Filter What it changes What your function receives What it returns
    layerslider_slider_markup A project’s HTML The HTML as a string, the project as an array, and the id of the project’s element on the page The HTML to print instead
    layerslider_slider_init The script that starts a project The script as a string without <script> tags, the project, and the id of its element The script to print instead
    layerslider_pre_parse_defaults A project’s settings, before LayerSlider reads them The project’s data, with its settings in properties under the keys they’re saved with, such as autostart The changed data
    layerslider_post_parse_defaults A project’s data, after LayerSlider has read its settings The project’s data in the same form, with the changes of layerslider_pre_parse_defaults The changed data
    layerslider_override_defaults The default of each project setting, which a new project shows when it opens in the Project Editor LayerSlider’s table of defaults, in groups such as slider, slides, and layers, with each setting under a name such as autoStart and its default in value The changed table
    layerslider_attr_list One attribute of a slide or a layer, such as a layer’s style The attribute’s parts, as an array of name:value; strings The attribute’s value, as one string
    layerslider_root_url The address that LayerSlider loads its files from The address of LayerSlider’s folder, without /assets The address to use instead, also without /assets
    ls_meta_generator The generator <meta> tag and the HTML comment that LayerSlider prints in the page’s <head> Both, as one string What to print instead
    ls_can_access_library Who can use the project picker, the Choose Project window that lists your projects for inserting one Whether the current user has access, as true or false true to give access, false to refuse it

    To change the settings that projects are printed with, use layerslider_pre_parse_defaults, as the third example does: a setting changed in layerslider_post_parse_defaults doesn’t reach the page.

    A theme that loads LayerSlider from its own folder needs layerslider_root_url: see what to return for a theme’s copy.

    Without code, the Project Library Access list in Plugin Settings › Permissions sets who can use the project picker. Your function receives what the plugin’s permissions decide for the current user, and what it returns takes their place.

    Wrapping or changing a project’s HTML

    To put a project inside an element of your own, or to change its HTML, return the changed HTML from layerslider_slider_markup. The HTML that the filter receives comes without the project’s script, which layerslider_slider_init receives separately.

    layerslider_before_slider_content and layerslider_after_slider_content print their output together, before the project, when LayerSlider builds the project’s HTML. With the shortcode in a page of a block theme, their output stands at the top of the document, before <!DOCTYPE html>. In a theme template that calls layerslider(), their output stands where the template calls the function.

    If your code was written for LayerSlider 8, see which changes in LayerSlider 9 affect it.

    Examples

    The first example runs your own code after LayerSlider has loaded.

    PHP
    add_action( 'layerslider_ready', function () {
    	// Your code that relies on LayerSlider.
    } );

    The second example wraps each project that your pages print in an element with the class project-frame. Popups go through the filter too, but LayerSlider moves a popup out of its place as soon as the popup starts. To leave popups as they are, return their HTML unchanged: for a popup, the project that the filter receives as its second argument has flag_popup set. Change the class to one of your own.

    PHP
    add_filter( 'layerslider_slider_markup', function ( $markup ) {
    	return '<div class="project-frame">' . $markup . '</div>';
    } );

    With the third example, LayerSlider prints every project as if its Auto-Start Slideshow switch were off.

    PHP
    add_filter( 'layerslider_pre_parse_defaults', function ( $data ) {
    	$data['properties']['autostart'] = false;
    	return $data;
    } );