PHPVibe’s add_filter() and apply_filter()

5 Sep 2014
@PHPVibe
5723
1

PHPVibe’s add__filter()

Hooks a function to a specific filter action.

Usage:

add_filter( $tag, $function_to_add );

Example:

If you wish to dynamically inject a new title to the PHPVibe page:

function seotitle( $text ) {
return "My static title";
/* clearly you can build some php and use some globals here, not just use text ;) */}
add_filter( 'phpvibe_title', 'seotitle' );

PHPVibe’s apply_filter()

Calls the functions added to a filter hook.
The callback functions attached to filter hook $tag are invoked by calling this function. This function can be used to create a new filter hook by simply calling this function with the name of the new hook specified using the $tag parameter.

Usage:

apply_filters( $tag, $value, $var ... );

Example:

//SEO
function seo_title() {
return apply_filters( 'phpvibe_title', get_option('seo_title'));
}
function seo_desc() {
return apply_filters( 'phpvibe_desc', get_option('seo_desc'));
}

Useful hooks in PHPVibe

This two hooks may also help a lot (after the seo hooks explained upper):

function extra_js() {
return apply_filter( 'filter_extrajs', false );
}
function extra_css() {
return apply_filter( 'filter_extracss', false );
}

The inject custom css and js in your template file. You can attach an add_filter() to them easily.

function more_css( $text ) {
$mycss = '';
return $text.$mycss;
}
add_filter( 'filter_extracss', 'more_css' );

1 Comment

Leave a Reply

Your email address will not be published. Required fields are marked *