Filters and actions hooks
#
PHP hooks and actionsIn plugin core we have a filters and actions hooks that are analogs of wordpress filters and actions with some improverments and advantages in use for PHP code.
Wordpress Function | Visualcomposer Analog |
---|---|
apply_filters | vcfilter |
do_action | vcevent |
add_filter | EventsFilters::addFilter() |
add_action | EventsFilters::addEvent() |
#
vcfilterThis function invokes all functions attached to filter hook $filter
.
vcfilter( string $filter, mixin $body = '', mixin $payload = [], bool $haltable = false)
Parameters
$filter
(string) Required - The name of the filter hook.
$body
(mixed) Optional - The value to filter.
$payload
(mixed) Optional - Additional parameters to pass to the callback functions.
$haltable
(bool) Optional - If true, then we can't override this filter.
Example in Visual Composer core.
Example:
$post = get_post();$link = get_edit_post_link($post, 'url');$query = [ 'vcv-action' => 'frontend', 'vcv-source-id' => $post->ID,];$frontendUrl = $link . http_build_query($query, '', '&');
vcfilter('vcv:frontend:url', $frontendUrl, ['sourceId' => $sourceId, 'query' => $query])
#
EventsFilters::addFilter()Adds a callback function to a filter hook.
addFilter(string $filterName, string $methodCallback, int $weight = 0)
Parameters
$filterName
(string) Required - The name of the filter to add the callback to.
$methodCallback
(string) Required - The callback to be run when the filter is applied.
$weight
(int) Optional - Used to specify the order in which the functions associated with a particular filter are executed.
Example can be implemented in functions.php
theme file or any plugin file.
Example:
add_action('vcv:api', function() { vchelper('Filters')->listen('vcv:frontend:url', 'actionHookExample');
function actionHookExample($url, $payload) {
if (empty($payload['sourceId']) || empty($payload['sourceId'])) { return $url; } $url = 'https://example.com'; return $url; }});
#
vceventCalls the callback functions that have been added to an action hook.
vcevent(string $event, mixin $payload = [])
Parameters
$event
(string) Required - The name of the action to be executed.
$payload
(mixed) Optional - Additional parameters to pass to the callback functions.
Example in Visual Composer core.
Example:
global $post;
vcevent( 'vcv:api:postSaved', ['post' => $post]);
#
EventsFilters::addEvent()Adds a callback function to an action hook.
addEvent(string $eventName, string $methodCallback, int $weight = 0)
Parameters
$eventName
(string) Required - The name of the action to add the callback to.
$methodCallback
(string) Required - The callback to be run when the action is called.
$weight
(int) Optional - Used to specify the order in which the functions associated with a particular action are executed.
Example can be inplemented in functions.php
theme file or any plugin file.
Example:
use VisualComposer\Framework\Container;use VisualComposer\Framework\Illuminate\Support\Module;use VisualComposer\Helpers\Traits\EventsFilters;
class Extender extends Container implements Module{ use EventsFilters;
public function __construct() { $this->addEvent( 'vcv:api:postSaved', 'initialize' ); }
public function initialize($payload) { if (get_post_type($payload['post']->ID) !== 'post') { return; }
// do some stuff here }}
new Extender();
#
JS hooks and actionsAdds filter to get or set data. Arguments:
- filter key
- callback function with an argument of provided data for that filter, add key and value to object and return data if needed.
Available filter points:
saveRequestData
- will be called on saving the post/page, object data that will saved will be provided as an argument, any key with value can be added and returned to this object and will be saved in DB. To receive the passed key on PHP side there are events that are listening on editor loading('vcv:dataAjax:getData')
and saving('vcv:dataAjax:setData')
. On saving all passed values can be received through the request. To pass the values to the editor on load, they need to be added to the response array.
Example:
window.vcwbEditorApi.addFilter('saveRequestData', (data) => { // Add custom key with any value data.exampleInsights = 'example insights value' // Return data return data})
insightPanelsData
- will be called on insights panel opening, object data with information about insight panels will be provided, to insert custom panel add key and value to object and return it.
Example:
window.vcwbEditorApi.addFilter('insightPanelsData', (data) => { data['third-party'] = { index: 1, type: 'third-party', title: 'Example Panel', icon: 'cog' // icon list is predefined in plugin core } return data})