Filters

Overview

Filters enable the admin to modify/hook into certain aspects of the task being done. There are several Filters in Virtualizor that you can take advantage of.

Note : Filters have been added since Virtualizor version 3.0.4

The following guide will show you how the Server Admin can configure Filter Functions to customize the create vps, manage vps, start vps and several other events.

1. SSH to your server and go to the following path:

/usr/local/virtualizor/hooks

2. You can see the file filter.txt

3. Rename the file to make it a PHP file as filter.php

4. Uncomment the function that matches your requirement, add the code inside the function as per your requirements and save the file.

5. That’s it you have configured your filter

insert_filter

We make use of insert_filter function to trigger a specific filter.

insert_filter(string $tag, string $function_to_add, int $priority, int $accepted_args);

Parameters

ParamTypeDescription
$tagstring/RequiredAction name that triggers the specific function.
$function_to_addstring/RequiredName of the function to be triggered.
$priorityint/optionalPriority in which you want the filter to be triggered. (Default is 10)
$accepted_argsint/optionalNumber of arguments accepted by the filter function. (Default is 1)

Example

Following is the example describing how a filter is triggered by an action

If you would like to make some changes whenever an error is triggered in Virtualizor, you will have to rename the file filter.txt to filter.php and uncomment the following part and add the code in my_error_handle() function.

insert_filter('error_handle', 'my_error_handle', 1, 1);

function my_error_handle($error){
     // Add your custom code here
}

insert_filter adds the data added by you to Virtualizor and when the appropriate event is triggered your respective function is called. For eg., in the above example, whenever the event error_handle occurs, function my_error_handle is called and the code inside the same is executed.

addvs

Description

The addvs filter will trigger your function VM creation is done.

NOTE: This function will be called ONLY on Master

Parameters

KeyValueDescription
$newvsarray (Required)Contains details of the VM which is being created

Usage

Uncomment the following part inside filter.php and add your code inside the function my_addvs

insert_filter(‘addvs’, ‘my_addvs’, 1, 1);
function my_addvs($newvs){
// Add your custom code here
}

editvs

Description

The editvs filter will trigger your function VM creation is done.

NOTE: This function will be called ONLY on Master

Parameters

KeyValueDescription
$vpsarray (Required)Contains details of the VM before edit operation
$editvsarray (Required)Contains New details of the VM after edit operation

Usage

Uncomment the following part inside filter.php and add your code inside the function my_editvs

insert_filter(‘editvs’, ‘my_editvs’, 1, 2);
function my_editvs($vps, $editvs){
// Add your custom code here
}

my_after_config_write

Description

The my_after_config_write filter will trigger your function when Virtualizor has written the config file for the VM.

Parameters

KeyValueDescription
$vpsarray (Required)Contains details of the VM

Usage

Uncomment the following part inside filter.php and add your code inside the function my_after_config_write

insert_filter(‘after_config_write’, ‘my_after_config_write’, 1, 1);
function my_after_config_write($vps){
// Add your custom code here
}

after_createvps

Description

The after_createvps filter will trigger your function after Virtualizor has created the VM.

Parameters

KeyValueDescription
$vpsarray (Required)Contains details of the VM

Usage

Uncomment the following part inside filter.php and add your code inside the function my_after_createvps

insert_filter(‘after_createvps’, ‘my_after_createvps’, 1, 1);
function my_after_createvps($vps){
// Add your custom code here
}

before_deletevps

Description

The before_deletevps filter will trigger your function before virtualizor deletes the VM.

Parameters

KeyValueDescription
$vpsarray (Required)Contains details of the VM

Usage

Uncomment the following part inside filter.php and add your code inside the function before_deletevps

insert_filter(‘before_deletevps’, ‘my_before_deletevps’, 1, 1);
function my_before_deletevps($vps){
// Add your custom code here
}

deletevs

Description

The before_deletevps filter will trigger your function when Virtualizor deletes the VM.

NOTE: This filter will be called on Master server ONLY

Parameters

KeyValueDescription
$vpsarray (Required)Contains details of the VM

Usage

Uncomment the following part inside filter.php and add your code inside the function deletevs

insert_filter(‘deletevs’, ‘my_deletevs’, 1, 1);
function my_deletevs($vps){
// Add your custom code here
}

after_deletevps

Description

The after_deletevps filter will trigger your function after Virtualizor has deleted the VM.

Parameters

KeyValueDescription
$vpsarray (Required)Contains details of the VM

Usage

Uncomment the following part inside filter.php and add your code inside the function after_deletevps

insert_filter(‘after_deletevps’, ‘my_after_deletevps’, 1, 1);
function my_after_deletevps($vps){
// Add your custom code here
}

before_startvps

Description

The before_startvps filter will trigger your function before Virtualizor starts the VM.

Parameters

KeyValueDescription
$vpsarray (Required)Contains details of the VM

Usage

Uncomment the following part inside filter.php and add your code inside the function after_deletevps

insert_filter(‘before_startvps’, ‘my_before_startvps’, 1, 1);
function my_before_startvps($vps){
// Add your custom code here
}

after_startvps

Description

The after_startvps filter will trigger your function after Virtualizor has started the VM.

Parameters

KeyValueDescription
$vpsarray (Required)Contains details of the VM

Usage

Uncomment the following part inside filter.php and add your code inside the function after_startvps

insert_filter(‘after_startvps’, ‘my_after_startvps’, 1, 1);
function my_after_startvps($vps){
// Add your custom code here
}

create_vm_pre_addvs

Description

The create_vm_pre_addvs filter will trigger your function before creating a VM from Cloud user account

Parameters

KeyValueDescription
$vpsarray (Required)Contains details of the VM

Usage

Uncomment the following part inside filter.php and add your code inside the function create_vm_pre_addvs

insert_filter(‘create_vm_pre_addvs’, ‘my_create_vm_pre_addvs’, 1, 1);
function my_create_vm_pre_addvs($vps){
// Add your custom code here
}

Was this helpful to you?