How to remove first image from wordpress post with caption

I struggled lot for this issue. Some times I only removed the first image. some times I removed first caption. Using following code you can easily remove the post first image and caption.

How to remove first image from wordpress post with caption if caption is present

I used the following code for first image removing.

/*
 * Removing the first image from post
 */
function remove_first_image ($content) {
 if (!is_page() && !is_feed() && !is_feed() && !is_home()) {
    if (preg_match("/<img[^>]+\>/i",$content)) {
		 $content = preg_replace("/<img[^>]+\>/i", "", $content, 1);
 	}
	return $content;
   }
}

But I faced the some issues with that code. When first image has caption then only image get removed and caption will remain there.

After that I used the following code for caption remove.

/*
 * Removing the first caption from post with image
 */
function remove_first_image_caption ($content) {
 if (!is_page() && !is_feed() && !is_feed() && !is_home()) {
 $content = preg_replace("(\)", "", $content, 1);
 } return $content;
}
add_filter('the_content', 'remove_first_image_caption');

But if first image has no caption and second image image has caption then both the images will be get removed from post.

After some struggle I written following code. Following is the solution:

Final Code.

/*
 * Removing the first image from post
 * functionality added to delete caption is present to first image.
 */
function remove_first_image ($content) {
 if (!is_page() && !is_feed() && !is_feed() && !is_home()) {
    if (preg_match("/<img[^>]+\>/i",$content)) {
    //find first image URL
     $first_img = '';
     $output = preg_match_all('/< *img[^>]*src *= *["\']?([^"\']*)/', $content, $matches);
     $first_img = $matches[1][0];

    //find first image caption inner text
     $output = preg_match_all("/caption=['"](.*)/", $content, $matches);

     //find first image present in first caption text or not
        $pos = strpos($matches[0][0], $first_img);

    // if image URL found in caption array then delete the caption and image
        if ($pos !== false) {
           $content = preg_replace("(\)", "", $content, 1);
        } else {
           $content = preg_replace("/<img[^>]+\>/i", "", $content, 1);
        }
    }

 } return $content;
}
add_filter('the_content', 'remove_first_image');

solved: pagination for Custom post type not working

Some people asked me about pagination of custom post type. That is very easy.

For showing the custom post type we always use the query_post method.

Just use the following code in your template or theme file.

<!--?<span class="hiddenSpellError" pre="" data-mce-bogus="1"-->php get_header(); ?>

            <!--?<span class="hiddenSpellError" pre="" data-mce-bogus="1"-->php  query_posts( 'post_type=custom-post-type&paged='.$paged );
                                    if (have_posts()) : while (have_posts()) : the_post(); ?>

loop here

              			<!--?<span class="hiddenSpellError" pre="" data-mce-bogus="1"-->php endwhile; ?>
				  <div id="nav-below" class="navigation"></pre>
<div class="nav-previous"><!--?<span class="hiddenSpellError" pre="" data-mce-bogus="1"-->php next_posts_link( __( '<span class="meta-nav">←</span> Older posts', 'twentyten' ) ); ?></div>
<pre></pre>
<div class="nav-next"><!--?<span class="hiddenSpellError" pre="" data-mce-bogus="1"-->php previous_posts_link( __( 'Newer posts <span class="meta-nav">→</span>', 'twentyten' ) ); ?></div>
<pre>
				  </div><!-- #nav-below -->
				  <!--?<span class="hiddenSpellError" pre="" data-mce-bogus="1"-->php endif; wp_reset_query(); ?>

<!--?<span class="hiddenSpellError" pre="" data-mce-bogus="1"-->php get_footer(); ?>
remove first image from wordpress post with caption
remove first image from wordpress post with caption
How to remove first image from wordpress post with caption if caption is present

show posts to only registered users in wordpress

Some people want to show posts to only registered users in wordpress in site. You just need to create category and publish your posts in private category.

How to show posts to only registered users in wordpress

Use the following code in index.php and single.php and page.php file.


$cat_id = get_cat_ID('private');
$args=array(
  'category__not_in' => $cat_id,
  'post_type' => 'post',
  'post_status' => 'publish',
  'posts_per_page' => 5
);

if(!is_user_logged_in()){
    $cat_id = get_cat_ID('private');
   $args=array(
  'category__not_in' => $cat_id,
  'post_type' => 'post',
  'post_status' => 'publish',
  'posts_per_page' => 5
);

} else {
    $cat_id = get_cat_ID('public');
$args=array(
  'category__not_in' => $cat_id,
  'post_type' => 'post',
  'post_status' => 'publish',
  'posts_per_page' => 5
);

}
// The Query
query_posts( $args );

// The Loop
while ( have_posts() ) : the_post();
	echo '<li>';
	the_title();
	echo '</li>';
endwhile;

// Reset Query
wp_reset_query();
How to show posts to only registered users in wordpress
How to show posts to only registered users in wordpress

How to create breadcrumbs in wordpress

we need to show breadcrumbs in wordpress site. For pages and category we can create breadcrumbs in wordpress. Shown, How to create breadcrumbs in wordpress.

How to create breadcrumbs in wordpress

We need to put following code in functions.php file.

<!--?<span class="hiddenSpellError" pre="" data-mce-bogus="1"-->php
function wordpress_breadcrumbs() {

  $delimiter = '&raquo;';
  $name = 'Home'; //text for the 'Home' link
  $currentBefore = '<span class="current">';</span>
  $currentAfter = '';

  if ( !is_home() && !is_front_page() || is_paged() ) {

    echo '<div id="crumbs">';

    global $post;
    $home = get_bloginfo('url');
    echo '<a href="' . $home . '">' . $name . '</a> ' . $delimiter . ' ';

    if ( is_category() ) {
      global $wp_query;
      $cat_obj = $wp_query->get_queried_object();
      $thisCat = $cat_obj->term_id;
      $thisCat = get_category($thisCat);
      $parentCat = get_category($thisCat->parent);
      if ($thisCat->parent != 0) echo(get_category_parents($parentCat, TRUE, ' ' . $delimiter . ' '));
      echo $currentBefore . 'Archive by category '';
      single_cat_title();
      echo ''' . $currentAfter;

    } elseif ( is_day() ) {
      echo '<a href="' . get_year_link(get_the_time('Y')) . '">' . get_the_time('Y') . '</a> ' . $delimiter . ' ';
      echo '<a href="' . get_month_link(get_the_time('Y'),get_the_time('m')) . '">' . get_the_time('F') . '</a> ' . $delimiter . ' ';
      echo $currentBefore . get_the_time('d') . $currentAfter;

    } elseif ( is_month() ) {
      echo '<a href="' . get_year_link(get_the_time('Y')) . '">' . get_the_time('Y') . '</a> ' . $delimiter . ' ';
      echo $currentBefore . get_the_time('F') . $currentAfter;

    } elseif ( is_year() ) {
      echo $currentBefore . get_the_time('Y') . $currentAfter;

    } elseif ( is_single() ) {
      $cat = get_the_category(); $cat = $cat[0];
      echo get_category_parents($cat, TRUE, ' ' . $delimiter . ' ');
      echo $currentBefore;
      the_title();
      echo $currentAfter;

    } elseif ( is_page() && !$post->post_parent ) {
      echo $currentBefore;
      the_title();
      echo $currentAfter;

    } elseif ( is_page() && $post->post_parent ) {
      $parent_id  = $post->post_parent;
      $breadcrumbs = array();
      while ($parent_id) {
        $page = get_page($parent_id);
        $breadcrumbs[] = '<a href="' . get_permalink($page->ID) . '">' . get_the_title($page->ID) . '</a>';
        $parent_id  = $page->post_parent;
      }
      $breadcrumbs = array_reverse($breadcrumbs);
      foreach ($breadcrumbs as $crumb) echo $crumb . ' ' . $delimiter . ' ';
      echo $currentBefore;
      the_title();
      echo $currentAfter;

    } elseif ( is_search() ) {
      echo $currentBefore . 'Search results for '' . get_search_query() . ''' . $currentAfter;

    } elseif ( is_tag() ) {
      echo $currentBefore . 'Posts tagged '';
      single_tag_title();
      echo ''' . $currentAfter;

    } elseif ( is_author() ) {
       global $author;
      $userdata = get_userdata($author);
      echo $currentBefore . 'Articles posted by ' . $userdata->display_name . $currentAfter;

    } elseif ( is_404() ) {
      echo $currentBefore . 'Error 404' . $currentAfter;
    }

    if ( get_query_var('paged') ) {
      if ( is_category() || is_day() || is_month() || is_year() || is_search() || is_tag() || is_author() ) echo ' (';
      echo __('Page') . ' ' . get_query_var('paged');
      if ( is_category() || is_day() || is_month() || is_year() || is_search() || is_tag() || is_author() ) echo ')';
    }

    echo '</div>';

  }
}
?>

After that put following code in header.php file.

<?php if (function_exists('wordpress_breadcrumbs')) wordpress_breadcrumbs(); ?>

After putting above code we can see the breadcrumbs as following;

Home » Parent Page » Sub Page1 » Sub Page2

Home » Category » Subcategory » Post Name

How to create breadcrumbs in wordpress
How to create breadcrumbs in wordpress

Code has been taken from above Source

wordpress theme wysiwyg editor default css

Many wordpress developers are creating wordpress theme. Many times when writing in editor and applying formatting. Formatting is not exactly coming in theme. we given wordpress theme wysiwyg editor default css.

Some time image alignment and photo caption is not coming properly.

wordpress theme wysiwyg editor default css

When you are creating new wordpress theme or taking theme from anyone put following CSS code in style.css file. Make sure following CSS code is present in your wordpress theme.

 /* =WordPress Core
 -------------------------------------------------------------- */
 .alignnone {
 margin: 5px 20px 20px 0;
 }

.aligncenter, div.aligncenter {
 display:block;
 margin: 5px auto 5px auto;
 }

.alignright {
 float:right;
 margin: 5px 0 20px 20px;
 }

.alignleft {
 float:left;
 margin: 5px 20px 20px 0;
 }

.aligncenter {
 display: block;
 margin: 5px auto 5px auto;
 }

a img.alignright {
 float:right;
 margin: 5px 0 20px 20px;
 }

a img.alignnone {
 margin: 5px 20px 20px 0;
 }

a img.alignleft {
 float:left;
 margin: 5px 20px 20px 0;
 }

a img.aligncenter {
 display: block;
 margin-left: auto;
 margin-right: auto
 }

.wp-caption {
 background: #fff;
 border: 1px solid #f0f0f0;
 max-width: 96%; /* Image does not overflow the content area */
 padding: 5px 3px 10px;
 text-align: center;
 }

.wp-caption.alignnone {
 margin: 5px 20px 20px 0;
 }

.wp-caption.alignleft {
 margin: 5px 20px 20px 0;
 }

.wp-caption.alignright {
 margin: 5px 0 20px 20px;
 }

.wp-caption img {
 border: 0 none;
 height: auto;
 margin:0;
 max-width: 98.5%;
 padding:0;
 width: auto;
 }

.wp-caption p.wp-caption-text {
 font-size:11px;
 line-height:17px;
 margin:0;
 padding:0 4px 5px;
 }

&nbsp;

Above CSS code is very important for every wordpress theme.

wordpress-default css for wordpress theme
wordpress-default css for wordpress theme

How to How to remove first image from post wordpress

wordpress tutorial, remove first image from post wordpress. In many wordpress old site they used the first image as post or featured image. Here solution. while showing the single post they not want to show the first image which is uploaded.

How to remove first image from post wordpress

How to remove first image from post wordpress
How to remove first image from post wordpress

Using following code you can remove the first image from wordpress post. Please put following code in to functions.php file (you will find this file in your theme folder)


function remove_first_image ($content) {
if (!is_page() && !is_feed() && !is_feed() && !is_home()) {
$content = preg_replace("/<img[^>]+\>/i", "", $content, 1);
} return $content;
}
add_filter('the_content', 'remove_first_image');

when you see the single page of blog then you will not see the first image from blog post.

 

how to remove images from wordpress post content

WordPress tutorial, how to remove images from wordpress post content. Some time we want to remove the images from wordpress post. Given code here in article.

I got following code for removing the images from post. you can use following code in single.php or page.php file.

how to remove images from wordpress post content

how to remove images from wordpress post content
how to remove images from wordpress post content

<?php
ob_start();
the_content('Read the full post',true);
$postOutput = preg_replace('/<img[^>]+./','', ob_get_contents());
ob_end_clean();
echo $postOutput;
?>

increase file upload size limit in wordpress

Many times people want to upload big size media files into wordpress blog for download. Some people asked me about uploading the PDF files and video files into wordpress blog. You can easily upload big size media file to wordpress.

increase file upload size limit in wordpress

When to do wordpress installation that time default upload file limit is 2mb. Best way to increase the upload limit is adding code into .htaccess file. This file you will find in wordpress installation folder. Dont look into theme folder. Check your wordpress root folder for finding the .htaccess file.

Add following code in that file.


php_value upload_max_filesize 200M
php_value post_max_size 200M
php_value max_execution_time 2000
php_value max_input_time 2000

increase file upload size limit in wordpress
increase file upload size limit in wordpress

After this you need to add the following code in to your wp-config.php file.


<tt>define('WP_MEMORY_LIMIT', '200MB');</tt>

Increase the maximum amount of a time a PHP script will run. Note: If using a shared hosting service, you may need to ask your host to increase the limit.

For you information I am giving you the information about most popular hosting providers.

Hostgator will gives the ability to increase the limit upto only 64mb.

Bluehost will gives the ability to increase the limit upto only 96mb.

Godaddy will gives the ability to increase the limit upto only 64mb.

Mostly all shared hosting providers gives you facility to upload files upto 64mb size file. If you are having any doubts then you can write to me on wordpressapi@gmail.com.

block spam bot wordpress sites using apache

WordPress tip for security, block spam bot wordpress sites using apache. I faced a lot of issue with spam comments in my blog. Spam-bot sites will just hit. Spam-bot sites will just hit the wp-comments-post.php file and put the comments. Due to this our lot of time get wasted.

block spam bot wordpress sites using apache

Using apache setting you can protect your blog and restrict the spam bot. You need to made changes in your Virtualhost entry or you can change the .htaccess file also.

When a spam-bot comes in, it hits the file directly and usually does not leave a referrer. This allows for some nifty detection and action direct from the server. If you are not familiar with Apache directives, then write the following in your root directory .htaccess file:

RewriteEngine On
RewriteCond %{REQUEST_METHOD} POST
RewriteCond %{REQUEST_URI} .wp-comments-post\.php*
RewriteCond %{HTTP_REFERER} !.*yourdomain.com.* [OR]
RewriteCond %{HTTP_USER_AGENT} ^$
RewriteRule (.*) http://%{REMOTE_ADDR}/$ [R=301,L]
block spam bot wordpress sites using apache
block spam bot wordpress sites using apache

This will:

  1. Detect when a POST is being made
  2. Check to see if the post is on wp-comments-post.php
  3. Check if the referrer is in your domain or if no referrer
  4. Send the spam-bot BACK to its originating server’s IP address.

You just need to change the youdomain.com to your domain address.

This tutorial is written by purabtech.in. If you are having any issue then please write to me on support@purabtech.in

kill spam wordpress comments by changing comment file

In Article, we tell you about how to change the filename of wp-comments-post.php file another name. and kill spam wordpress comments by changing filename.

kill spam wordpress comments by changing filename wp-comments-post.php

With WordPress blogs and sites spam comments is very big issue. That waste our lot of time. There is very nice plugin called Akismet by wordpress. Which is helpful for catching the spam comments but that is not smart enough. Some spambots simply hit the wp-comments-post.php and wp-trackback.php files directly, without scanning your site to find the real names of these scripts. You can foil such bots by renaming these files. My site is also faced the spam comments and hack issues by spmmers. So following trick is very useful.

This files will not track which comment is spam. In this tutorial I will tell you about how to change the filename of wp-comments-post.php file another name. This way you can easily protect your blog against spammer comments.

You need to just follow the steps:

  1. Back up all your files.
  2. Replace all instances of wp-comments-post.php in your theme template files with wp-comments-kill-spam.php. In most themes, this will mean editing comments.php and comments-popup.php; it’s a good idea to double-check by searching through all files in your theme directory with the “find and replace” feature of your favorite editor.
  3. Upload your edited template files.
  4. Rename your wp-comments-post.php file to wp-comments-die-spam-die.php. Be sure that wp-comments-post.php is gone when you’re done, or spammers will still be able to use it!
  5. (Optional) Create a blank file and name it wp-comments-post.php. This will prevent spambots from getting a 404 error. If your 404 page is large, this will save you some bandwidth; it might also keep the spammers from catching on to your trick.
  6. Check that everything works by posting a test comment to your blog.

You will find following code in comments.php file.


<form action="<!--?php echo get_option('siteurl'); ?-->/wp-comments-post.php" method="post" id="commentform">

just replace to that as follows:


<form action="<!--?php echo get_option('siteurl'); ?-->/<tt>wp-comments-kill-spam.php</tt>" method="post" id="commentform">

This is simple way to protect your blog without any wordpress plugin. This tip is written by purabtech.in

If you are having any issue then please write to me support@purabtech.in.

kill spam wordpress comments by changing comment file
kill spam wordpress comments by changing comment file

how to speed up wordpress site load time

For speed up WordPress without developer then you can also do that. In this Article we shown,  how to speed up wordpress site load time, Using some tricks you can speed up your wordpress site. Here we given some very basic steps for this.

You need to install varnish with wordpress and apache and increase wordpess website performance so much drastically.

Now more than 19 millions websites build in wordpress only. So wordpress sites market increased so much. People like the faster sites with good content and graphics. If your site visitors are less and content is limited then you don’t need to worry but content grows and traffic of your site increases then problem happens.

how to speed up wordpress site load time

how to speed up wordpress site load time
how to speed up wordpress site load time

When my site traffic is grown then I faced lot of issue. I changed four times my hosting service. I spend to much time about doing R&D about bandwidth and speed of wordpress blog. Then I noted some certain points for loading the site fastly.

In this article I written some points which are very important for increasing the speed of wordpress blogs.

Use the Cache Plugin

Use the WP Super Cache in your wordpress site. This plugin is very helpful, If you are using shared hosting then also this plugin is very helpful. you will see significant decrease in your site load time and much more efficiency in the usage of server resources.

Get good Hosting

Choosing hosting for wordpress sites if always issue. There are some very nice hosting providers for small wordpress sites. If your blog or site is having less than one thousand page views per day then only you need to go with shared hosting. If you are having more than one thousand page views then you should go for VPS or dedicated linux server. But for setting up wordpress on server you need to server knowledge also. If you are having the more than 10k to 20k visitors per day then you should go with Amazon or Rackspace or Gogrid cloud hosting server. For shared I can recommend following options.

I worked on Amazon EC2 and Amazon s3 for many web applications. If you are having any issues then you can write to me.

Hotlinking and Prevent Leeching

hotlinking means someone directly accessing your site images from other site. This will kill your bandwidth. If you want to prevent to stealing your site images then you need to just put following code in .htaccess file.


RewriteEngine on
RewriteCond %{HTTP_REFERER} !^$
RewriteCond %{HTTP_REFERER} !^http(s)?://(www\.)?purabtech.in [NC]
RewriteCond %{HTTP_REFERER} !^http(s)?://(www\.)?google.com [NC]
RewriteCond %{HTTP_REFERER} !^http(s)?://(www\.)?feeds2.feedburner.com/wordpressapi [NC]
RewriteRule \.(jpg|jpeg|png|gif)$ – [NC,F,L]

Remove the unnecessary wordpress plugins

We always use the multiple wordpress plugins for various purpose. My advise is as much as possible don’t use the wordpress plugins. Just use the required wordpress plugins. For contact form and share buttons we always use the wordpress plugins but don’t use plugins for that. Use the following articles and scripts for avoiding the wordpress plugins:

  1. how to create contact us page without wordpress plugin
  2. Add the extra new users details or fields to wordpress without plugin
  3. Display wordpress Tags In A Dropdown Menu without plugin
  4. How to change the Visual Editor Font Size wordpress without plugin
  5. Display wordpress Tags In A Dropdown Menu without plugin
  6. wordpress pagination style without wordpress plugin
  7. show popular posts without wordpress plugin in theme
  8. How to send smtp email through wordpress without plugin
  9. How to put digg button in wordpress without plugin
  10. Get the recent comments without using wordpress plugin or widget
  11. How to Add the social Bookmar Icons in WordPress theme without wordpress plugins
  12. How to exclude pages from wordpress menu without plugin
  13. Show related posts with wordpress post without using any plugin

This will save your bandwidth and your wordpress load fast.

Choose the Best Theme for your site

Choosing the right wordpress theme is also important. Your wordpress theme should load faster and minimum quires will fired to database server so your web page load fast. You should check following article for choosing the fast loading themes.

With great CSS and HTML code wordpress themes are great and HTML5 ready themes will load fastly and that is SEO friendly aslo.

Use the Compressed Images

Images always take so much bandwidth so you need to be very careful about images in your wordpress blog. Before uploading images to wordpress resize the image and then upload it to wordpress.

Images that use only a few colors should be saved as .GIF or .PNG files, and images that use a full spectrum of colors (like true photographs) should be saved as .JPG files. If you use a photo editing software, like Photoshop, you can “save for the web,” which will find the best format to save, while still maintaining quality.

Show Excerpt on Home page

On homepage dont show the full post. Just show the excerpt of post on home page. With your post showing featured image is always good. That will attract your user to read the article.If each of your post is a list post and it is displayed in full text mode, then it will be very inconvenient for your users because the page will take an immense amount of time and server resources to load. Therefore you must use excerpts and limit the count on how many posts are displayed.

Compress CSS and Javascript

CSS and javascript files also another requests to apache server. You should compress your css and javascript file in one or two files.

  • Stylesheets

Stylesheets are easy to compress, just use your favorite text editor to delete comments and unnecessary spaces and line breaks. Although you won’t see a drastic difference unless your style sheets involve tens of thousands of lines.

  • JavaScript

Compressing JavaScript isn’t as simple, but it is still relatively easy. There are a number of tools available, such as this one, which is free, that will compress your JavaScript code for you. Better yet, don’t rely on much JavaScript at all if you can help it. If your site has lots of interactivity, moving parts and fancy features, there is a good chance it is running a lot of JavaScript and that could slow things down.

Optimize Your MySQL Database

Some times your tables will go overhead. You should clean your database. For this you should use  Optimize DB, wordpress plugin does exactly what we mentioned above except you don’t have to mess with phpMyAdmin. This plugin will reduce your work. Every 5 posts you should optimize the your database.

Use Sprite Images

If you are using many small images in your website then create sprite image. Sprite image will increase your site performance.