register activation hook using in wordpress plugin

wordpress hooks are very important for wp developers. We can use the wp hooks in wordpress plugin and themes. Here in this article I will show you how to register the hook in wordpress plugin. I given the simple code sample which will give you the fare idea about using the wordpress hooks.

register activation hook using in wordpress plugin

register activation hook using in wordpress plugin
register activation hook using in wordpress plugin

register_activation_hook is very important function for creating the wordpress plugin. The function register_activation_hook registers a plugin function to be run when the plugin is activated.
You can use the register_activation_hook as follows:

<!--?<span class="hiddenSpellError" pre="" data-mce-bogus="1"-->php register_activation_hook($file, $function); ?>

If you can going to create the wordpress simple plugin then this function is very useful. this function you can use as follows in files.
If you have a function called myplugin_activate() in the main plugin file at either

* wp-content/plugins/myplugin.php or
* wp-content/plugins/myplugin/myplugin.php

you can use code as follows:

global $myvar;
$myvar='whatever';

function myplugin_activate() {
  global $myvar;
  echo $myvar; // this will be 'whatever'
}

register_activation_hook( __FILE__, 'myplugin_activate' );

1. register_activation_hook() must be called from the main plugin file – the one that has “Plugin Name: …” directive.
2. Your hook function must be in the same file as well. From that function it is ok to call other functions defined in other files.
3. Doing echo “My hook called!”; – does not work from it. So this is not a good way to judge whether it working or not.
4. Your global variables must be explicitly declared as global for them to be accessed from inside of my_activation_hook().

Check the sample wordpress plugin code.

<!--?<span class="hiddenSpellError" pre="" data-mce-bogus="1"-->php
/*
Plugin Name: A Test
Description: A Test
*/

require_once (dirname(__FILE__) . '/my_other_file.php');

// This code *will not* work. Activation hook function must be defined in the main plugin file.
//    register_activation_hook (dirname(__FILE__) . '/my_other_file.php', 'my_other_function');

// This code will work.
register_activation_hook (__FILE__, 'test_activated');

// This is correct way to declare/access globals.
global $some_var;    // globals must be declared explicitly. Without this you will not be able to access '$some_var' from within 'test_activated()' function.
$some_var = 'hey';

//===========================================================================
function test_activated ()
{
   global $some_var; // Now it will be 'hey'.

   // This function is defined in 'my_other_file.php'
   my_other_function ();

   // This will not work, so don't try. If you need logging write something in temporary log file in here via fopen/fwrite.
	// If you want to quickly test if your activation hook works - put exit() into it. At least you'll see error during activation.
   echo 'test_activated called!';
}
//===========================================================================

?>

The function register_deactivation_hook registers a plugin function to be run when the plugin is deactivated.
following function will deactivate your plugin

register_deactivation_hook( __FILE__, 'myplugin_deactivate' );

wordpress theme help for wordpress developers

Many People want to create the wordpress theme. But they don’t know where to start and how to start. We given wordpress theme help for wordpress developers. For creating the wordpress theme only two files are important. First file is style.css file and index.php file if you create that only two files in your wordpress theme folder than also you can able to create the wordpress theme easily.

wordpress theme help for wordpress developers

Here I will show you which is necessary files in wordpress theme folder.

header.php - header section
index.php - main section
sidebar.php - sidebar section
footer.php - footer section
single.php - post template
page.php - page template
comments.php - comments template
search.php - search content
searchform.php - search form
archive.php - archive
functions.php - special functions
404.php - error page

If you have very basic knowledge of php then you are able to create Above files very easily. In index.php file you need the PHP loop for showing the wordpress posts.

<!--?<span class="hiddenSpellError" pre="" data-mce-bogus="1"-->php if(have_posts()) : ?>
   <!--?<span class="hiddenSpellError" pre="" data-mce-bogus="1"-->php while(have_posts()) : the_post(); ?>
<!--?<span class="hiddenSpellError" pre="" data-mce-bogus="1"-->php the_title(); ?>
<!--?<span class="hiddenSpellError" pre="" data-mce-bogus="1"-->php the_content(__('(more...)')); ?>
// Custom HTML & PHP code
   <!--?<span class="hiddenSpellError" pre="" data-mce-bogus="1"-->php endwhile; ?>
<!--?<span class="hiddenSpellError" pre="" data-mce-bogus="1"-->php else : ?>
<!--?<span class="hiddenSpellError" pre="" data-mce-bogus="1"-->php endif; ?>

Using above code you can show the wordpress posts in your wordpress theme. Just you need to put above code in right place where you want to show the multiple posts.

Then Cut the header part of index.php file and put in header.php file and put < ?php get_header(); ?>
instead of deleted header part.

Then Cut the sidebar part of index.php file and put in sidebar.php file and put < ?php get_sidebar(); ?>
instead of deleted sidebar part.

Then Cut the footer part of index.php file and put in footer.php file and put < ?php get_footer(); ?>
instead of deleted footer part.

In single.php file you need to copy paste the index.php file and then put following code in the loop for showing the comments.

<!--?<span class="hiddenSpellError" pre="" data-mce-bogus="1"-->php if(have_posts()) : ?>
   <!--?<span class="hiddenSpellError" pre="" data-mce-bogus="1"-->php while(have_posts()) : the_post(); ?>
<!--?<span class="hiddenSpellError" pre="" data-mce-bogus="1"-->php the_title(); ?>
<!--?<span class="hiddenSpellError" pre="" data-mce-bogus="1"-->php the_content(__('(more...)')); ?>
// Custom HTML & PHP code
   <!--?<span class="hiddenSpellError" pre="" data-mce-bogus="1"-->php endwhile;
comments_template();
?>
<!--?<span class="hiddenSpellError" pre="" data-mce-bogus="1"-->php else : ?>
<!--?<span class="hiddenSpellError" pre="" data-mce-bogus="1"-->php endif; ?>

Above tags we called as template tags in wordpress api and themes.
In header.php file following lines or code is important. For title, description for blog home, theme url following code is useful. Following tags we called as “Template Bloginfo Tags”.
Where you want to show the title use following code.

< ?php bloginfo('name'); ?> - Title of the blog
< ?php bloginfo('charset'); ?> - Displays the character set
< ?php bloginfo('description'); ?> - Displays the description of the blog
< ?php bloginfo('url'); ?> - Displays the address of the blog
< ?php bloginfo('rss2_url'); ?> - Displays the RSS URL
< ?php bloginfo('template_url'); ?> - Displays the URL of the template
< ?php bloginfo('pingback_url'); ?> - Displays the pingback URL
< ?php bloginfo('stylesheet_url'); ?> - Displays the URL for the template's CSS file
< ?php bloginfo('wpurl'); ?> - Displays URL for WordPress installation
< ?php bloginfo('name'); ?>

Common and very useful wordpress theme tags as follows.

< ?php the_time() ?> - Displays the time of the current post
< ?php the_date() ?> - Displays the date of a post or set of posts
< ?php the_title(); ?> - Displays or returns the title of the current post
< ?php the_permalink() ?> - Displays the URL for the permalink
< ?php the_category() ?> - Displays the category of a post
< ?php the_author(); ?> - Displays the author of the post
< ?php the_ID(); ?> - Displays the numeric ID of the current post
< ?php wp_list_pages(); ?> - Displays all the pages
< ?php wp_tag_cloud(); ?> - Displays a tag cloud
< ?php wp_list_cats(); ?> - Displays the categories
< ?php get_calendar(); ?> - Displays the calendar
< ?php wp_get_archives() ?> - Displays a date-based archives list
< ?php posts_nav_link(); ?> - Displays Previous page and Next Page links
< ?php next_post_link() ?> - Displays Newer Posts link
< ?php previous_post_link() ?> - Displays previous link

With this article I attached the very useful wordpress 3.0 help functions list. Please download that also.

WordPress 3.0 Cheat Sheet wordpress theme help for wordpress developers
wordpress functions and theme help wordpress theme help for wordpress developers

How do I get wordpress API Key

Many people are running there site with wordpress. For many reasons We need to WordPress api key. For using the wordpress stat and akismet plugin we need the wordpress api key. In this article we given details for get wordpress API Key

How do I get wordpress API Key

How to get the free wordpress api key?

Go to following URL:

http://akismet.com/personal/

How do I get wordpress API Key
How do I get wordpress API Key

Put your email address and click on register button.

How do I get wordpress API Key
How do I get wordpress API Key

Click on use Akismet for free! Link. Than fill all your information in the form and hit the Next button.

After submission you will see the following message.

You will get the wordpress api through wordpress in following format.

Thanks for choosing Akismet.

Your Akismet API key is: ------------

If you have any questions or concerns about your Akismet subscription, don't hesitate to ask:

http://akismet.com/contact/

Thanks,
The Akismet team.

Which IDE is best for wordpress development

Question is, Which IDE is best for wordpress development, Every programmer has his own choice of IDEs But I like the NetBeans so much for PHP, Java, Ruby on Rails, javascript Development. Being Programmer I like NetBeans so much.

Which IDE is best for wordpress development

NetBeans does not have any good think for wordpress still I do development in Netbeans. I know every wordpress developers first choice is Dreamweaver. I also used Dreamweaver for so many times. I can personally say Dreamweaver is really good tool for Web Designer. If you are having advanced knowledge of dreamweaver then you can develop any site in any language.

Dreamweaver is first choice for web designers. Here in this article I am going to talk about the best IDE for wordpress development.

Adobe DreamWeaver CS5

For WordPress development Dereamweaver is really great tool. You need to watch this video.

Dreamweaver is ever made IDE for wordpress development.

Netbeans

Which IDE is best for wordpress development
Which IDE is best for wordpress development

What I like about Netbeans is that is free. every update you got that is free. WordPress made in PHP and you will great support for PHP in netbeans. What you want more.  Only think I did not like about Netbeans is that is very slow on windows and linux also.

Eclipse

Eclipse is another IDE which is very popular among the programmers. I used eclipse for years. I like that so much. Eclipse has no support for wordpress but it has good support for PHP. So I like eclipse also.

How to add pagination in wordpress

Many times adding the custom pagination in wordpress blog is very important. Adding custom pagination in wordpress blog is good. In this article, we will show you, How to add pagination in wordpress with plugin and without wp plugin

How to add pagination in wordpress

  • Simple WordPress Default pagination

You need to open the your index.php file from wordpress theme and put following code in that file.

<div class="navigation">
<div class="alignleft"><?php next_posts_link('Previous') ?></div>
</div>
<div class="alignright"><?php previous_posts_link('Next') ?></div>

If you want the pagination in single post then you can use the following code:

<?php next_posts_link($label , $max_pages); ?>
<?php prev_posts_link($label , $max_pages); ?>

More pagination style visit above URL: wordpress pagination style without wordpress plugin

There are some very nice wordpress plugin available for wordpress pagination.

How to add pagination in wordpress
How to add pagination in wordpress

PageNavi wordpress plugin is really nice. I personally like this plugin so much. With this plugin you can apply your custom css also.

  • Changing the CSS

If you need to configure the CSS style of WP-PageNavi, you can copy the pagenavi-css.css file from the plugin directory to your theme’s directory and make your modifications there. This way, you won’t lose your changes when you update the plugin.

Alternatively, you can uncheck the “Use pagenavi.css?” option from the settings page and add the styles to your theme’s style.css file directly.

wordpress show latest post from each category

Many people want to show wordpress show latest post from each category posts from there selected categories or all categories on home page. Here we given code snippet for this.
I this article I will show how easily you can achieve this.

wordpress show latest post from each category

Please following code in your home page which is your theme’s index.php file.

&lt;!--?&lt;span class=&quot;hiddenSpellError&quot; pre=&quot;&quot; data-mce-bogus=&quot;1&quot;&gt;php&lt;/span&gt;--&gt;
$categories=get_categories($all_categories);
 foreach($categories as $category) {
 $post_args=array(
 'showposts' =--&gt; 1, // you can fetch number of articles from each category
 'category__in' =&gt; array($category-&gt;term_id),
 'caller_get_posts'=&gt;1
 );
 $posts=get_posts($post_args);
 if ($posts) {
 echo '&lt;/pre&gt;
Category: sprintf( __( &quot; href=&quot;' . get_category_link( $category-&gt;term_id ) . '&quot;&gt;name ) . '&quot; ' . '&gt;' . $category-&gt;name.'&lt;/pre&gt;
&lt;pre&gt; ';foreach($posts as $post) {&lt;/pre&gt;
&lt;pre&gt;
 setup_postdata($post); ?&gt;&lt;/pre&gt;
&lt;div&gt;

php the_title_attribute(); ?&gt;&quot; href=&quot;&lt;!--?php the_permalink() ?--&gt;&quot; rel=&quot;bookmark&quot;&gt;
 &lt;!--?&lt;span class=&quot;hiddenSpellError&quot; pre=&quot;&quot; data-mce-bogus=&quot;1&quot;&gt;php&lt;/span&gt; the_title(); ?--&gt;

&lt;!--?&lt;span class=&quot;hiddenSpellError&quot; pre=&quot;&quot; data-mce-bogus=&quot;1&quot;&gt;php&lt;/span&gt; the_excerpt(); ?--&gt;

&lt;/div&gt;
&lt;pre&gt;
 &lt;!--?&lt;span class=&quot;hiddenSpellError&quot; pre=&quot;&quot; data-mce-bogus=&quot;1&quot;&gt;php&lt;/span&gt;--&gt;
 } // foreach($posts
 } // if ($posts
 } // foreach($categories
?--&gt;

if you want to show the posts from specific articles then use following code.

&lt;!--?&lt;span class=&quot;hiddenSpellError&quot; pre=&quot;&quot; data-mce-bogus=&quot;1&quot;&gt;php&lt;/span&gt;--&gt;
$selected_categories=array(
 'include' =--&gt; '1,4,9',
 'order' =&gt; 'ASC'
 );

$categories=get_categories($selected_categories);foreach($categories as $category) {
 $post_args=array(
 'showposts' =&gt; 1, // you can fetch number of articles from each category
 'category__in' =&gt; array($category-&gt;term_id),
 'caller_get_posts'=&gt;1
 );
 $posts=get_posts($post_args);
 if ($posts) {
 echo '&lt;/pre&gt;
Category: sprintf( __( &quot; href=&quot;' . get_category_link( $category-&gt;term_id ) . '&quot;&gt;name ) . '&quot; ' . '&gt;' . $category-&gt;name.'&lt;/pre&gt;
&lt;pre&gt; ';foreach($posts as $post) {&lt;/pre&gt;
&lt;pre&gt;
 setup_postdata($post); ?&gt;&lt;/pre&gt;
&lt;div&gt;

php the_title_attribute(); ?&gt;&quot; href=&quot;&lt;!--?php the_permalink() ?--&gt;&quot; rel=&quot;bookmark&quot;&gt;
 &lt;!--?&lt;span class=&quot;hiddenSpellError&quot; pre=&quot;&quot; data-mce-bogus=&quot;1&quot;&gt;php&lt;/span&gt; the_title(); ?--&gt;

&lt;!--?&lt;span class=&quot;hiddenSpellError&quot; pre=&quot;&quot; data-mce-bogus=&quot;1&quot;&gt;php&lt;/span&gt; the_excerpt(); ?--&gt;

&lt;/div&gt;
&lt;pre&gt;
 &lt;!--?&lt;span class=&quot;hiddenSpellError&quot; pre=&quot;&quot; data-mce-bogus=&quot;1&quot;&gt;php&lt;/span&gt;--&gt;
 } // foreach($posts
 } // if ($posts
 } // foreach($categories
?--&gt;

How to clean up and optimize wordpress database

Keeping your wordpress database is very important for wordpress database. In this article we explained, How to clean up and optimize wordpress database. This will improve your site speed aslo. Many times you install the wordpress plugins and some time you dont want that plugins and you delete that plugins.

How to clean up and optimize wordpress database

There is very nice wordpress plugin is avilable for database backup.

WP-DB-Backup

WP-DB-Backup allows you easily to backup your core WordPress database tables. You may also backup other tables in the same database.
But that plugins create some tables in your wordpress database. You need to remove that tables from your wordpress database.
Imp note: when ever you are cleaning the database or deleting the unwanted tables from wordpress database. Please consult with your web administrator.
Dont forget to take a full backup of your database.

This tutorial should be forward-compatible with WordPress 3.0

  • Install WP-DB-Backup by Austin Matzko
  • Mouse over Tools so that the down arrow appears
  • Click the down arrow
  • Click Backup
  • You’ll see something like this:
How to clean up and optimize wordpress database
How to clean up and optimize wordpress database

On the left are the default database tables included with WordPress. All of these are included every time you backup. The only thing you have to decide here is whether to exclude spam comments from being backed up (I recommend this) and whether to exclude post revisions (I recommend excluding these too, unless you have a specific reason for keeping revisions).

On the right is a list of additional database tables, most of which were probably created by plugins. There’s also a table called that will end with the name “commentmeta” – this can be used by plugins.

If your plugins have created a lot of data that you would like to save, or you’ve spent a lot of time configuring particular plugins, you’ll want to backup these tables. Otherwise, you can keep your database backups smaller by not checking them.

  • Next we see the Backup Option. There are three choices here:

A. Save to the server

This will save a backup of your database as a file on your web server. I don’t recommend this.

B. Download to your computer.

This will create a database backup file that you can save to your local computer.

C. Email backup to:

This allows you to send a copy of the backup to any e-mail address you’d like.

  • Let’s go ahead and download a copy to our hard drives now.

Check options you want in the Tables section then click “Backup now!”

You should see a progress bar, and when that’s done your browser will prompt you to save the file. You can save this file wherever you’d like.

  • There’s another section called “Scheduled Backup.” This is where this program gets really great.

Here we can schedule a backup to be e-mailed to a particular e-mail address however often we’d like. I recommend checking selecting “Once Daily.”

  • On the right, you’ll see that list of optional tables again. Check the ones you want to backup every time the backup runs.
  • Enter the e-mail address you want the backups delivered to in the “Email backup to:” field.
  • Click “Schedule backup.”
  • You should now get a backup file as an e-mail attachment every day. You should save these attachments to your local computer. If you’re using Gmail or another web mail host that has a lot of storage space, you might want to leave your databases on their server as an additional off-site backup. Be aware that that’s an additional security risk – if your e-mail account is ever compromised, the would have access to all of your database backups.

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 the the_post_thumbnail In WordPress

Now WordPress in core the new post thumbnail function will not changed until. we have given info about How to use the the_post_thumbnail In WordPress.

use the the_post_thumbnail In WordPress

You can provide 4 picture formats to the function (change the width and height values to your need):

use the the_post_thumbnail In WordPress
use the the_post_thumbnail In WordPress
// the thumbnail
the_post_thumbnail(array(100,100));

// medium resolution
the_post_thumbnail(array(300,200));

// large resolution
the_post_thumbnail(array(600, 400));

// original
the_post_thumbnail();

You can set how the images should align. It is also possible to assign an own class:

//  left align
the_post_thumbnail(array(100,100), array('class' => 'alignleft'));

//  right align
the_post_thumbnail(array(100,100), array('class' => 'alignright'));

//  center
the_post_thumbnail(array(100,100), array('class' => 'aligncenter'));

// align right and the class  'my_own_class'
the_post_thumbnail(array(100,100), array('class' => 'alignright my_own_class'));

The 3rd possibility is the control of the images size with an array of height and width:
For this purpose we suppose that the settings for thumbnail is 150×150, for medium 300×200 and for large 600×400.

// thumbnail scaled to 60x60 pixel
the_post_thumbnail(array(60,60), array('class' => 'alignleft'));

// original thumbnail
the_post_thumbnail(array(150,150), array('class' => 'alignleft'));

// medium resolution scaled to 200x133 pixel
the_post_thumbnail(array(200,200), array('class' => 'alignleft'));

// large resolution scaled to 400x266 Pixel
the_post_thumbnail(array(400,345), array('class' => 'alignleft'));

We see that the image proportions are always maintained, even if one specifies crooked values.

For the Theme Designers is this not necessarily easier, because no one knows what the user will put in his settings o his library. One way to approach this problem, to query the options for the various sizes:

// width of the thumbnails
get_option('thumbnail_size_w');

//  height of the thumbnails
get_option('thumbnail_size_h');

//  height of the medium resolution
get_option('medium_size_h');

//  width of the large resolution
get_option('large_size_w');

//  1 = Crop thumbnail to exact dimensions, 0 = Crop off
get_option('thumbnail_crop')

You can change these values in your theme.

$w = get_option('thumbnail_size_w') / 2;
$h = get_option('thumbnail_size_h') /2;

the_post_thumbnail(array($w, $h), array('class' => 'alignleft'));

Here another example: If the size of a thumbnail is bigger than 100×100 and crop is activated, then the thumbnail should be resized to 100×100, otherwise use the original thumbnail.

if(get_option('thumbnail_size_w') > 100 && get_option('thumbnail_crop') == 1) {
    the_post_thumbnail(array(100,100));
}else{
    the_post_thumbnail('thumbnail');
}

What Matt is saying?

I wouldn’t recommend anyone use the named arguments for the_post_thumbnail, like ‘thumbnail’, could you remove those from your tutorial.

Inspired by

increase the upload_max_filesize in WordPress

The default upload file size is in php settings is 2 MB and for wordpress also default upload size is 2 MB. File upload size is dependent on PHP settings of your server. Upload size is also dependent on your hosting plan or hosting service provider. For shared hosting most of time you cannot increase the file upload limit.

increase the upload_max_filesize in WordPress

When we try to upload big size image or media files in wordpress we got the following error

The uploaded file exceeds the upload_max_filesize directive in php.ini
For fixing the issue use following steps
Use the php.ini file from root folder.

1. Find this line in the php.ini file in your php installation upload_max_filesize = 2MB and replace it with a higher value (e.g. upload_max_filesize = 64MB)
2. Search for this line in your php.ini file post_max_size and increase it.

If you don’t have a php.ini file in your directory,
you can usually generate one from the control panels of your host. or create the file and put following values in that
memory_limit = 32M
upload_max_filesize = 100M
upload_max_filesize = 100M
post_max_size = 100M

increase the upload_max_filesize in WordPress
increase the upload_max_filesize in WordPress

This file is copied into your wp-admin folder, the problem should be solved.