Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Sample WordPress Action and Filter Hooks
Code Samples for Hooks And Filters Class
<?php
/* Filter the title to add a page number if necessary.
------------------------------------------------------------------------------------ */
add_filter( 'wp_title', 'page_numbered_wp_title', 10, 2 );
function page_numbered_wp_title( $title, $sep ) {
global $paged, $page;
if ( is_feed() ) {
return $title;
}
// Add a page number if necessary.
if ( $paged >= 2 || $page >= 2 ) {
$title = "$title $sep " . sprintf( __( 'Page %s', 'my-text-domain' ), max( $paged, $page ) );
}
return $title;
}
/* Force One Column Layout for WordPress Dashboard Pages
------------------------------------------------------------------------------------ */
add_filter( 'screen_layout_columns', 'change_to_one_columns_pages_layout' );
function change_to_one_columns_pages_layout( $columns ) {
$columns['page'] = 1;
return $columns;
}
add_filter( 'get_user_option_screen_layout_page', 'change_screen_layout_pages' );
function change_screen_layout_pages() {
return 1;
}
/* Add a signature image to the end of a post
------------------------------------------------------------------------------------ */
add_filter( "the_content", "content_signature" );
function content_signature($content){
if(is_single()){
$content .= '<p><img src="'. 'http://demo.handsonwordpress.com/wp-content/uploads/2014/08/' .'author.jpeg" alt="Nicholas R. Batik" /></p>';
return $content;
}
}
/* Add default text to page, post, and custom post-type titles
------------------------------------------------------------------------------------ */
add_filter( 'default_title', 'wpatx_default_post_title', 10, 2 );
function wpatx_default_post_title( $title ) {
global $post_type;
switch ( $post_type ) {
case 'post':
$title = "Project: ";
break;
case 'page':
// $title = "<p>All my Pages start with this paragraph</p>";
break;
case 'daily':
// $title = "Daily Log - ".date("m/d/y");
$title = "Daily Log - ".date("Y-m-d");
break;
}
return $title;
}
/* Add default text to page, post, and custom post-type content
------------------------------------------------------------------------------------ */
add_filter( 'default_content', 'wpatx_editor_content', 10, 2 );
function wpatx_editor_content( $content, $post ) {
switch( $post->post_type ) {
case 'post':
$content = "This is my default content that I want to add to the beginning of every post: ";
break;
case 'page':
// $title = "<p>All my Pages start with this paragraph</p>";
case 'sources':
$content = 'your content';
break;
case 'stories':
$content = 'your content';
break;
case 'pictures':
$content = 'your content';
break;
default:
$content = 'your default content';
break;
}
return $content;
}
/* Change the footer to include automatically updating copyright date
------------------------------------------------------------------------------------ */
add_action('wp_footer', 'wpatx_footer_copyright', 100);
function wpatx_footer_copyright() {
$backtotop_text = '';
$copyYear = 2012;
$curYear = date('Y');
$creds_text = 'Copyright &copy; ' .
$copyYear . (($copyYear != $curYear) ? '-' . $curYear : '') .
' <a href="'.site_url().'">Austin WordPress Demo Site</a>. <br />All Rights Reserved &#xb7;' .
' Site design and development by' .
' <a href="http://PleiadesWebCenter.com" target="_blank">Nick Batik</a>' .
' using <a href="http://wordpress.org/" target="_blank">WordPress</a>' .
' &#xb7;' .
' <a href="'.site_url().'/wp-admin">Admin</a>';
$output = '<div class="creds">' . $creds_text . '</div>';
echo $output;
}
/* Replace strange text characters that are pasted into posts
------------------------------------------------------------------------------------ */
add_filter('the_content','filter_content_gremlins');
add_filter('the_excerpt','filter_content_gremlins');
add_filter('content_save_pre','filter_content_gremlins');
function filter_content_gremlins($content) {
$content_gremlins = array (
' ',
'“',
'”',
'—',
'’'
);
$content_zapgrems = array (
' ',
'"',
'"',
'',
'\''
);
$content=str_ireplace($content_gremlins,$content_zapgrems,$content);
return $content;
}
/* Remove blog comments autolinks
------------------------------------------------------------------------------------ */
remove_filter('comment_text', 'make_clickable', 9);
/* Add Private Notes to Posts
------------------------------------------------------------------------------------ */
// How to use: add [note]<em>Your note text</em>[/note]  
function sc_note( $atts, $content = null ) {  
     if ( current_user_can( 'publish_posts' ) )  
        return '<div class="note">'.$content.'</div>';  
    return '';  
}  
add_shortcode( 'note', 'sc_note' ); 
/* Add Custom body-classes based on categories
------------------------------------------------------------------------------------ */
add_filter('body_class','wpatx_section_style');
function wpatx_section_style($classes) {
global $post;
// Add the page or posts taxonomy terms to the body class
$terms = get_the_terms( $post->ID , 'category' );
// Loop over each item since it's an array
if ($terms) {
foreach( $terms as $term ) {
$classes[] = 'term-' . $term->slug;
unset($term);
}
if (in_array('term-category-item-1', $classes)) { $classes[] = 'background-green'; }
elseif (in_array('term-category-item-2', $classes)) { $classes[] = 'background-pink'; }
elseif (in_array('term-category-item-3', $classes)) { $classes[] = 'background-red'; }
elseif (in_array('term-category-item-4', $classes)) { $classes[] = 'background-teal'; }
else { $classes[] = 'background-red'; }
} else {
$classes[] = 'background-red';
}
// return the $classes array
return $classes;
}
/* Custom Shortcode to display active hooks and filters on a WordPress page or post
------------------------------------------------------------------------------------ */
add_shortcode('wpatx-hook-list', 'list_hooks');
function list_hooks( $filter = false ){
global $wp_filter;
global $user_ID;
// First check of user is logged in as admin
if( $user_ID ) {
if( current_user_can('level_10') ) {
$hooks = $wp_filter;
ksort( $hooks );
foreach( $hooks as $tag => $hook ) {
dump_hook($tag, $hook);
}
}
}
}
function dump_hook( $tag, $hook ) {
ksort($hook);
echo "<pre>>>>>>\t<strong>$tag</strong><br>";
foreach( $hook as $priority => $functions ) {
echo $priority;
foreach( $functions as $function )
if( $function['function'] != 'list_hook_details' ) {
echo "t";
if( is_string( $function['function'] ) )
echo $function['function'];
elseif( is_string( $function['function'][0] ) )
echo $function['function'][0] . ' -> ' . $function['function'][1];
elseif( is_object( $function['function'][0] ) )
echo "(object) " . get_class( $function['function'][0] ) . ' -> ' . $function['function'][1];
else
print_r($function);
echo ' (' . $function['accepted_args'] . ') <br>';
}
}
echo '</pre>';
}
@oneblackcrayon

This comment has been minimized.

Copy link

oneblackcrayon commented Sep 6, 2014

These are awesome!

Thank you for sharing.

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.