Užitečné hooks & filters

Modify post title when some string exists in the post content
Add lock icon if there is a member shortcode present in the post content.
Without *$post->ID == $id* not only post title is changed but also menu items and next/previous links.


function modify_title($title, $id) {
    global $post;
    $content = $post->post_content;
    if(strpos($content, '[member]') AND $post->ID == $id) {
        $theme_dir = get_bloginfo('stylesheet_directory');
        $lock = ' <img src="'.$theme_dir.'/images/lock.gif" border="0"/>';
        $new_title = $title.$lock;
        return $new_title;
    } else {
        return $title;
    }
}
add_filter('the_title', 'modify_title', 10, 2);

Add extra contact methods to user profiles

By default, WordPress allow users to enter an AIM name on their profile, but no Facebook and no Twitter names!
In order to add more contact methods to user profile, simply paste this hook in your functions.php file. In this example it will add Facebook and Twitter, but it can be used for any website or service you need.


function my_user_contactmethods($user_contactmethods){
  $user_contactmethods['twitter'] = 'Twitter Username';
  $user_contactmethods['facebook'] = 'Facebook Username';

  return $user_contactmethods;
}

add_filter('user_contactmethods', 'my_user_contactmethods');

Automatically enable threaded comments

By default, WordPress do not enable threaded comments. If you want/need to change this, here is a handy code snippet to paste in your functions.php file:


function enable_threaded_comments(){
 if (!is_admin()) {
  if (is_singular() AND comments_open() AND (get_option('thread_comments') == 1))
   wp_enqueue_script('comment-reply');
  }
}

add_action('get_header', 'enable_threaded_comments');

Automatically replace words in your posts


function replace_text_wps($text){
    $replace = array(
        // 'WORD TO REPLACE' => 'REPLACE WORD WITH THIS'
        'wordpress' => '<a href="#">wordpress</a>',
        'excerpt' => '<a href="#">excerpt</a>',
        'function' => '<a href="#">function</a>'
    );
    $text = str_replace(array_keys($replace), $replace, $text);
    return $text;
}

add_filter('the_content', 'replace_text_wps');
add_filter('the_excerpt', 'replace_text_wps');

Quick maintenance mode


function cwc_maintenance_mode() {
    if ( !current_user_can( 'edit_themes' ) || !is_user_logged_in() ) {
        wp_die('Maintenance, please come back soon.');
    }
}
add_action('get_header', 'cwc_maintenance_mode');

Zdroj »

Přidání boxu pro excerpt (stručný výpis) i do stránek


function enable_page_excerpt() {
add_post_type_support( 'page', 'excerpt' );
}
add_action('init', 'enable_page_excerpt');

Deaktivace HTML v komentářích


add_filter('pre_comment_content', 'wp_specialchars');

„10 Useful WordPress Hook Hacks »“:“http://www.smashingmagazine.com/2009/08/10-useful-wordpress-hook-hacks/

Pexeso a další hry pro WordPress

Tři hry od stejného autora. Velmi použitelné, bohatě konfigurovatelné.

MyPuzzle Find The Pair (pexeso)
http://wordpress.org/plugins/mypuzzle-find-the-pair-a-memory-game/
http://blog.mypuzzle.org/find-the-pair-for-wordpress/

MyPuzzle Jigsaw
http://wordpress.org/plugins/mypuzzle-jigsaw/
http://mypuzzle.org/jigsaw/wordpress.html

MyPuzzle Sliding
http://wordpress.org/plugins/mypuzzle-sliding/
http://mypuzzle.org/sliding/wordpress.html

Nastavení jiného jazyka pro web (frontend) a jiného pro administraci

User Language Switch
https://wordpress.org/plugins/user-language-switch/

Allows backend users to set the language displayed in the back-end and front-end of your site. It also allows to translate pages and posts. Define one language for admin and the other for theme. Don’t define WPLANG in your wp-config.php file.

A nebo:


// setup one language for admin and the other for theme
// must be called before load_theme_textdomain()
function set_my_locale($locale) {
	
	$locale = ( is_admin() ) ? "cz_CS" : "en_US";
	setlocale(LC_ALL, $local );
	return $locale;

}
add_filter( 'locale', 'set_my_locale' );

Jak vytvořit a přidat vlastní shortcodes

Zakódování e-mailové adresy


function cwc_mail_shortcode( $atts , $content=null ) {
    for ($i = 0; $i < strlen($content); $i++) $encodedmail .= "&#" . ord($content[$i]) . ';'; 
    return '<a href="mailto:'.$encodedmail.'">'.$encodedmail.'</a>';
}
add_shortcode('mailto', 'cwc_mail_shortcode');

Použití shortcode:

[mailto]email@yourdomain.com[/mailto]

Obsah pouze pro přihlášené uživatele


function cwc_member_check_shortcode( $atts, $content = null ) {
	 if ( is_user_logged_in() && !is_null( $content ) && !is_feed() )
		return $content;
	return '';
}

add_shortcode( 'member', 'cwc_member_check_shortcode' );

Použití shortcode:

[member]This text will be only displayed to registered users.[/member]

Zobrazení PDF v iframe


function cwc_viewpdf($attr, $url) {
    return '<iframe src="https://docs.google.com/viewer?url=' . $url . '&embedded=true" style="width:' .$attr['width']. '; height:' .$attr['height']. ';" frameborder="0">Your browser should support iFrame to view this PDF document</iframe>';
}
add_shortcode('embedpdf', 'cwc_viewpdf');

Použití shortcode


[embedpdf width="600px" height="500px"]http://infolab.stanford.edu/pub/papers/google.pdf[/embedpdf]

Volání shortcode v šabloně (mimo editor)


echo do_shortcode('[jmeno_shortcode]');

viz dokumentace

Znepřístupnění shortcodes

There might be some cases that you need the shortcodes of the text to be omitted. When that time comes, strip_shortcodes() is your friend.

Let’s say that you need to strip shortcodes in the homepage but let them run in other content pages:


<?php
function remove_shortcode_from_index( $content ) {
    if ( is_home() )
        $content = strip_shortcodes( $content );
    return $content;
}
add_filter( 'the_content', 'remove_shortcode_from_index' );
?>

Jak vytvořit vlastní shortcode (musilda.cz) »
10 super useful WordPress shortcodes »
Mastering WordPress shortcodes »
8 fun & useful shortcode functions for WordPress »

Používání uživatelských polí v šablonách

Using Custom Fields In Your WordPress Template »


// key = název (klíč) uživatelského pole
// $post->ID lze použít pouze v loopu
if($data = get_post_meta($post->ID, 'key', true)) {
    echo $data;
}

// v případě pole
if($data = get_post_meta($post->ID, 'key')) {
    echo $data[0];
    echo $data[1];
}

Detailed example for the usage of the add_post_meta, delete_post_meta, update_post_meta, and get_post_meta functions.

Úprava RSS kanálu pomocí hooku

Action hooks and filter hooks »

Vysvětlení a příklady »

All plugin hooks in WordPress 3.5 »


###################################################################################
# tento hook prida do RSS thumbnail, odstrani iframe a urizne obsah za tagem "more"
###################################################################################

function improveRSS($content) {
global $post;
if ( has_post_thumbnail( $post->ID ) ){
$content = '' . get_the_post_thumbnail( $post->ID, 'rss-thumbnail', array( 'style' => 'margin:0; border: 10px solid #202020' ) ) . '' . $content;
}

$content = preg_replace( '/<iframe(.*)\/iframe>/is', '', $content );

$teaser = preg_split( '/<span id\="(more\-\d+)"><\/span>/', $content );
$readmore = '<a href="'.get_permalink().'">Více na webu »</a>';
$content = $teaser[0].$readmore;
return $content;
}

add_filter('the_excerpt_rss', 'improveRSS');
add_filter('the_content_feed', 'improveRSS');

Automatické stažení náhledů videa z videoserverů

Video Thumbnails
http://wordpress.org/extend/plugins/video-thumbnails/

Video Thumbnails makes it easy to automatically display video thumbnails in your template. When you publish a post, this plugin will find the first video embedded and locate the thumbnail for you. Thumbnails can be saved to your media library and set as a featured image automatically. There’s even support for custom post types and custom fields!

Adresy náhledových obrázků na YouTube:

img.youtube.com/vi/VIDEO_ID/#.jpg

# 1,2,3 – 120×90 px
# 0 – 480×360 px

(Použito např. na abeceda-anglictiny.cz