Tuesday, August 28, 2012

How to automatically add rel=”lightbox” to all images embedded in a post

The well known jQuery plugin Lightbox is a very simple way to open images in fancy full-screen boxes. It is very easy to use, but you have to add a rel=”lightbox” attribute to each image you want to open in a lightbox. Here’s a cool code snippet to automatically add the rel=”lightbox” attribute to all images embedded in your posts.
 
Paste the following code snippet in your functions.php file. 
Once done, a rel="lightbox" attribute will be automatically 
added to all images embedded in a post.
 
 function my_addlightboxrel($content) {   
      global $post;
      $pattern ="/<a(.*?)href=('|\")(.*?).(bmp|gif|jpeg|jpg|png)('|\")(.*?)>/i";         
  $replacement = '<a$1href=$2$3.$4$5 rel="lightbox" 
                title="'.$post->post_title.'"$6>';        
  $content = preg_replace($pattern, $replacement, $content);       
  return $content;
  }
add_filter('the_content', 'my_addlightboxrel');

How to easily add shortcuts to WordPress toolbar

As with many things, WordPress makes it easy to customize the Toolbar, using the add_node() functions. In this recipe, I’m going to show you how to add a shortcut to WordPress toolbar.

function custom_toolbar_link($wp_admin_bar) {
$args = array( 'id' => Help,
'title' => 'Help',
'href' => 'http://h20bikash.blogspot.com/',
'meta' => array( 'class' =>Help,
'title' => 'Help' ) );
$wp_admin_bar->add_node($args);
}
add_action('admin_bar_menu', 'custom_toolbar_link', 999);

How to add .pdf support to the WordPress media manager

By default, the built-in WordPress media manager allows you to filter media by three types: images, audio and video. But if you work a lot with .pdf files, you may need to add an option to filter .pdf files from the media manager. Here is a simple code snippet to do it.

Paste this code into your functions.php file. Save the file, and you're done.
 
function modify_post_mime_types( $post_mime_types ) {
// select the mime type, here: 'application/pdf'
// then we define an array with the label values
$post_mime_types['application/pdf'] = array( __( 'PDFs' ), __( 'Manage PDFs' ), _n_noop( 'PDF (%s)', 'PDFs (%s)' ) );
// then we return the $post_mime_types variable
return $post_mime_types;
} // Add Filter Hook
add_filter( 'post_mime_types', 'modify_post_mime_types' );

How to automatically empty trash on a daily basis

Simply open your wp-config.php file (Located at the root of your WordPress install) and add the following line of code:

define('EMPTY_TRASH_DAYS', 1);

How to remove the url field from WordPress comment form

In some cases, you may not need or want that people who comment your posts can leave their website url. Here is a simple code snippet to remove the url field from WordPress comment form.

Paste the code below into your functions.php file. Once saved, the url field will be removed from your comment form.
 
function remove_comment_fields($fields) {  
    unset($fields['url']); 
     return $fields;  
}
  add_filter('comment_form_default_fields','remove_comment_fields');

Monday, August 13, 2012

Complete flexible Project Task Management System


This WordPress plugin is a complete flexible Project Task Management System. It's a very easy to use, but yet a powerful plugin. 

Using jQuery UI datepicker with WordPress custom post meta

if(is_admin()) {
wp_enqueue_script('jquery-ui-datepicker');
wp_enqueue_script('custom-js', get_template_directory_uri().'/js/custom-js.js');
wp_enqueue_style('jquery-ui-custom', get_template_directory_uri().'/css/jquery-ui-custom.css');
}
function my_admin_footer() {
?>
<script type="text/javascript">
jQuery(document).ready(function(){
jQuery('#registration_lastdate').datepicker({
dateFormat : 'd M yy'
});

jQuery('#training_date').datepicker({
dateFormat : 'd M yy'
});
});
</script>
<?php
}
add_action('admin_footer', 'my_admin_footer');

Monday, August 6, 2012

Custom page links without or with a list


$args = array(
'orderby' => 'post_title',
'order' => 'ASC',
'post_type' => 'page',
//'post_parent'=> '35',
'caller_get_posts' => 1
);

$pages = get_posts($args);
foreach($pages as $page) {
$out .= '<div class="bluecategory_wrapper">';
$out .= '<a href="'.get_permalink($page->ID).'" title="'.wptexturize($page->post_title).'">
<div class="bluecategory_left"></div><div class="bluecategory_bg">'.wptexturize($page->post_title).'</div>
<div class="bluecategory_right"></div></a></div>';
}
$out = $out;
echo $out;

Thursday, August 2, 2012

Change WordPress “from” email header

By default, the default WordPress email adress looks like WordPress@yoursitename.com. Want to use your real email and username instead?

function res_fromemail($email) {
$wpfrom = get_option('admin_email');
return $wpfrom;
}
function res_fromname($email){
$wpfrom = get_option('blogname');
return $wpfrom;
}
add_filter('wp_mail_from', 'res_fromemail');
add_filter('wp_mail_from_name', 'res_fromname');

Sending users email on post publish

function send_email_publish_post($post_ID) 
{ global $wpdb; $permalink = get_permalink( $post_ID ); 
 $query = "SELECT * FROM {$wpdb->users}"; 
 $users = $wpdb->get_results( $query);
 foreach($users as $user) 

$permalink = get_permalink( $post_ID ); 
$message="A New Post published. You can view the post details from the link ".$permalink." This is an auto generated notification. Please do not reply to it."; 
wp_mail( $user->user_email, "A New Post published", $message); 
 } 
return $post_ID; 

add_action('wp_publish_post','send_email_publish_post');

Wednesday, August 1, 2012

add theme support

add_theme_support( $feature );
$feature
  • 'post-formats'
  • 'post-thumbnails'
  • 'custom-background'
  • custom-header'
  • 'automatic-feed-links'
  • 'menus' 

Enabling Support for Post Thumbnails

add_theme_support( 'post-thumbnails' ); 

Default Usage 

if ( has_post_thumbnail() ) { the_post_thumbnail(); } 

Thumbnail Sizes 

the_post_thumbnail(); // without parameter -> Thumbnail the_post_thumbnail('thumbnail'); // Thumbnail (default 150px x 150px max) the_post_thumbnail('medium'); // Medium resolution (default 300px x 300px max) the_post_thumbnail('large'); // Large resolution (default 640px x 640px max) the_post_thumbnail( array(100,100) ); // Other resolutions 

Example of a new Post Thumbnail size named "category-thumb". 

add_image_size( 'category-thumb', 300, 9999 ); //300 pixels wide (and unlimited height)
Here is an example of how to use this new Post Thumbnail size in theme template files. the_post_thumbnail( 'category-thumb' );

Custom Backgrounds

add_theme_support( 'custom-background' ); 

Note that you can add default arguments using:
$defaults = array( 'default-color' => '', 
'default-image' => '', 
'wp-head-callback' => '_custom_background_cb', 
'admin-head-callback' => '', 'admin-preview-callback' => '' ); 

add_theme_support( 'custom-background', $defaults ); 

Example  
An example using default '#000000' background color with 'background.jpg' background image:

$args = array( 'default-color' => '000000',
 'default-image' => get_template_directory_uri() . '/images/background.jpg', ); 
add_theme_support( 'custom-background', $args );