Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Example of Use cases for Filters
<?php
/**
* code #1 - update the post excerpt character length
* @param integer
* @return integer
*/
function my_excerpt_length($length)
{
return 65;
}
add_filter('excerpt_length', 'my_excerpt_length');
/**
* code #2 - replace 'Read more' text with new string same URL
* @param string
* @return string
*/
function new_excerpt_more($text)
{
return ' [...]';
}
add_filter('excerpt_more', 'new_excerpt_more');
/**
* code #3 - remove automatic <p> tags in Wordpress editor
*/
remove_filter('the_content', 'wpautop');
/**
* code #4 - append 'first' class onto the 1st <p> element in each post
* @param string
* @return string
*/
function first_paragraph($content){
global $post;
if ($post->post_type == "post") {
return preg_replace('/&lt;p([^>]+)?>/', '&lt;p$1 class="first">', $content, 1);
}
else {
return $content;
}
}
add_filter('the_content', 'first_paragraph');
/**
* code #5 - add & remove custom Wordpress social contacts from user profile
* @param array
* @return array
*/
function cusom_social_contact_links($contacts) {
// Adding
$contacts['twitter'] = 'Twitter';
$contacts['youtube'] = 'YouTube';
$contacts['dribbble'] = 'Dribbble';
// Removing
unset($contacts['yim']);
unset($contacts['aim']);
unset($contacts['jabber']);
return $contacts;
}
add_filter('user_contactmethods','cusom_social_contact_links', 10, 1);
/**
* code #6 - Remove the URL Field from the WordPress Comment Form
* @param array
* @return array
*/
function remove_comment_fields($fields) {
unset($fields['url']);
return $fields;
}
add_filter('comment_form_default_fields','remove_comment_fields');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.