Some functionalities can be added by way of add_filter()

How to add them. And the collection thus far!

Rogier

Rogier

Senior Developer

Last Updated: 4/4/2019

Categories

Most Popular Articles

Subscribe to our newsletter

    For EU Subscribers | Privacy Statement
    For USA Subcribers | Privacy Statement

  • This field is for validation purposes and should be left unchanged.

A quick introduction

WordPress offers filter hooks to allow plugins to modify various types of internal data at runtime.

A plugin can modify data by binding a callback to a filter hook. When the filter is later applied, each bound callback is run in order of priority, and given the opportunity to modify a value by returning a new value.

The following example shows how a callback function is bound to a filter hook.

Note that $example is passed to the callback, (maybe) modified, then returned:

function example_callback( $example ) {
    // Maybe modify $example in some way.
    return $example;
}
add_filter( 'example_filter', 'example_callback' );

If you want to know more, before adding your first add_filter(), see below source. If it’s now your first time, see our collection of filters below.

source: https://developer.wordpress.org/reference/functions/add_filter/

If you have never used add_filter() or other functions to modify .php files in your theme or plugin files. Please make sure you have a back-up of the file you’re modifying and quick access to your FTP to revert back if anything goes wrong.

Adding your filters with Must-Use Plugins. It’s really simple!

Adding filters and hooks for our plugin might look difficult, but it can be done quite easy. Copy/Paste below the needed snippet below in your favorite text editor and follow the instructions in our article about MU Plugins

Our collection of prefilled add_filter()

Block or unblock script tags

/**
 function cmplz_edit_script_tags($script_tags){
$script_tags[] = 'my-blocked-url.com';
if (($key = array_search('my-blocked-script-tag.js',
$script_tags)) !== false) {
unset($script_tags[$key]);
}
return $script_tags;
}
add_filter('cmplz_known_script_tags', 'cmplz_edit_script_tags');

Block or unblock asynchronous script tags

/**
 * Filter urls for script elements or their contents which are loaded asynchronously and should be blocked
 * @param array $async_script_tags
 * @return array $async_script_tags
 */
function cmplz_edit_async_script_tags($async_script_tags){
    $async_script_tags[] = 'my-blocked-url.com';
    if (($key =
    array_search('my-blocked-script-tag.js',
    $async_script_tags)) !== false) {
    unset($async_script_tags[$key]);
}
    return $async_script_tags;
}
add_filter('cmplz_known_async_tags', 'cmplz_edit_async_script_tags');

Block or unblock iFrames


/**
 * Filter urls for iframes that should be blocked
 * @param array $iframe_tags
 * @return array $iframe_tags
 */

function cmplz_edit_iframe_tags($iframe_tags){
    $iframe_tags[] = 'my-blocked-url.com';
    unset($iframe_tags['my-un-blocked-url.com']);

    return $iframe_tags;
}
add_filter('cmplz_known_iframe_tags', 'cmplz_edit_iframe_tags');

Edit the source of your placeholder (iFrames)


/**
 * Filter URL to image or video placeholder which is placed in a blocked iframe source
 * @param string $placeholder
 * @return string $placeholder
 */
function cmplz_edit_source_placeholder($placeholder){
    $placeholder = 'url-to-my-placeholder.jpg';
    return $placeholder;
}
add_filter('cmplz_source_placeholder','cmplz_edit_source_placeholder');

Edit individual placeholders


/**
 * Filter placeholder on an individual basis
 * @param string $new_src current placeholder, default value
 * @param string $type e.g. youtube
 * @param string $original_src original src
 * @return string $placeholder
 */
function cmplz_edit_placeholder($placeholder, $type, $original_src){
    $placeholder = 'url-to-placeholder.jpg';
    return $placeholder;
}
add_filter('cmplz_placeholder','cmplz_edit_placeholder' ,10, 3);

Filter default placeholder


/**
 * Filter default placeholder image on blocked content
 * @param string $placholder
 * @return string $placeholder
 */
function cmplz_edit_default_placeholder($placeholder){
    $placeholder = 'my-placeholder-url';

    return $placeholder;
}
add_filter('cmplz_default_placeholder', 'cmplz_edit_default_placeholder');

Modify text on blocked content


/**
 * Filter text which is shown on blocked content
 * @param string $text
 * @return string $text
 */
function cmplz_edit_blocked_content_text($text){
    $text = 'this content is blocked. Deal with it!';
    return $text;
}
add_filter('cmplz_accept_cookies_blocked_content','cmplz_edit_blocked_content_text');

Filter comments in document html


/**
 * Filter comment used in document html
 * @param string $comment
 * @return string $comment
 */

function cmplz_edit_document_comment($comment){
    $comment = 'my-comment';
    return $comment;
}
add_filter("cmplz_document_comment", 'cmplz_edit_document_comment');

Filter document output


/**
 * Filter output of document html 
 * @param string $html
 * @param string $type e.g. cookie-policy
 * @param int $post_id
 * @return string $html
 */

function cmplz_edit_document_html($html, $type, $post_id){
    //do something with html 

    return $html;
}
add_filter('cmplz_document_html', 'cmplz_edit_document_html', 10, 3);

Filter document css


/**
 * Filter custom document css
 * @param string $custom_css
 * @param string $type
 * @param int $post_id
 * @return string $custom_css
 */

function cmplz_edit_custom_document_css($custom_css, $type, $post_id){
    //do something with css

    return $custom_css;
}
add_filter('cmplz_custom_document_css', 'cmplz_edit_custom_document_css', 10, 3);

Changing cookie banner ‘read more’ URL


/**
 * Filter Cookie banner settings 
 * @param array $output
 * @return array $output
 */
function cmplz_edit_cookie_settings($output){
    $output['readmore_url'] = 'my-cookie-policy-url';
    return $output;
}
add_filter('cmplz_cookie_settings', 'cmplz_edit_cookie_settings');

Changing revoke settings URL


/**
 * Filter revoke link html 
 * @param string $html
 * @return string $html
 */

function cmplz_edit_revoke_link($html){
    //edit revoke link html

    return $html;
}
add_filter('cmplz_revoke_link', 'cmplz_edit_revoke_link');
© Copyright - Complianz 2019