Skip to content

Filter and Action Hooks

Michiel Tramper edited this page Jul 12, 2019 · 2 revisions

The Velocity WordPress theme has some filters hooks which allow you to modify settings and templates.

Action Hooks

In the current version, the Velocity theme uses the following action hooks:

Name Passed parameters Description
'velocity_before_content' None Executes just before displaying the post content in singular.php.
'velocity_after_content' None Executes just after displaying the post content in singular.php.

Filter Hooks

Velocity utilizes many filter hooks, which are categorized below per file

General Filter Hooks

These are general filters described in classes/velocity.php.

Name Passed parameters Description
'velocity_configurations' $configurations The array of configurations The array with all configurations that are loaded for the Velocity theme.
'velocity_templates' $templates The array of WordPress templates The templates that are used for altering the template hierarchy. Helps WordPress to find the templates inside the templates folder of the theme.
'velocity_modules' $modules The array of theme modules The modules that may accept a configuration, as long as these configurations are stored under a similar array key as the given module.

Component Class

These are filters used by the component class, located in classes\views\component.php

Name Passed parameters Description
'velocity_{$component}_properties' $properties The array of properties. Allows adjusting the properties that are loaded into a component. A component is a reusable part of the website, such as a grid of posts. Please replace {$component} for the actual name of the component.

Author Component Class

These are filters used by the author component, located in classes\views\components\author.php

Name Passed parameters Description
'velocity_post_avatar_size' $size The integer for the size of the avatar. This filter can be used to change the size of the author's avatar used in posts.

Meta Component Class

These are filters used by the meta component, located in classes\views\components\meta.php

Name Passed parameters Description
'velocity_projects_avatar_size' $size The integer for the size of the avatar. This filter can be used to change the size of the author's avatar used in projects.

Posts Component Class

These are filters used by the posts component, located in classes\views\components\posts.php

Name Passed parameters Description
'the_content' $content The content within a post. This filter is used inside a function to retrieve the excerpt. It is also a basic filter used by WordPress.
'excerpt_length' $lenght The length of an excerpt. This filter is used inside a function to retrieve the excerpt. It is also a basic filter used by WordPress.
'excerpt_more' $more The more text used within an excerpt, defaults to '…'. This filter is used inside a function to retrieve the excerpt. It is also a basic filter used by WordPress.
'wp_trim_excerpt' $text The excerpt, $raw_excerpt The raw excerpt. This filter is used inside a function to retrieve the excerpt. It is also a basic filter used by WordPress.

Share Component Class

These are filters used by the share component, located in classes\views\components\share.php

Name Passed parameters Description
'velocity_sharing_networks' $networks The array of networks that is displayed in the social sharing buttons, $url The URL that is shared, $title The title that is used for sharing. This filter can be used to dynamically remove or add sharing buttons.

Singular View Class

These are filters used by class preparing the variables for the singular template, located in classes\views\singular.php

Name Passed parameters Description
'the_content' $content The content within a post. This filter is used to apply the WordPress content filters on the retrieved content outside of the loop.

Template View Class

These are filters used by the abstract class that is used to prepare variables for templates, located in classes\views\template.php

Name Passed parameters Description
'velocity_template_data' $data The array of data. The array of data that is loaded within a template, usually customizer options, metadata and general options.
'velocity_($template}_properties' $properties The array of properties. The array of prepared properties that are used in actual templates to display data. Please replace {$template} for the template you're referring to, for example, 'velocity_singular_template'.

Adding Customizer, Meta and Option Fields, Scripts, Styles, Menus

With the 'velocity_configurations' filter hook, any of the Theme configurations can be changed. This included the configurations for meta boxes, customizer fields and options pages. The following code alters some of the configurations.

These configurations follow the format as described in the WIKI of each of their modules, as configurations are inserted into a module (such as the WP Custom Fields Module).

add_filter('velocity_configurations', function($config) {

    // Adds a new style, according to the specifications of WP Enqueue on https://github.com/makeitworkpress/wp-enqueue
    $config['enqueue'][] =  ['handle' => 'velocity-second', 'src' => get_template_directory_uri() . '/assets/css/velocity.second.css'];

    // Registers a new menu, according to the specifications of WP Register on https://github.com/makeitworkpress/wp-register
    $config['register']['menus']['secondary'] = __('Secondary Navigation', 'velocity');                                

    // Registers a new post type, according to the specifications of WP Register on  https://github.com/makeitworkpress/wp-register
    $config['register']['postTypes'][] = [
        'name'      => 'clients',
        'plural'    => __( 'Clients', 'velocity' ),
        'singular'  => __( 'Client', 'velocity' ),
        'args'      => [
            'menu_icon'     => 'dashicons-admin-users', 
            'has_archive'   => true, 
            'show_in_rest'  => true, 
            'supports'      => ['author', 'editor', 'thumbnail', 'title']
        ],
        'slug'      => 'client'
    ];  
    
    // Example of registering a new customizer panel, according to the specifications of WP Custom Fields on  https://github.com/makeitworkpress/wp-custom-fields
    $config['settings'][] = [ 
        'frame'         => 'customizer', 
        'description'   => __('Custom Customizer settings for the Velocity Theme.', 'velocity'),
        'id'            => 'custom_customizer',
        'title'         => __('Velocity', 'velocity'), 
        'sections'      => [
            [
                'id'            => 'custom_section',
                'priority'      => 2,
                'title'         => __('Custom Section', 'velocity'), 
                'fields'        => [
                    [
                        'default'       => '',
                        'id'            => 'additional_logo',
                        'title'         => __('Additional Logo Image', 'velocity'),
                        'type'          => 'media',
                    ]                            
                ]          
            ],
        ]
    ];         

    // Adds a new tab to the existing options page, according to the specifications of WP Custom Fields on  https://github.com/makeitworkpress/wp-custom-fields
    $config['settings']['options']['sections']['new'] = [ 
        'icon'          => 'settings',
        'id'            => 'new',
        'title'         => __('New Settings', 'velocity'),
        'description'   => __('Newly added settings', 'velocity'),
        'fields'        => [                                                                        
            [
                'columns'       => 'half',
                'description'   => __('Some description', 'velocity'),
                'id'            => 'password',
                'title'         => __('Some Title', 'velocity'),
                'type'          => 'input',
                'subtype'       => 'password'
            ],
            [
                'columns'       => 'half',
                'default'       => '',
                'description'   => __('Choose something', 'velocity'),
                'id'            => 'select',
                'options'       => [
                    'one'       => __('One', 'velocity'),
                    'two'       => __('Two', 'velocity')
                ],
                'title'         => __('Select', 'velocity'),
                'type'          => 'select'
            ]  
        ]
    ];        
    
    // Example of adding a metabox to a post, according to the specifications of WP Custom Fields on  https://github.com/makeitworkpress/wp-custom-fields
    $config['settings'][] = [ 
        'frame'     => 'meta',  
        'id'        => 'your_meta_key',
        'title'     => __('Meta Fields', 'velocity'),
        'screen'    => ['post'],
        'type'      => 'post',
        'context'   => 'normal',
        'priority'  => 'default',
        'sections'  => [
            'general' => [
                'id'        => 'section_one',
                'title'     => __('Default Options', 'velocity'),
                'icon'      => 'settings',
                'tabs'      => false,
                'fields'    => [
                    [
                        'id'            => 'subtitle',
                        'title'         => __('Subtitle', 'velocity'),
                        'description'   => __('Adds a subtitle.', 'velocity'),
                        'type'          => 'textarea',
                        'rows'          => 3          
                    ],
                ]
            ]
        ]
    ];

    return $config;

});