Wednesday, December 28, 2011

Display a thumbnail from a Youtube using a shortcode

 How to display a thumbnail from a Youtube using a shortcode

Copy the following code below and then paste it into the functions.php file under theme folder.

/*
    THis Shortcode will display youtube thumbnail on the wordpress blog.
    Usage:
    [youtube_thumb id="VIDEO_ID" img="0" align="left"]
    VIDEO_ID= Youtube video id
    img=0,1,2 or 3
    align= left,right,center
*/
function wp_youtube_video_thumbnail($atts) {
     extract(shortcode_atts(array(
          'id' => '',
          'img' => '0',
          'align'=>'left'
     ), $atts));
    $align_class='align'.$align;
   
}
add_shortcode('youtube_thumb', 'wp_youtube_video_thumbnail');

Now, you can use the shortcode. The shortcode accept 3 parameters: The video ID, the image size (0 for 480*360px, 1 for 120*90) and the image alignment.

[youtube_thumb id="8O9YA7PZZe0" img="0" align="center"]

Wednesday, November 30, 2011

Automatically remove p tags on images in wordpress

Automatically remove p tags on images in wordpress
Wordpress By default embed images in < p > tags. but if you don’t want those p tags to show, the following example display a great way to remove the < p > tags without having to chnage the WordPress core

Just paste the following code in theme functions.php file, and save the file.the
tags are gone now.





add_filter('the_content', 'remove_ptags_on_images');

Monday, October 31, 2011

Login form shortcode for WordPress

Here we learn how to displaying login forms on wordpress using a shortcode Paste the following code into functions.php file:

function sandip_login_form_shortcode() {
    if ( is_user_logged_in() )
        return '';

    return wp_login_form( array( 'echo' => false ) );
}

function sandip_add_shortcodes() {
    add_shortcode( 'sandip-login-form', 'sandip_login_form_shortcode' );
}

add_action( 'init', 'sandip_add_shortcodes' );

Once done, we can now use the shortcode as shown in the following example. just paste it on the post editor, where you want the login form to be displayed. [sandip-login-form]

Friday, September 30, 2011

Retrieve a remote page using WordPress

How to retrieve a remote page using WordPress


If you want to get the content,file info from a remote file for use it in wordpress instal then follow the step describe below.
Use the wp_remote_get() function (Or wp_remote_post() is you prefer using the POST method) to retrieve the desired url. The following example describe how to retrieve an url and display its content as well as the file info.

This code can be used anywhere on your template files.

$response = wp_remote_get( 'http://sandipdas.in/file.txt' );
if( is_wp_error( $response ) ) {
echo 'Something went wrong!';
} else {
echo 'Response:';
print_r( $response );

';
}

Wednesday, September 28, 2011

Display your latest Google+ update on WordPress

How to display your latest Google+ update on your WordPress blog


Just paste the following code where you want to display your latest Google+ update. Don't forget to put your Google+ ID on line 3.


include_once(ABSPATH.WPINC.'/rss.php');
$googleplus = fetch_feed("http://plusfeed.appspot.com/117307278436515842100

"); // Replace 117307278436515842100 by your own ID
echo '< a href="'; echo $googleplus->items[0]['link']; echo '">';
echo $googleplus->items[0]['summary'];


echo '';

Wednesday, August 24, 2011

Display a thumbnail of any website in wordpress

In WordPress we can display a thumbnail of any website. Wordpress have a service named mShots, Its allow us to get snapshots of any website. We can use a shotcode to easily display a snapshot of a given website.


To create the shortcode ,just paste the following code into functions.php file.



function wpr_snap($atts, $content = null) {
extract(shortcode_atts(array(
"snap" => 'http://s.wordpress.com/mshots/v1/',
"url" => 'http://www.sandipdas.in',
"alt" => 'My image',
"w" => '500', // width
"h" => '400' // height
), $atts));

$img = '' . $alt . '';
return $img;
}

add_shortcode("snap", "wpr_snap");


Now use the following shortcode to display any snaphot

[snap url="http://www.sandipdas.in" alt="description" w="500" h="400"]

Saturday, July 23, 2011

Display Custom Fields Outside Loop in WordPress

How to Display Custom Fields Outside The Loop in WordPress

When you need to display post-specific content for example a data for a specific post. Usually post specific contents are added via a custom field which can only be displayed inside a post loop.Here we will show you how you can display custom fields outside the loop.

Please check the following code:


global $wp_query;
$postid = $wp_query->post->ID;
echo get_post_meta($postid, 'Your-Custom-Field-data', true);
wp_reset_query();


You will need to make sure that you change the part where it says “Your-Custom-Field-data”.

Tuesday, July 5, 2011

Wordpress Tricks

Get Category Name By ID (WordPress)
eho get_cat_name( $cat_id );


Get Category Link By ID (WordPress)
echo get_category_link($cat_id);

Sunday, June 26, 2011

A Custom Read More in Wordpress

Display A Custom Read More in Wordpress

To display "A Custom Read More" all you need to do is replace your usual the_content template tag with the following code. So when you write a new post, create a new custom field with the key name "custom_more".

$custommore = get_post_meta($post->ID, 'custom_more', true);
if (!$custommore) { $custommore = 'Read More »'; }
the_content($custommore);

Wednesday, June 15, 2011

Detect visitor browser within WordPress

How to detect the visitor browser within WordPress

To ensure maximum cross-browser compatibility on the wordpress blog theme, We have to able to detect the browser used by the blog visitor. WordPress can detect client browser. I describe the proces below.

Paste the code below in your functions.php file:


add_filter('body_class','browser_body_class');
function browser_body_class($classes) {
global $is_lynx, $is_gecko, $is_IE, $is_opera, $is_NS4, $is_safari, $is_chrome, $is_iphone;

if($is_lynx) $classes[] = 'lynx';
elseif($is_gecko) $classes[] = 'gecko';
elseif($is_opera) $classes[] = 'opera';
elseif($is_NS4) $classes[] = 'ns4';
elseif($is_safari) $classes[] = 'safari';
elseif($is_chrome) $classes[] = 'chrome';
elseif($is_IE) $classes[] = 'ie';
else $classes[] = 'unknown';

if($is_iphone) $classes[] = 'iphone';
return $classes;
}


Now saved the file and the function will automatically add a CSS class to the body tag, as shown in the exemple below:

body class="home blog logged-in safari"

So now with the help of the code, you just have to take your stylesheet, and add some browser-specific styles!

Tuesday, May 24, 2011

Display Twitter-like “time ago” on WP

Display Twitter-like “time ago” on your WordPress blog

Twitter have a built-in function that display time from now, like “3 days ago” or “more than a month ago”.If you want to do same with your WordPress post use the following function.

Paste the following into your functions.php file:






function post_time_ago( $type = 'post' ) {
    $d = 'comment' == $type ? 'get_comment_time' : 'get_post_time';
    return human_time_diff($d('U'), current_time('timestamp')) . " " . __('ago');
}

Now, you can use the following function in your theme files:
 echo post_time_ago();

Sunday, May 8, 2011

How to modify size of embedded videos

Modify size of embedded videos in wordpress

 In WordPress, it is easy to embed videos on the blog. But sometimes, dealing with  the video sizes is a bit painful. Here is a solution how you can adjust the size of any embedded video using a filter in wordpress

 Just  paste the following code on the functions.php file.

 function custom_embed_defaults($embed_size){
    $embed_size['width'] = 650; // Adjust values to your needs
    $embed_size['height'] = 550;

    return $embed_size;
 }

 add_filter('embed_defaults', 'custom_embed_defaults');

Saturday, April 23, 2011

Display wordpress post titles with a custom length

Display wordpress post titles with a custom length

Display wordpress post titles with a custom length

 If you want to  display only the first X characters of your wordpress post title, I display here some hack for that.

 The first thing to do is to create the function. Open your functions.php file and paste this code:

  function ODD_title($char) {
         $title = get_the_title($post->ID);
         $title = substr($title,0,$char);
         echo $title;
 }

 Now, you use the function on your wordpress theme files. Just pass how many characters you want to display as a parameter. In the following example, only the first 30 chars of the title will be displayed:

ODD_title(20);

So with the following code you can display wordpress post titles with a custom length

Set a maximum word count on post title

Set a maximum word count on wordpress post titles

 If you want to be able to set a maximum word count of post titles so you dont have to think titles that are too long, the following code is help you to overcome that. Here I’ll show you how you can easily set a maximum word count on post titles.


 To apply this hack on wordpress post title , just paste the following ode in your functions.php file:

 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');

So with this code you can set a maximum word count on post title

Wednesday, March 30, 2011

How to remove WordPress admin bar from front end

In the following process you can  remove WordPress 3.1 admin bar

WordPress 3.1 introduced a new feature: the admin bar. It is very useful, but if you like to remove it. Please follow the instruction.

Paste the following piece of code into your functions.php file, save it to remove admin bar.

wp_deregister_script('admin-bar');
wp_deregister_style('admin-bar');
remove_action('wp_footer','wp_admin_bar_render',1000);

Thursday, March 24, 2011

How to replace words in your wordpress posts

Replace words in your wordpress posts

Let your Wordpress blog was named “portfolio” and you renamed it “myportfolio”. Then don’t edit your xxx posts to replace every single occurence! With this very useful code will do it for you, like a magic!


Just put words to replace in the array on line 4. Once done, paste the code into your function.php file, save the file, and you're ready!

function replace_text_wps($text){
   $replace = array(
       // 'WORD TO REPLACE' => 'REPLACE WORD WITH THIS'
       'portfolio' => 'myportfolio',
       'excerpt' => 'excerpt',
       'function' => 'function'
   );
   $text = str_replace(array_keys($
replace), $replace, $text);
   return $text;
}

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

Tuesday, February 1, 2011

How to increase WordPress memory limit

If ever this happen “Allowed memory size of xxx bytes exhausted”, then we need to increase WordPress memory limit. This can be done


open your wp-config file and paste the following code in it.

define('WP_MEMORY_LIMIT', '96M');

Monday, January 31, 2011

Set An Expiration Time For Wordpress Posts

Set An Expiration Time For Wordpress Posts:

Sometimes (like, when we are running a contest), We want to be able to publish a post and then automatically stop displaying it after a certain date. This may seem quite hard to do but using the custom field we can do this easyly.

To do this Edit the WP theme and replace the current WordPress loop with this "modified" code:


if (have_posts()) :
while (have_posts()) : the_post(); ?>
$expirationtime = get_post_custom_values('expiration');
if (is_array($expirationtime)) {
$expirestring = implode($expirationtime);
}

$secondsbetween = strtotime($expirestring)-time();
if ( $secondsbetween > 0 ) {
// For example...
the_title();
the_excerpt();
}
endwhile;
endif;



To create a post set to expire at a certain date and time, Like create a custom field. Specify expiration as a key and your date and time as a value (with the format mm/dd/yyyy 00:00:00). The post will not show up after the time on that stamp


**This code does not remove or unpublish post, It just prevents it from being displayed in the loop.

Thursday, January 6, 2011

How to make capital letters with CSS style

You can capitalize words and letters with this css.

.classname{
text-transform: capitalize;
}

This is make Abcde to Abcde

.classname{
text-transform: uppercase;
}

This is make abcde to ABCDE

I just got my #domain @BigRock. Get upto 25% off with my personalized coupon link Get upto 25% off