How to use custom post type in wordpress

From wordpress 3.0 version release wordpress gives the facility to add a custom post type functionality. With custom post type wordpress is became more powerful and expendable and more advanced We given info about, How to use custom post type in wordpress.

How to use custom post type in wordpress

WordPress recognized the need to people and industry and they introduced the custom post type. In this tutorial I will tell you how to use the custom post type very effectively.

Adding Custom post type is very easy. Using theme or plugin file you can add the custom post types in wordpress admin area.

Here I am going to give you example using wordpress theme files. Open you functions.php file and copy paste the following code in that file.


add_action('init', 'create_product');
 function create_product() {
 $product_args = array(
 'label' => __('Product'),
 'singular_label' => __('Product'),
 'public' => true,
 'show_ui' => true,
 'capability_type' => 'post',
 'hierarchical' => false,
 'rewrite' => true,
 'supports' => array('title', 'editor', 'thumbnail')
 );
 register_post_type('product',$product_args);
 }

How to use custom post type in wordpress
How to use custom post type in wordpress

The function register_post_type() accepts two arguments: the name we want to give our post type, and a list of arguments used to create that post type, which we put in an array called $args.

Using above code that code will add the product post type to wordpress panel.

Now you can add the meta fields to custom post type. Use the following code in file for add the meta fields. Custom meta fields function is available from quite some time. add_meta_box() function is very useful to adding custom fields to wordpress post.


<?php

 add_action("admin_init", "add_product");
 add_action('save_post', 'update_thumbnail_url');
 function add_product(){
 add_meta_box("product_details", "product Options", "product_options", "product", "normal", "low");
 }
 function product_options(){
 global $post;
 $custom = get_post_custom($post->ID);
 $thumbnail_url = $custom["thumbnail_url"][0];
 $product_info = $custom["product_info"][0];
 $product_infos = $custom["product_infos"][0];
 $video_code = $custom["video_code"][0];

?>
 <div id="product-options">
 <label>Thumbnail URL:</label><input size="100" name="thumbnail_url" value="<?php echo $thumbnail_url; ?>" /><br>
 <label>Product Info:</label><input size="100" name="product_info" value="<?php echo $product_info; ?>" /><br>
 <img src="<?php echo $product_infos; ?>"><br>
 <label>Video Code:</label><textarea cols="50" rows="5" name="video_code"><?php $video_code; ?></textarea>
 </div><!--end product-options-->
<?php
 }
 function update_thumbnail_url(){
 global $post;
 update_post_meta($post->ID, "thumbnail_url", $_POST["thumbnail_url"]);
 update_post_meta($post->ID, "product_info", $_POST["product_info"]);
 update_post_meta($post->ID, "video_code", $_POST["video_code"]);

 }
?>

Using following code you can see the product information in edit product page and you can able to see the all information in for edit. Admin will know which fields are available to edit.

How to use custom post type in wordpress
How to use custom post type in wordpress

<?php
add_action("manage_posts_custom_column",  "product_custom_columns");
add_filter("manage_edit-product_columns", "product_edit_columns");

function product_edit_columns($columns){
 $columns = array(
 "cb" => "<input type=\"checkbox\" />",
 "title" => "Product Title",
 "thumbnail_url" => "Thumbnail URL",
 "product_info" => "Product Info",
 "video_code" => "Video Code",
 );
 return $columns;
}
function product_custom_columns($column){
 global $post;
 switch ($column) {
 case "thumbnail_url":
 $custom = get_post_custom();
 echo $custom["thumbnail_url"][0];
 break;
 case "product_info":
 $custom = get_post_custom();
 echo $custom["product_info"][0];
 break;
 case "video_code":
 $custom = get_post_custom();
 echo $custom["video_code"][0];
 break;
 }
}

?>

Using custom post type and add meta tag you can develop very nice applications. Now I am going to show you how you can extract the custom posts in wordpress frontend.

Using wp_query you can easily extract product posts. In any category page or conditionally you can use following code.


$loop = new WP_Query( array( 'post_type' => 'product', 'posts_per_page' => 10 ) );
while ( $loop->have_posts() ) : $loop->the_post();
 the_title();
the_content();
global $post;
 $custom = get_post_custom($post->ID);
echo $thumbnail_url = $custom["thumbnail_url"][0];
echo $product_info = $custom["product_info"][0];
echo $video_code = $custom["video_code"][0];
 echo '<div>';
 the_content();
 echo '</div>';
endwhile;

Or you can create the custom theme page using following code. Just create product.php page in your wordpress theme folder.Put following code in that file.


<?php

/*Template Name: Product*/

?>

<?php get_header(); ?>

<div id="container">

<div id="content" role="main">

<div id="post-<?php the_ID(); ?>" <?php post_class(); ?>

<?php $recent = new WP_Query('post_type=product&posts_per_page=10′); while($recent->have_posts()) : $recent->the_post();?>

<?php the_title( '<h2><a href="' . get_permalink() . '" title="' . the_title_attribute( 'echo=0′ ) . '" rel="bookmark">', '</a></h2>' ); ?>

<div>

<?php the_content(); ?>

<?php

the_content();
global $post;
$custom = get_post_custom($post->ID);
echo $thumbnail_url = $custom["thumbnail_url"][0];
echo $product_info = $custom["product_info"][0];
echo $video_code = $custom["video_code"][0];

?>

<?php wp_link_pages( array( 'before' => '<div>' . __( 'Pages:', 'wordpressapi' ), 'after' => '</div>' ) ); ?>

<?php edit_post_link( __( 'Edit', 'wordpressapi' ), '<span>', '</span>' ); ?>

</div><!– .entry-content –>

<?php comments_template( ", true ); ?>

<?php endwhile; ?>

</div><!– #content –>

</div><!– #container –>

<?php get_sidebar(); ?>

<?php get_footer(); ?>

Now create product page and product as template.

If you have any issues or doubts then write to me.

how to do persistent database connection in wordpress

Here in wordpress tutorial, we explained, how to do persistent database connection in wordpress. Persistent connections are links that do not close when the execution of your script ends.

What is persistent database connection?

Persistent connections are links that do not close when the execution of your script ends. When a persistent connection is requested, PHP checks if there’s already an identical persistent connection (that remained open from earlier) – and if it exists, it uses it. If it does not exist, it creates the link. An ‘identical’ connection is a connection that was opened to the same host, with the same username and the same password (where applicable).

how to do persistent database connection in wordpress
how to do persistent database connection in wordpress

If you want to use the persistent database connection then you should follow my steps:

how to do persistent database connection in wordpress

First Open the wp-db.php file from wp-includes folder. In that file find following words:


// @mysql_connect( $dbhost, $dbuser, $dbpassword, true );

//Change that to

@mysql_pconnect( $dbhost, $dbuser, $dbpassword, true );

comment the mysql_connect line. This line you will find two times in that file. You need to change the line both the times. Then upload this file to your wordpress installation.

Persistent database connection will open only one connection and for every query that will check for connection is present or not. If connection is already present then your query will execute using that persistent database connection.

There are couple of issues with persistent database connection, When you are using the persistent connection you should keep following things in mind.

Imp: There are a couple of additional caveats to keep in mind when using persistent connections. One is that when using table locking on a persistent connection, if the script for whatever reason cannot release the lock, then subsequent scripts using the same connection will block indefinitely and may require that you either restart the httpd server or the database server. Another is that when using transactions, a transaction block will also carry over to the next script which uses that connection if script execution ends before the transaction block does. In either case, you can use register_shutdown_function() to register a simple cleanup function to unlock your tables or roll back your transactions. Better yet, avoid the problem entirely by not using persistent connections in scripts which use table locks or transactions (you can still use them elsewhere).

How to use smiley images in wordpress posts

If you want to use smiley images in wordpress posts then you can use our tricks. Here in this article, we given sample and trick for using smiley in posts.

Follow our steps.

  1. Find your smiley image files in the /wp-includes/images/smilies directory and back them up to another directory
  2. Note the names of each smiley file. Your files must match these names and should be in the same ‘gif’ image format.
  3. For predictable behavior, the image sizes should be similar.
  4. Upload your new files to the /wp-includes/images/smilies directory with an FTP program.

How to use smiley images in wordpress posts

How to use smiley images in wordpress posts
How to use smiley images in wordpress posts

If you want to changes the names of smiley and you need to edit wp-includes/functions.php file.

In that file you will find the “smilies_init” function.

You will find the following code in that file.


if ( !isset( $wpsmiliestrans ) ) {
 $wpsmiliestrans = array(
 ':mrgreen:' => 'icon_mrgreen.gif',
 ':neutral:' => 'icon_neutral.gif',
 ':twisted:' => 'icon_twisted.gif',
 ':arrow:' => 'icon_arrow.gif',
 ':shock:' => 'icon_eek.gif',
 ':smile:' => 'icon_smile.gif',
 ':???:' => 'icon_confused.gif',
 ':cool:' => 'icon_cool.gif',
 ':evil:' => 'icon_evil.gif',
 ':grin:' => 'icon_biggrin.gif',
 ':idea:' => 'icon_idea.gif',
 ':oops:' => 'icon_redface.gif',
 ':razz:' => 'icon_razz.gif',
 ':roll:' => 'icon_rolleyes.gif',
 ':wink:' => 'icon_wink.gif',
 ':cry:' => 'icon_cry.gif',
 ':eek:' => 'icon_surprised.gif',
 ':lol:' => 'icon_lol.gif',
 ':mad:' => 'icon_mad.gif',
 ':sad:' => 'icon_sad.gif',
 '8-)' => 'icon_cool.gif',
 '8-O' => 'icon_eek.gif',
 ':-(' => 'icon_sad.gif',
 ':-)' => 'icon_smile.gif',
 ':-?' => 'icon_confused.gif',
 ':-D' => 'icon_biggrin.gif',
 ':-P' => 'icon_razz.gif',
 ':-o' => 'icon_surprised.gif',
 ':-x' => 'icon_mad.gif',
 ':-|' => 'icon_neutral.gif',
 ';-)' => 'icon_wink.gif',
 '8)' => 'icon_cool.gif',
 '8O' => 'icon_eek.gif',
 ':(' => 'icon_sad.gif',
 ':)' => 'icon_smile.gif',
 ':?' => 'icon_confused.gif',
 ':D' => 'icon_biggrin.gif',
 ':P' => 'icon_razz.gif',
 ':o' => 'icon_surprised.gif',
 ':x' => 'icon_mad.gif',
 ':|' => 'icon_neutral.gif',
 ';)' => 'icon_wink.gif',
 ':!:' => 'icon_exclaim.gif',
 ':?:' => 'icon_question.gif',
 );

You can edit that names as per your requirement and add more smiley.

If you want to put some css for your smiley then you can use or put following CSS class in your style.css file (Theme folder).

img.wp-smiley {
   float: none;
   padding:2px;
   border:none;
}

That sit! Have fun.

change background for specific page and category in wordpress

WordPress tutorial, We will show how to change background for specific page and category in wordpress theme. achieve using conditional tags.

change background for specific page and category in wordpress

Many times clients demands for different background color or different images as a background. We can very easily achieve this using wordpress conditional tags.

change background for specific page and category in wordpress
change background for specific page and category in wordpress

You can use following code in your page.php or category.php file which is present in wordpress theme. If you want to change the background for specific page then open page.php file from theme folder.


<body <?php if(is_page('contact us'))&nbsp; echo 'class="contact_us"';?>>

Use following css code in your style.css file


.contact_us{

.background-color:#ccc;

}

.php_category{

.background-color:#181818;

}

If you want to use different background for your category then use following code.



<body <?php if(is_category('php''))&nbsp; echo  'class="php_category"';?>>

How do we protect wordpress admin panel and secure it

WordPress admin security is very important. Many people are always take a shared hosting. we given Information about How do we protect wordpress admin panel.  Many people are always take a shared hosting for publishing the wordpress website.

With shared hosting or dedicated you should always think about hack proof wordpress admin panel. So no one can misuse your worepress admin panel. Here in this article I will give you some very nice tips about, how to protect our wordpress admin panel.

protect wordpress admin panel
protect wordpress admin panel

protect wordpress admin panel

1. Create very strong password

Always create a very strong password for your admin user. You should add some special characters and numbers in your password. Your password should be 10 digit minimum and not related to your name or surname.

Main important think is dont disclose your password to anyone.

2. Limit wordpress admin access via IP address

You can use the .htaccess file for limiting the wordpress admin panel for specific ip addresses only. Use following code for that.

<files “wp-login.<span=”” class=”hiddenSpellError” pre=”” data-mce-bogus=”1″>php”>
Order Deny,Allow
Deny from all
Allow from Your.IP.Address

allow from 192.168.2.45

</Files>
[/php]

3. Avoid the “admin” Username

You should always avoid the admin username to login. You can easily achieve this. First create another super admin with another name and open your functions.php file from wordpress theme folder and use following code.


add_filter(‘login_errors’,create_function(‘$a’, “return null;”));

There are some very nice plugins also available for providing the wordpress security your wordpress website. Here are few:

http://wordpress.org/extend/plugins/one-time-password/

http://wordpress.org/extend/plugins/semisecure-login-reimagined/

http://www.bad-neighborhood.com/login-lockdown.html

http://wordpress.org/extend/plugins/stealth-login/

WordPress bloggers must install google gears

So many times we saw the turbo link in the upper right corner of the admin area. (Remember when computers used to have a Turbo button?) Behind it is a new feature of WordPress.com, support for Gears.

Wordpress bloggers must install google gears
WordPress bloggers must install google gears

In 2007 Google launched its new product called Google Gears. Google Gears makes it easy to browse the Internet, or sites you have already visited so you can visit them again using an amazing cache technology.

Here I will explain you what is google gears is doing. When we open our worpdpress admin, wordpress will download common images, JavaScript libraries and CSS files.

If we use the google gears then WordPress will store all these ’static’ files on your hard drive and will not download them from the web server. Means each time you are opening the WordPress admin panel that time wordpress will not download the common images and css and javascript. Thus, it should speed up things especially if you are on a slow connection.

Following article which is written by Matt (Founder of WordPress) is really helpful about knowing about wordpress and google gears.

http://en.blog.wordpress.com/2008/07/02/gears/

You can go to following URL and download google gears and install it on your local machine.

Google Gears

The Google gears is available for  Windows, Windows Mobile, Mac OS Tiger/Leopard, Mac OS Snow Leopard, Linux and Android.

So just install it your local computer.

how to create wordpress pages by script

For plugin or custom wordpress theme we need create wordpress pages by script. In this article we given code to how to create wordpress pages by script.

how to create wordpress pages by script

You can create the pages by using script. You can execute any script using the “$wpdb->insert” or

“$wpdb->query”.

For creating wordpress post or page you can use the wp_insert_post function. Before calling wp_insert_post() it is necessary to create an object (typically an array) to pass the necessary elements that make up a post. The wp_insert_post() will fill out a default list of these but the user is required to provide the title and content otherwise the database write will fail.

You can use following code for creating the page.  This code will execute when only you are logged in to wordpress.

// Create post object
  $my_post = array();
  $my_post['post_title'] = 'My post';
  $my_post['post_content'] = 'This is my post.';
  $my_post['post_status'] = 'publish';
  $my_post['post_author'] = 1;
  $my_post['post_type'] = 'page',
  $my_post['post_category'] = array(8,39);

// Insert the post into the database
  wp_insert_post( $my_post );

You can create wordpress plugin and use this code. Use following code for wordpress plugin. Copy the code and create the create_wordpress_pages.php file and put in plugin folder.

/*
Plugin Name: Create WordPress Pages
Plugin URI: http://images.purabtech.in/
Description: Create wordpress pages by script
Version: 1.0
Author: wordpressapi
Author URI: http://images.purabtech.in/
*/
function create_wordpress_pages(){
// Create post object
  $my_post = array();
  $my_post['post_title'] = 'My post';
  $my_post['post_content'] = 'This is my post.';
  $my_post['post_status'] = 'publish';
  $my_post['post_author'] = 1;
  $my_post['post_type'] = 'page',
  $my_post['post_category'] = array(8,39);

// Insert the post into the database
  wp_insert_post( $my_post );
}
register_activation_hook(__FILE__, 'create_wordpress_pages');
how to create wordpress pages by script
how to create wordpress pages by script

how to display all months wordpress posts on homepage

In this article, we will show you how to retrieve wordpress posts within specific time frame and display all months wordpress post. code snippet with detail.

how to display all months wordpress posts on homepage

how to display all months wordpress posts on homepage
how to display all months wordpress posts on homepage

WordPress is providing as full proof wordpress api so we can achieve the this using wordpress api only.

You can use the query_posts() function for fetching the wordpress posts. Open your index.php from your wordpress template folder. Before loop just use the following code for in index.php file.


$current_date = getdate();
query_posts(monthnum=' .$current_date["mon"] );

You can pass the multiple parameters to query_posts function as per your requirement.

  • hour= – hour (from 0 to 23)
  • minute= – minute (from 0 to 60)
  • second= – second (0 to 60)
  • day= – day of the month (from 1 to 31)
  • monthnum= – month number (from 1 to 12)
  • year= – 4 digit year (e.g. 2009)
  • w= – week of the year (from 0 to 53)

Now I will show the another example. which will show only current date post on home page.


$current_date = getdate();
query_posts('year=' .$current_date["year"] .'&monthnum=' .$current_date["mon"] .'&day=' .$current_date["mday"] );

Another good example for fetching the 30 days latest post from wordpress

<!--?php
//based on Austin Matzko's code from wp-hackers email list
  function filter_where($where = '') {
    //posts in the last 30 days
    $where .= " AND post_date --> '" . date('Y-m-d', strtotime('-30 days')) . "'";
    return $where;
  }
add_filter('posts_where', 'filter_where');
query_posts($query_string);
?>

Using above code you can display all months wordpress posts.

Solved: issue with wordpressMU image or media uploading

Many people is having issues with image uploading with WordPressMU. In this article I will tell you the solution. Solved: issue with wordpressMU image or media uploading.

Solved: issue with wordpressMU image or media uploading
Solved: issue with wordpressMU image or media uploading

Solved: issue with wordpressMU image or media uploading

Single wordpress and wordpressMu has different behaviour.. If you are using apache webserver then there is no issue but if you are using than there is issue with image mapping. specially with nginx web server.
You need to write the rewrite rule for image. Also you need to check about folder permission in your “blogs.dir” directory. change folder permission to 777.
For single wordpress there is uploads folder is present but for wordpressMu there is no uploads folder present in directory. You should look for blogs.dir folder which is present in wordpressMu/wp-content folder.

For solve the issue add the following lines in your apache configuration(httpd.conf) file. If this file is not present then add following lines in your .htaccess file.

RewriteEngine On
RewriteBase /

#uploaded files
RewriteRule ^(.*/)?files/$ index.php [L]
RewriteCond %{REQUEST_URI} !.*wp-content/plugins.*
RewriteRule ^(.*/)?files/(.*) wp-content/blogs.php?file=$2 [L]

# add a trailing slash to /wp-admin
RewriteCond %{REQUEST_URI} ^.*/wp-admin$
RewriteRule ^(.+)$ $1/ [R=301,L]

RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule . - [L]
RewriteRule  ^([_0-9a-zA-Z-]+/)?(wp-.*) $2 [L]
RewriteRule  ^([_0-9a-zA-Z-]+/)?(.*\.php)$ $2 [L]
RewriteRule . index.php [L]

&lt;IfModule mod_security.c&gt;
&lt;Files async-upload.php&gt;
SecFilterEngine Off
SecFilterScanPOST Off
&lt;/Files&gt;
&lt;/IfModule&gt;

One another issue.
You should check the “Upload File Types” settings going here.
http://yoursite/wp-admin/wpmu-options.php

Check the “Max upload file size” and also check the “Blog upload space”

For Nginx you will find the configuration code from https://purabtech.in/image-upload-issue-in-wordpressmu-with-nginx/

If you have still facing issue with image uploading in wordpressMU. Feel free to write to me or write a comment.

wordpress create archive page for wordpress theme

Given code for wordpress create archive page for wordpress theme. search engine sites will look for two main file. First sitemap.xml file and archive page. First I would say somthing about archive page in any website. Archive page is very important for SEO purpose.

Create an Archive Page in your WordPress theme

If you do not having archive page in your website so you are lacking somewhere in SEO for your website.  What I am going to show in this article, How to create the archive page for worpdress theme or website.  In wordpress creating the archive is very easy.

Follow my steps to create the archive page in wordpress site.

  • Open your wordpress theme folder and copy the single.php as named archive.php
    Note: dont copy index.php file as archive.php. It may have different UI and programming attribute in index.php.
  • Then copy archive.php as archive_template.php new file.
  • Open the archive_template.php file and copy paste the following code in top of file.
<?php
/*
Template Name: Archives Page
*/
?>
  • Again open the archive_template.php file and find the post loop. Between the loop copy past the following code.
<h1> All Archives in dropdown</h1>
<select name="archive-dropdown" onChange='document.location.href=this.options[this.selectedIndex].value;'>
 <option value=""><?php echo attribute_escape(__('Select Month')); ?></option>
 <?php wp_get_archives('type=monthly&format=option&show_post_count=1'); ?> </select>

<br><br>
<h1> All category</h1>
<ul><?php wp_list_cats('sort_column=name&optioncount=1') ?></ul>

<br><br>
<h1> All Archives</h1>
<ul><?php wp_get_archives('type=monthly&show_post_count=1') ?></ul>

Above code will give you the archive with dropdown and archive with category name with post count and archive with month and post count.

  • After doing this upload two new files to server in wordpress theme folder (archive.php and archive_template.php).
  • Go to your wordpress Admin panel using browser
  • Create the new page with title “Archive” and put some information about your website in that page.(Dont publish the page wait and follow next steps.)
wordpress create archive page for wordpress theme
wordpress create archive page for wordpress theme
  • Check the template section (right side under attribute section). Choose archive page as template and publish and save the page.

That sit. You are able to see the archive page for your website.