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
/**
* Filter urls for script elements or their contents which should be blocked
* @param array $script_tags
* @return array $script_tags
*/
function cmplz_edit_script_tags($script_tags){
$script_tags[] = 'my-blocked-url.com';
unset($script_tags['my-un-blocked-url.com']);
return $script_tags;
}
add_filter('cmplz_known_script_tags', 'cmplz_edit_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';
unset($async_script_tags['my-un-blocked-url.com']);
return $async_script_tags;
}
add_filter('cmplz_known_async_tags', 'cmplz_edit_async_script_tags');
/**
* 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');
/**
* Filter class used for the element which contains the video.
* @param string $class
* @return string $class
*/
function cmplz_edit_video_class($class){
$class .= ' my-class';
return $class;
}
add_filter('cmplz_video_class','cmplz_edit_video_class');
/**
* 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 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 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 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);
/**
* 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');
/**
* 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');
/**
* 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($new_src, $type, $original_srcrc){
$known_script_tags[] = 'my-blocked-url.com';
unset($known_script_tags['my-un-blocked-url.com']);
return $known_script_tags;
}
add_filter('cmplz_placeholder','cmplz_edit_placeholder' ,10, 3);
/**
* 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');