Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I am trying to create a small plugin but it not creating table in database when i activate the plugin. please have a look the code given below there is function function jal_install () it not called.

      <?php
        /*
        Plugin Name: My-Ads
        Plugin URI: http://wordpress.org/#
        Description: This is the most basic wordpress plugin, gets input from the user and displays it on every page.
        Author: shalu


       */

       /*when plugin is activated*/

       register_activation_hook(_FILE_,'my_first_install');

      /*when plugin is deactivated*/
      register_deactivation_hook( __FILE__, 'my_first_remove' );

      function my_first_install()
       {
         add_option("my_first_data", 'Testing !! My Plugin is Working Fine.', 'This is my first plugin panel data.', 'yes');

       }
       function my_first_remove() 
          {
        /* Deletes the database field */
        delete_option('my_first_data');

           }

           /*Code-Add Admin Panel Menu Item*/

      if(is_admin())
        {
               add_action('admin_menu','my_first_admin_menu');

           function my_first_admin_menu()
            {
             add_options_page('My First', 'Ads', 'administrator','my-first',        'my_first_plugin_page');
             }
            }



         function my_first_plugin_page() {
       ?>

         <div>
        <h2>Add your ads from here</h2>
          <form method="post" action="options.php" enctype="multipart/form-data">
             <?php wp_nonce_field('update-options'); ?>
          <table width="510">
      <tr valign="top">
        <th width="92" scope="row">Name:</th>
          <td width="406">
           <input name="my_first_data" type="text" id="my_first_data" value="<?php echo get_option('my_first_data'); ?>" /></td>
           </tr>
           <tr>
         <th width="92" scope="row">Target Url:</th>
         <td width="406">
          <input name="Target_Url" type="text" id="Target_Url" value="<?php echo get_option('Target_Url'); ?>" />
           </td>
          <tr>
          <tr>
         <th width="92" scope="row">Ad image:</th>
        <td width="406">
          <input name="file" type="file" id="file"/></td>
          </tr>
       </table>
      <input type="hidden" name="action" value="update" />
      <input type="hidden" name="page_options" value="my_first_data" />
        <p>
       <input type="submit" value="<?php _e('Save Changes') ?>" />
         </p>
        </form>
           </div>
           <?php

               }

          /* This calls my_first() function when wordpress initializes.*/
        function my_first()
       {

          echo get_option('my_first_data');
           }

       function jal_install () {
           global $wpdb;
              global $jal_db_version;

           $table_name = $wpdb->prefix . "liveshoutbox";
           echo $table_name;
            if($wpdb->get_var("show tables like '$table_name'") != $table_name) {

           $sql = "CREATE TABLE " . $table_name . " (
      id mediumint(9) NOT NULL AUTO_INCREMENT,
  time bigint(11) DEFAULT '0' NOT NULL,
  name tinytext NOT NULL,
  text text NOT NULL,
  url VARCHAR(55) NOT NULL,
  UNIQUE KEY id (id)
);";

  require_once(ABSPATH . 'wp-admin/includes/upgrade.php');
  dbDelta($sql);

  $rows_affected = $wpdb->insert( $table_name, array( 'time' => current_time('mysql'), 'name' => $welcome_name, 'text' => $welcome_text ) );

  add_option("jal_db_version", $jal_db_version);

            }
          }

        //calling the function
        register_activation_hook(__FILE__,'jal_install');

         ?> 

please help me where is the issue. I shall be very thankful you

share|improve this question
add comment

3 Answers 3

The first issue as AutoBLogged pointed out is that you've incorrectly referenced _FILE_ and not __FILE__ in the first activation hook.

There's a possible secondary problem, and i say possible because i've never had to use the activation or deactivation hooks extensively myself, and that's with calling register_activation_hook twice, once toward the top of the script, and once toward the bottom.

I'd assume(and i could be wrong here) that you can't create two seperate activation hooks for a single file. The solution should be simple though, take the code from both functions and move it into one, so you only need one call to register_activation_hook.

share|improve this answer
    
You are absolutely correct, @t31os! thanks a lot! –  Philip Mar 8 '12 at 10:44
add comment

It seems like you just copy-pasted the code from over here and expect it to simply work. Looking at how the single lines are messed up and at stuff like the _FILE_ issue or $rows_affected getting set but called nowhere, i'd say best recommendation is to simply read your code yourself again, try to clean it up and add some comments that act as inline "what does this line do"-protocol.

Further more no plugin should delete it's options during deactivation, but on uninstall. Also try to use function and var names that explain what stuff is or does.

Last line: Don't add tables to the database unless you're pretty sure about how stuff works. You could really mess up a lot.

share|improve this answer
add comment

Here's one problem:

register_activation_hook(_FILE_,'my_first_install');

That should be this:

register_activation_hook(__FILE__,'my_first_install');
share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.