
The C # offers various basic and advanced C # concepts. C # language is both for beginners and professionals. C # is a .Net Framework programming language.
The C # consist all C # themes, control instructions,
The C # offers various basic and advanced C # concepts. C # language is both for beginners and professionals. C # is a .Net Framework programming language.
The C # consist all C # themes, control instructions,
The snippet below will let you have the author bio excerpt.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
<?php // Paste the code below to the functions.php file inside the theme directory you're using. function author_excerpt (){ $word_limit = 20; // Limit the number of words $more_txt = 'read more about:'; // The read more text $txt_end = '...'; // Display text end $authorName = get_the_author(); $authorUrl = get_author_posts_url( get_the_author_meta('ID')); $authorDescription = explode(" ", get_the_author_meta('description')); $displayAuthorPageLink = count($authorDescription) > $word_limit ? $txt_end.' '.$more_txt.' <a href="'.$authorUrl.'">'.$authorName.'</a>' : '' ; $authorDescriptionShort = array_slice($authorDescription, 0, ($word_limit)); return (implode($authorDescriptionShort, ' ')).$displayAuthorPageLink; } ?> <?php // Add this code snippet to the single.php file where you like to display the author bio excerpt. if (function_exists('author_excerpt')){echo author_excerpt();} ?> |
The following snippet will let you disable self trackbacks.
1 2 3 4 5 6 7 8 |
<?php function disable_self_ping( &$links ) { foreach ( $links as $l => $link ) if ( 0 === strpos( $link, get_option( 'home' ) ) ) unset($links[$l]); } add_action( 'pre_ping', 'disable_self_ping' ); ?> |
The following snippet has to be added to functions.php and it will remove the “wrong password” screen shake.
1 2 3 4 5 6 |
<?php function my_login_head() { remove_action('login_head', 'wp_shake_js', 12); } add_action('login_head', 'my_login_head'); ?> |
The following snippet will let you show the admin bar to administrators.
1 2 3 4 5 6 |
<?php // Snippet code goes here! if (!current_user_can('administrator')) : show_admin_bar(false); endif; ?> |
To have the QR-code rendered for every article, just insert the following snippet inside the WordPress loop.
1 2 3 4 5 |
<img src="http://api.qrserver.com/v1/create-qr-code/?size=500x500&data=" <?php the_permalink() ?> " alt="QR Code for <?php the_title_attribute(); ?> "/> |
The snippet below will allow you to disable plugin updates.
1 2 3 4 |
<?php remove_action( 'load-update-core.php', 'wp_update_plugins' ); add_filter( 'pre_site_transient_update_plugins', create_function( '$a', "return null;" ) ); ?> |
The snippet below will let you list all categories with posts.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
<?php //for each category, show all posts $cat_args=array( 'orderby' => 'name', 'order' => 'ASC' ); $categories=get_categories($cat_args); foreach($categories as $category) { $args=array( 'showposts' => -1, 'category__in' => array($category->term_id), 'caller_get_posts'=>1 ); $posts=get_posts($args); if ($posts) { echo '<p>Category: <a href="' . get_category_link( $category->term_id ) . '" title="' . sprintf( __( "View all posts in %s" ), $category->name ) . '" ' . '>' . $category->name.'</a> </p> '; foreach($posts as $post) { setup_postdata($post); ?> <p><a href=" <?php the_permalink() ?> " rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?> "> <?php the_title(); ?> </a></p> <?php } // foreach($posts } // if ($posts } // foreach($categories ?> |
The following snippet will let you override WordPress and site URL.
1 2 3 4 5 |
<?php // Define wp home and site url define('WP_HOME','http://production.whateversite.com'); define('WP_SITEURL','http://production.whateversite.com'); ?> |
The following snippet will let you get the first link in post.
1 2 3 4 5 6 7 8 9 10 11 |
function get_content_link( $content = false, $echo = false ) { if ( $content === false ) $content = get_the_content(); $content = preg_match_all( '/hrefs*=s*["']([^"']+)/', $content, $links ); $content = $links[1][0]; if ( empty($content) ) { $content = false; } return $content; } |
The snippets checks that if a sidebar of your website having any widget areas in it or not.
1 2 3 4 5 6 7 |
function is_sidebar_active($index) { global $wp_registered_sidebars; $widgetcolums = wp_get_sidebars_widgets(); if ($widgetcolums[$index]) return true; return false; } |
The following snippet will let you redirect your author archive link to your website’s about us page.
1 2 3 4 |
add_filter( 'author_link', 'my_author_link'); function my_author_link() { return home_url( 'about' ); } |
The following snippet will let you change image path only.
1 |
UPDATE wp_posts SET post_content = REPLACE (post_content, 'src="http://www.oldsiteurl.com', 'src="http://yourcdn.newsiteurl.com'); |
The following snippet will let you add thumbnails in manage posts/pages list.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 |
/****** Add Thumbnails in Manage Posts/Pages List ******/ if ( !function_exists('AddThumbColumn') && function_exists('add_theme_support') ) { // for post and page add_theme_support('post-thumbnails', array( 'post', 'page' ) ); function AddThumbColumn($cols) { $cols['thumbnail'] = __('Thumbnail'); return $cols; } function AddThumbValue($column_name, $post_id) { $width = (int) 35; $height = (int) 35; if ( 'thumbnail' == $column_name ) { // thumbnail of WP 2.9 $thumbnail_id = get_post_meta( $post_id, '_thumbnail_id', true ); // image from gallery $attachments = get_children( array('post_parent' => $post_id, 'post_type' => 'attachment', 'post_mime_type' => 'image') ); if ($thumbnail_id) $thumb = wp_get_attachment_image( $thumbnail_id, array($width, $height), true ); elseif ($attachments) { foreach ( $attachments as $attachment_id => $attachment ) { $thumb = wp_get_attachment_image( $attachment_id, array($width, $height), true ); } } if ( isset($thumb) && $thumb ) { echo $thumb; } else { echo __('None'); } } } // for posts add_filter( 'manage_posts_columns', 'AddThumbColumn' ); add_action( 'manage_posts_custom_column', 'AddThumbValue', 10, 2 ); // for pages add_filter( 'manage_pages_columns', 'AddThumbColumn' ); add_action( 'manage_pages_custom_column', 'AddThumbValue', 10, 2 ); |
The following snippet will let you checks if the visitor is from mobile device or not.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
<?php // Add to functions.php // version proof, checks if the visitor is from a mobile device function muneeb_wp_is_mobile() { if ( function_exists( 'wp_is_mobile' ) ) return wp_is_mobile(); //code from wp_is_mobile function, wp_is_mobile() is located in wp-includes/vars.php version 3.4 static $is_mobile; if ( isset($is_mobile) ) return $is_mobile; if ( empty($_SERVER['HTTP_USER_AGENT']) ) { $is_mobile = false; } elseif ( strpos($_SERVER['HTTP_USER_AGENT'], 'Mobile') !== false // many mobile devices (all iPhone, iPad, etc.) || strpos($_SERVER['HTTP_USER_AGENT'], 'Android') !== false || strpos($_SERVER['HTTP_USER_AGENT'], 'Silk/') !== false || strpos($_SERVER['HTTP_USER_AGENT'], 'Kindle') !== false || strpos($_SERVER['HTTP_USER_AGENT'], 'BlackBerry') !== false || strpos($_SERVER['HTTP_USER_AGENT'], 'Opera Mini') !== false ) { $is_mobile = true; } else { $is_mobile = false; } return $is_mobile; } // Use anywhere if ( muneeb_wp_is_mobile() ){ //do mobile stuff here } ?> |
The following snippet will let you temporarily close down your site for visits while your website can still be accessed by admins resulting in letting you do the changes that you are planning for
1 2 3 4 5 6 7 8 |
// Temp Maintenance - with http response 503 (Service Temporarily Unavailable) // This will only block users who are NOT an administrator from viewing the website. function wp_maintenance_mode(){ if(!current_user_can('edit_themes') || !is_user_logged_in()){ wp_die('Maintenance, please come back soon.', 'Maintenance - please come back soon.', array('response' => '503')); } } add_action('get_header', 'wp_maintenance_mode'); |
The snippet below will let you add featured images to WordPress feeds.
1 2 3 4 5 6 7 8 9 |
function rss_post_thumbnail($content) { global $post; if(has_post_thumbnail($post->ID)) { $content = get_the_post_thumbnail($post->ID) . $content; } return $content; } add_filter('the_excerpt_rss', 'rss_post_thumbnail'); add_filter('the_content_feed', 'rss_post_thumbnail'); |
The following snippet will let you set minimal comment limit in WordPress.
1 2 3 4 5 6 7 8 9 |
add_filter( 'preprocess_comment', 'minimal_comment_length' ); function minimal_comment_length( $commentdata ) { $minimalCommentLength = 20; if ( strlen( trim( $commentdata['comment_content'] ) ) < $minimalCommentLength ) { wp_die( 'All comments must be at least ' . $minimalCommentLength . ' characters long.' ); } return $commentdata; } |
The following snippet will allow PHP in WordPress text widget.
1 2 3 4 5 6 7 8 9 10 |
function php_text($text) { if (strpos($text, '<' . '?') !== false) { ob_start(); eval('?' . '>' . $text); $text = ob_get_contents(); ob_end_clean(); } return $text; } add_filter('widget_text', 'php_text', 99); |
The following snippet will let you remove URL from comment form.
1 2 3 4 5 6 |
add_filter('comment_form_default_fields', 'unset_url_field'); function unset_url_field($fields){ if(isset($fields['url'])) unset($fields['url']); return $fields; } |
The snippet below will let you display some random post.
1 2 3 4 5 6 7 8 |
<ul> <?php $rand_posts = get_posts('numberposts=5&orderby=rand'); foreach( $rand_posts as $post ) : ?> <li><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></li> <?php endforeach; ?> </ul> |
The following snippet will let you customize the default avatar.
1 2 3 4 5 6 7 8 9 |
<?php // Create a new options in the WordPress admin function newgravatar ($avatar_defaults) { $myavatar = get_bloginfo('template_directory') . '/images/avatary.jpg'; $avatar_defaults[$myavatar] = "Avatary"; return $avatar_defaults; } add_filter( 'avatar_defaults', 'newgravatar' ); ?> |
The snippet below will let you create a custom post template.
1 2 3 4 5 6 |
<?php /* Single Post Template: [New Template Name] Description: You always want to include a description. Be organized! */ ?> |
The snippet below will let you limit number of post revisions.
1 2 3 4 5 6 |
<?php // Maximum of 3 revisions define('WP_POST_REVISIONS', 3); // Disable revisions define('WP_POST_REVISIONS', false); ?> |
Adding the following snippet to your theme’s functions.php file, you will be able to set a maximum word count on post titles.
1 2 3 4 5 6 7 |
function maxWord($title){ global $post; $title = $post->post_title; if (str_word_count($title) >= 10 ) //set this to the maximum number of words wp_die( __('Error: your post title is over the maximum word count.') ); } add_action('publish_post', 'maxWord'); |
Snippet Source/Credit: Pippin
The following snippet display the WordPress excerpt dynamic length.
1 2 3 4 5 6 7 8 9 10 11 12 |
function ms_rp_excerpt($limit) { $excerpt = explode(' ', get_the_excerpt(), $limit); if (count($excerpt) >= $limit) { array_pop($excerpt); $excerpt = implode(' ', $excerpt).'...'; } else { $excerpt = implode(' ', $excerpt); } $excerpt = preg_replace('`\[[^\]]*\]`', '', $excerpt); return $excerpt; } |
Snippet Source/Credit: Snipplr
The following snippet will let you delete both width and height attributes from image tag.
1 2 3 4 5 |
<?php $image = wp_get_attachment_image_src( get_post_thumbnail_id( $post->ID ), 'thumbnail' ); if ($image) : ?> <img src="<?php echo $image[0]; ?>" alt="" /> <?php endif; ?> |
Snippet Source/Credit: Stackexchange
Using the following snippet in your theme’s functions.php file, you will be able to show admin only for admins. Just copy and paste it and you are done.
1 2 3 |
if (!current_user_can('manage_options')) { add_filter('show_admin_bar', '__return_false'); } |
Snippet Source/Credit: Jeff Starr
Using the following snippet, you will be able to customize comment count text in WordPress.
1 2 3 |
<?php echo get_comments_number_text( "No comments, yet", "One Lonely Comment", "% Happy Comments"); ?> |
Snippet Source/Credit: WPSnipp
The following snippet will let you deregister all in one event calendar in WordPress.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
// clean up unwanted JS scripts function my_print_scripts(){ if ( !is_admin() ) { wp_deregister_script('jquery'); wp_deregister_script('ai1ec_requirejs'); } } add_action('wp_print_scripts', 'my_print_scripts'); // clean up unwanted CSS function my_print_css() { if ( !is_admin() ) { wp_deregister_style('ai1ec-general'); wp_deregister_style('ai1ec-event'); wp_deregister_style('ai1ec-calendar'); } } add_action('wp_print_styles', 'my_print_css'); |
Snippet Source/Credit: Ditscheri.com
The following snippet will let you disable WordPress comments completely.
1 |
<?php return false; ?> |
Snippet Source/Credit: aotearoawebdesign.co.nz
When you delete a post or a page or a comment, it goes in a Trash instead of being removed permanently. Using the following snippet will ease you job of deletion and will tell WordPress to automatically delete the trash itself. Simply copy and paste the following lines of code in your wp-config,php file and you are done.
1 |
define('EMPTY_TRASH_DAYS', 1); |
Note: Make sure that you do replace 1 by X to empty spam comments automatically every X days.
Adding this snippet to your theme’s functions.php file will let you show content only for logged in users.
1 2 3 4 5 6 7 8 9 10 |
<?php add_shortcode("hide","hide_shortcode"); function hide_shortcode($x,$text=null){ if(!is_user_logged_in()){ return "You have to been registered and logged in to see this content"; }else{ return do_shortcode($text); } } ?> |
Snippet Source/Credit: Snipplr
Using the following snippet, you will be able to add a logo to a template.
1 |
<img src="<?php echo get_stylesheet_directory_uri()?>/images/logo.png" alt="My Logo"> |
Snippet Source/Credit: WPSnipp
Adding the following snippet in your theme’s functions.php file, you will be able to count retweets in full text.
1 2 3 4 5 6 7 8 9 10 |
function tweetCount($url) { $content = file_get_contents("http://api.tweetmeme.com/url_info?url=".$url); $element = new SimpleXmlElement($content); $retweets = $element->story->url_count; if($retweets){ return $retweets; } else { return 0; } } |
Once saved copy and paste the following lines of snippets in your theme’s single.php file:
1 2 |
$rt = tweetCount(get_permalink()); echo "Retweeted ".$rt." times."; |
Snippet Source/Credit: WPRecipes
Using the following snippet, you will be able to automatically link Twitter usernames in WordPress posts.
1 2 3 4 5 |
function content_twitter_mention($content) { return preg_replace('/([^a-zA-Z0-9-_&])@([0-9a-zA-Z_]+)/', "$1@$2", $content); } add_filter('the_content', 'content_twitter_mention'); add_filter('comment_text', 'content_twitter_mention'); |
Snippet Source/Credit: Paulund.co.uk
Powered by WordPress & Theme by Anders Norén