Learn PHP, PHP Tutorial, PHP Code, MySQL, Javascript, JQuery, Ajax

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

Friday, May 18, 2012

How to automatically create meta description from content in wordpress

How to automatically create meta description from content in wordpress


WordPress by default don't add a "meta description" tag to into blog. Its not necessary, but some SEO experts insists that this tag is important for our website SEO purpose. So isn't it a great idea about generating a "meta description" using the post content? In below I have describe a useful code which can do this purpose easily.
For this please paste the following code into the functions.php file under theme folder

function create_meta_desc() {
global $post;
if (!is_single()) { return; }
$meta = strip_tags($post->post_content);
$meta = strip_shortcodes($post->post_content);
$meta = str_replace(array("\n", "\r", "\t"), ' ', $meta);
$meta = substr($meta, 0, 125);
echo "< meta name='description' content='$meta' />";
}
add_action('wp_head', 'create_meta_desc');
The code is ready now

Tuesday, May 8, 2012

How to disable plugin updates on WordPress


How to disable plugin updates on your WordPress blog

Wordpress By default automatically checks if the plugins updates are available so after checking if its find new update available it will ask you to install it. Plugin update is useful in most cases, but when you may not want them to updates plugins, like if you modified a plugin especially for perticular website. So I describe an easy way to disable plugin updates on any WordPress blog, Please check

So simple: Just paste the following code into functions.php and save it. Thats all

remove_action( 'load-update-core.php', 'wp_update_plugins' );
add_filter( 'pre_site_transient_update_plugins', create_function( '$a', "return null;" ) );

Thursday, April 5, 2012

Prevent WordPress to compress your jpg images

Prevent WordPress to compress your jpg images

Here I describe how you Prevent WordPress to compress your jpg images because by default, WordPress compress your uploaded jpg images

To do this just Open functions.php file from your theme folder and paste the following code below in the functions.php file:

add_filter('jpeg_quality', function($arg){return 100;});

Now Save the file.

 

Saturday, March 10, 2012

Replace excerpt ellipsis with post permalink

This is very useful tricks to replace the ellipsis [...] from the excerpt with a permalink to the post in wordpress.

Just paste the code below into your functions.php file in theme folder of wordpress. After saved, this tip will be applied to the wordpress blog.



function replace_excerpt($content) {
       return str_replace('[...]',
               '... <div class="more-link"><a href="'. get_permalink() .'">Continue Reading</a></div>',
               $content
       );
}
add_filter('the_excerpt', 'replace_excerpt');

Tuesday, March 6, 2012

Embed a page into another page in Wordpress

Embed a page into another page in Wordpress

Just paste the following code within the loop and make sure that you update page ID on line 1!


<?php $recent = new WP_Query("page_id=**ID**"); while($recent->have_posts()) : $recent->the_post();?>
       <h3><?php the_title(); ?></h3>
       <?php the_content(); ?>
<?php endwhile; ?>

Sunday, February 19, 2012

Add a login form on WordPress Theme

Add a login form on WordPress Theme

To display a login form just paste the following code on where you will like to display the wordpress login form. Like in sidebar or ant page template


<?php if (!(current_user_can('level_0'))){ ?>
<h2>Login Here</h2>
<form action="<?php echo get_option('home'); ?>/wp-login.php" method="post">
<input type="text" name="log" id="log" value="<?php echo wp_specialchars(stripslashes($user_login), 1) ?>" size="20" />
<input type="password" name="pwd" id="pwd" size="20" />
<input type="submit" name="submit" value="Send" class="button" />
    <p>
       <label for="rememberme"><input name="rememberme" id="rememberme" type="checkbox" checked="checked" value="forever" /> Remember me</label>
       <input type="hidden" name="redirect_to" value="<?php echo $_SERVER['REQUEST_URI']; ?>" />
    </p>
</form>
<a href="<?php echo get_option('home'); ?>/wp-login.php?action=lostpassword">Recover password</a>
<?php } else { ?>
<h2>Logout</h2>
<a href="<?php echo wp_logout_url(urlencode($_SERVER['REQUEST_URI'])); ?>">logout</a><br />
<a href="http://sandipdas.in/wp-admin/">admin</a>
<?php }?>

Saturday, February 18, 2012

Create thumbnail images using PHP

How to Create thumbnail images using PHP

By the following code you can understand how to create a thumbnail of a image

To create a thumbnail, we read in the file using the imagecreatefromjpeg() function and calculate the new thumbnail size. imagesx() and imagesy() functions return the width and height of the image respectively. Then we create a new image using the imagecreatetruecolor(). and Finally, we copy and resize the original file with the imagecopyresized() function and save thumbnail with imagejpeg()


$thumbWidth=70; // fix your widht
$image="mm_travel_photo1.jpg";
$src_img=imagecreatefromjpeg($image);
// get old widht and height
$old_w=imagesx($src_img);
$old_h=imagesy($src_img);
// calculate new widht and height;
$new_width = $thumbWidth;
$new_height = floor( $old_h * ( $thumbWidth / $old_w ) );
$tmp_image=imagecreatetruecolor($new_width,$new_height); // craete tmp images
imagecopyresampled( $tmp_image,$src_img, 0, 0, 0, 0, $new_width, $new_height, $old_w, $old_h);
imagejpeg( $tmp_image, "abc.jpeg" ); // get  the image and name it as abc.jpeg
imagedestroy( $tmp_image );


This process  can support for png and gif also

Extra contact methods to user profiles in wordpress

How to Add extra contact methods to user profiles in Wordpress

The following simple way you can do this , For this Paste the following code into function.php fille and then edit the line 4 and 5 as require.

add_filter('user_othercontactmethods', 'my_user_othercontactmethods');

function my_user_othercontactmethods($user_othercontactmethods){
  $user_othercontactmethods['facebook'] = 'Facebook Username';
  $user_othercontactmethods['twitter'] = 'Twitter Username';

  return $user_othercontactmethods;
}



Monday, January 16, 2012

Display custom text in wordpress

Display custom text in wordpress

Using modified the_excerpt() function we can display an excerpt which is not longer than a predetermined length and doesn’t cut off in mid-sentence.

To do is open functions.php file and paste the code below in it which works as a function

// Variable & intelligent excerpt length.
function print_excerpt($length) { // Max excerpt length. Length is set in characters
    global $post;
    $text = $post->post_excerpt;
    if ( '' == $text ) {
        $text = get_the_content('');
        $text = apply_filters('the_content', $text);
        $text = str_replace(']]>', ']]>', $text);
    }
    $text = strip_shortcodes($text); // optional, recommended
    $text = strip_tags($text); // use ' $text = strip_tags($text,'< p >< a >'); ' if you want to keep some tags

    $text = substr($text,0,$length);
    $excerpt = reverse_strrchr($text, '.', 1);
    if( $excerpt ) {
        echo apply_filters('the_excerpt',$excerpt);
    } else {
        echo apply_filters('the_excerpt',$text);
    }
}

// Returns the portion of haystack which goes until the last occurrence of needle
function reverse_strrchr($haystack, $needle, $trail) {
    return strrpos($haystack, $needle) ? substr($haystack, 0, strrpos($haystack, $needle) + $trail) : false;
}



Now you can use the print_excerpt() function in the theme files like this:

 print_excerpt(50);

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"]