how to integrate wordpress into php website

WordPress is very easy to work on. PHP tutorial, how to integrate wordpress into php website. you can very easily integrate the wordpress with php or html site. Here we given code for same.

There may be only a few features of WordPress you want to use when integrating it with your site, or you may want your entire site run with WordPress. This tutorial will guide you through making your WordPress site look like your current design.

how to integrate wordpress into php website

How to start?
First you need to disable the wordpress theme. using following code.

open your header.php file from your wordpress theme and put following code in that file.

<?php
/* Short and sweet */
define('WP_USE_THEMES', false);
require('./wp-blog-header.php');
?>

Create any php file and put following code in that file.

<?php
require('/the/path/to/your/wp-blog-header.php');
?>

In the event you want to show ten posts sorted alphabetically in ascending order on your web page, you could do the following to grab the posted date, title and excerpt:

<?php
require('/the/path/to/your/wp-blog-header.php');
?>

<?php
$posts = get_posts('numberposts=10&order=ASC&orderby=post_title');
foreach ($posts as $post) : start_wp(); ?>
<?php the_date(); echo "<br />"; ?>
<?php the_title(); ?>
<?php the_excerpt(); ?>
<?php
endforeach;
?>
how to integrate wordpress into php website
how to integrate wordpress into php website

For more information you can write to me.

how to protect your images on wordpress

Question is, how to protect your images on wordpress. Some time back our server got so much load and bandwidth of server is taken by other websites. preventing images from used by other site is important.

how to protect your images on wordpress

we checked the access to log of my site. we saw the request for my site images through other site. I decided to stop that. Earlier I written good article about this.

https://purabtech.in/protect-images-accessing-server-apache/

I written simple rewrite rule with mod_redirection.

Just open your .htaccess file from your root folder and following code in that.

#Replace ?wordpressapi\.com/ with your blog url
RewriteCond %{HTTP_REFERER} !^http://(.+\.)?wordpressapi\.com/ [NC]
RewriteCond %{HTTP_REFERER} !^$
#Replace /images/nohotlink.jpg with your "don't hotlink" image url
RewriteRule .*\.(jpe?g|gif|bmp|png)$ /images/nohotlink.jpg [L]

dont forget to replace your blog name.

 

In wordpress add tags and categories to custom post type

Many times we use the custom post type in wordpress. Wordpres tutorial, In wordpress add tags and categories to custom post type.  Some times we need to category and tags for custom post type which associated.

In wordpress add tags and categories to custom post type

you can use the following code in functions.php file.

// === CUSTOM POST TYPES === //
function create_my_post_types() {
	register_post_type( 'Services',
		array(
			'labels' => array(
				'name' => __( 'Services' ),
				'singular_name' => __( 'Services' ),
				'add_new_item' => 'Add New Services',
				'edit_item' => 'Edit Services',
				'new_item' => 'New Services',
				'search_items' => 'Search Services',
				'not_found' => 'No Services found',
				'not_found_in_trash' => 'No Services found in trash',
			),
			'_builtin' => false,
			'public' => true,
			'hierarchical' => false,
			'taxonomies' => array( 'website_type', 'service'),
			'supports' => array(
				'title',
				'editor',
				'excerpt',
                                'thumbnail'
			),
			'rewrite' => array( 'slug' => 'services', 'with_front' => false ),

		)
	);
}
add_action( 'init', 'create_my_post_types' );

// === CUSTOM TAXONOMIES === //
function my_custom_taxonomies() {
	register_taxonomy(
		'website_type',		// internal name = machine-readable taxonomy name
		'Services',		// object type = post, page, link, or custom post-type
		array(
			'hierarchical' => true,
			'label' => 'Website Types',	// the human-readable taxonomy name
			'query_var' => true,	// enable taxonomy-specific querying
			'rewrite' => array( 'slug' => 'website_type' ),	// pretty permalinks for your taxonomy?
		)
	);
	register_taxonomy(
		'service',
		'post',
		array(
			'hierarchical' => false,
			'label' => 'Service',
			'query_var' => true,
			'rewrite' => array( 'slug' => 'service' ),
		)
	);

}
add_action('init', 'my_custom_taxonomies', 0);

Please consult with your developer.

In wordpress add tags and categories to custom post type
In wordpress add tags and categories to custom post type

 

……………………..

Solved allowed memory size of 33554432 bytes exhausted wordpress

We solved Allowed memory size of 33554432 bytes exhausted which for wordpress. Given solution for allowed memory. Change given for wordpress/wp-config.php file.

Solved Allowed memory size of 33554432 bytes exhausted

We got always following ERROR PHP Fatal error:

Allowed memory size of 33554432 bytes exhausted (tried to allocate 10485761 bytes)

This issue with old and new wordpress versions both. First you need to increase memory limit for your php package. Use following method for increase the memory for php.

Open php configuration file


# vim /etc/php.ini

Change following sections:

max_execution_time = 300 ; Maximum execution time of each script, in seconds
max_input_time = 300 ; Maximum amount of time each script may spend parsing request data
memory_limit = 128M ; Maximum amount of memory a script may consume (16MB)

After this dont forget to restart apache server.

I know on 2.5.1 i needed to increase the memdory, but i don’t know how in 2.6. in the wp-config.php there no define to increase memory. If you are using old wordpress version less than wordpress 2.6 version or you are using the wordpress MU then use following code. open your wp-settings.php file from root folder and change following line

if ( !defined('WP_MEMORY_LIMIT') )
        define('WP_MEMORY_LIMIT', '32M');
to
if ( !defined('WP_MEMORY_LIMIT') )
        define('WP_MEMORY_LIMIT', '128M');

If you are using the newer wordpress version greater than 2.7 then use following method. Following URL is also helpful http://codex.wordpress.org/Editing_wp-config.php

#Increasing_memory_allocated_to_PHP Edit wp-config.php and enter the following line


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

If you are using the shared hosting then use following method.

  •  Create a file called php.ini in the root of your site (if you are using a hosted addon domain, this must be in the subdirectory of that site)
  • In php.ini, enter a line that says memory_limit = 64MB 3. In your site’s .htaccess (being a WordPress blog, I’m assuming there is one), enter the following line SetEnv PHPRC // (keep the slashes)
  • Edit wp-config.php and enter the following line

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

  • Upload the new files to the server Oh, and don’t tell your hosting provider you’ve done this… This will solve your issue.
Solved Allowed memory size of 33554432 bytes exhausted
Solved Allowed memory size of 33554432 bytes exhausted

solved wordpress custom post type pagination not working

From wordpress 3.0 version, wordpress started the custom post type functionality. There are API provided by wordpress for adding the pagination for custom post type. Some WP developers asked me about pagination of custom post type. That is very easy to add the pagination. Here in this article I given the code snippet for showing the pagination in custom post type.

solved wordpress custom post type pagination not working

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

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

<?php get_header(); ?>

            <?php  query_posts( 'post_type=custom-post-type&paged='.$paged );
                                    if (have_posts()) : while (have_posts()) : the_post(); ?>

//loop here

              			<?php endwhile; ?>
				  <div id="nav-below" class="navigation">
					<div class="nav-previous"><?php next_posts_link( __( '<span class="meta-nav">&larr;</span> Older posts', 'twentyten' ) ); ?></div>
					<div class="nav-next"><?php previous_posts_link( __( 'Newer posts <span class="meta-nav">&rarr;</span>', 'twentyten' ) ); ?></div>
				  </div><!-- #nav-below -->
				  <?php endif; wp_reset_query(); ?>

<?php get_footer(); ?>
solved wordpress custom post type pagination not working
solved wordpress custom post type pagination not working

Have fun!

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

wordpress pagination with query posts

wordpress pagination with query posts, Mostly people use query_posts function for fetching posts. But most of all people face issue with pagination.

wordpress pagination with query posts

Just use following code for pagination. It is simple and easy.

<?php
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
query_posts( 'post_type=documents&paged='.$paged );
query_posts( 'post_type=post&paged='.$paged );
if (have_posts()) : while (have_posts()) : the_post(); ?>

<a href="<?php the_permalink(); ?>" title="<?php the_title(); ?>"><?php the_title(); ?></a>
<?php the_excerpt();  ?>

 <?php endwhile; ?>
<div id="nav-below" class="navigation">
                                        <div class="nav-previous"><?php next_posts_link( __( '<span class="meta-nav">&larr;</span> Older posts', 'twentyten' ) ); ?></div>
                                        <div class="nav-next"><?php previous_posts_link( __( 'Newer posts <span class="meta-nav">&rarr;</span>', 'twentyten' ) ); ?></div>
                                </div><!-- #nav-below -->
                                  <?php endif; wp_reset_query(); ?>

wordpress pagination with query_posts
wordpress pagination with query_posts

Limit excerpt length by characters in wordpress

On wordpress home page many sites are showing the post description or we can say the excerpt. For UI purpose some time we need to control the excerpt character limit.  I already written about this in following article. But some people need the more advanced excerpt. Following code snippet will be helpful to you show the limit excerpt length by characters in wordpress.

First open your functions.php file and put following code in that file.

[viral-lock message=”Solution code is Hidden! It’s Visible for Users who Liked/Shared This article on Facebook or Twitter or Google+. Like or Tweet this article to reveal the content.”]

function get_excerpt($count){
  $permalink = get_permalink($post->ID);
  $excerpt = get_the_content();
  $excerpt = strip_tags($excerpt);
  $excerpt = substr($excerpt, 0, $count);
  $excerpt = substr($excerpt, 0, strripos($excerpt, " "));
  $excerpt = $excerpt.'... <a href="'.$permalink.'">more</a>';
  return $excerpt;
}

[/viral-lock]

Use this function if you’re planning on using it more than once with a different amount of characters.

Call the function plus the amount of characters –

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

Following is my old article link.
https://purabtech.in/set-wordpress-post-excerpt-length-limited-characters/

Limit excerpt length by characters in wordpress
Limit excerpt length by characters 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

Show selected tags in wordpress using Selective Tag Cloud Widget plugin

Some time people want to show Show selected tags in wordpress from all site. Through selective Tags plugin, you can select the post tags which you want to show. We are very happy to launch the new wordpress plugin called Selective Tags.

How to install Selective Tag Cloud Widget?

Follow the usual routine;

  1. Open WP admin – Plugins – Add New
  2. Enter “Selective Tag Cloud Widget” under search and hit Enter
  3. Plugin will show up as the first on the list, click “Install Now”

Or if needed, upload manually. Follow the steps below to install the plugin.

  1. Upload the Selective Tag Cloud Widget directory to the /wp-content/plugins/directory
  2. Activate the plugin through the ‘Plugins’ menu in wp
  3. Go to “Selective Tag Cloud Widget” option to configure the button

Show selected tags in wordpress using selective Tags plugin

Some time people want to show the only selective tags from all site. Through this widget you can select the post tags which you want to show on your site.

More information about Plugin as follows:

Selective Tag WordPress Plugin, provides sidebar widgets which can be used to display tags in the sidebar.

Selective Tag

Sidebar Widget

Selective Tag WordPress Plugin, provides sidebar widgets which can be used to display tags in the sidebar. You can have multiple widgets with different set of tags configured for each one of them.

Each widget allows you to choose

  • The set of tags displayed which is selected by admin
  • Admin can select tag to show in sidebar. Auto complete Text box for Tags.
  • When try to type tags, It will suggest you the used tags from wordpress site.
You can download the plugin from following location.

Screenshot

Show selected tags in wordpress using selective Tags plugin
Show selected tags in wordpress using selective Tags plugin