0

i am new in wordpress plugin development, i need your guide in this,when i activating my plugin then activating but does not creating the any table in my database

this is my code i written oops base:|

<?php 
class AtiBlogTest {
    function register(){
        //for backend
        add_action( 'admin_enqueue_scripts', array($this,'backendEnqueue'));
        //for frontend
        add_action( 'wp_enqueue_scripts', array($this,'frontendEnqueue'));
    }
    function backendEnqueue(){
        wp_enqueue_script( 'bootstrap.min', get_stylesheet_directory_uri().'/css/bootstrap.min.css');
        wp_enqueue_script( 'jquery.min',  plugin_dir_url( __FILE__ ).'/js/jquery.min.js');
    }
    function frontendEnqueue(){

        // wp_enqueue_style( 'bootstrap',get_stylesheet_directory_uri().'/css/bootstrap.css');
        // wp_enqueue_script( 'bootstrap.min', get_stylesheet_directory_uri().'/css/bootstrap.min.css');
    }
    public function creating_database_table(){
    /*==============creating tabel in database after activation of plugin=================*/
        function create_plugin_database_table() {
         global $wpdb;
         $table_name = $wpdb->prefix . 'sandbox';
         $sql = "CREATE TABLE $table_name (
         id mediumint(9) unsigned NOT NULL AUTO_INCREMENT,
         title varchar(50) NOT NULL,
         structure longtext NOT NULL,
         author longtext NOT NULL,
         PRIMARY KEY  (id)
         );";

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

        register_activation_hook( __FILE__, 'create_plugin_database_table' );
    /*======================================end============================================*/
    }
}  

if(class_exists('AtiBlogTest')){
$atiblogtest=new AtiBlogTest();
$atiblogtest->register();
$atiblogtest->creating_database_table();
}
/*===================================ajax call for upload image==================================*/
add_action('wp_ajax_get_states_by_plugin_ajax','get_ajax_callback_image');
add_action('wp_ajax_nopriv_get_states_by_plugin_ajax','get_ajax_callback_image');
function get_ajax_callback_image(){
    //echo $_POST['uploadedFile'];
}

/*================================================================================================*/

   ?>

check please what can be problems or i am missing something here in code...

  • dbDelta() is very specific, so your SQL needs to be exact. $table_name should be wrapped with curly braces, I think. Like so: CREATE TABLE {$table_name}. – butlerblog Jan 1 at 20:20

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.