0
votes

I am making my very first wordpress plugin in that I want to make table in my database when I activate the plugin. I write the code exactly what wordpress community said but it's not working. I tried the same several times but the results is the same. I also want to know my plugin showing 2 submenus in my main plugin menu, It's like

My Plugin -- My Plugin -- My Submenu Page

Please help I am very new to plugin development.

// Registering plugin
register_activation_hook(__FILE__, 'myplugin_activate');

//Deactivate plugin

register_deactivation_hook(__FILE__, 'myplugin_deactivate');

function myplugin_activate() {

    global $wpdb, $table_prefix;
    global $favoritethis_db_version;


    $table_name = $table_prefix.
    'my-plugin-table';

    $charset_collate = $wpdb - > get_charset_collate();

    if ($wpdb - > get_var("show tables like '$table_name'") != $table_name) {
        require_once(ABSPATH.
            'wp-admin/upgrade-functions.php');
        $sql = "CREATE TABLE ".$table_name.
        " (
        id mediumint(9) NOT NULL AUTO_INCREMENT,
            time datetime DEFAULT '0000-00-00 00:00:00'
        NOT NULL,
        name tinytext NOT NULL,
        text text NOT NULL,
        url varchar(55) DEFAULT ''
        NOT NULL,
        UNIQUE KEY id(id)
    ) $charset_collate;
    ";
}
require_once(ABSPATH.
    'wp-admin/includes/upgrade.php');
dbDelta($sql);

}

function myplugin_deactivate() {

    // Deactivation code here...
    echo "<script>alert('Ohhh.. no baby the plugin is deactivated now..')</script>";
}
2
What's the excellently you want ? solve submenu issue or anything else ? - user2439481
thanks budy I found the solution of the both at myself. - JD_bravo

2 Answers

1
votes

also I found the solution of the problem, I was facing as getting extra sub menu as same name as the main menu is.

Solution

//Main admin menus
add_action('admin_menu', 'add_my_custom_menu');

function add_my_custom_menu() {

    //add an item to the menu
    add_menu_page(
        'My Plugin',
        'My Plugin',
        10,
        plugin_dir_path(__FILE__).
        'admin/plugin-form.php',
        '',
        plugin_dir_url(__FILE__).
        'img/contact.png'
    );
    add_submenu_page(
        'my-plugin-name/admin/plugin-form.php',
        'Plugin Setting',
        'Plugin Setting',
        10,
        plugin_dir_path(__FILE__).
        'admin/plugin-form.php',
        'myplugin_options_page'
    );
    add_submenu_page(
        'my-plugin-name/admin/plugin-form.php',
        'Plugin Entries',
        'Plugin Entries',
        10,
        plugin_dir_path(__FILE__).
        'admin/entries.php',
        ''
    );
}

reason is that every first sub-menu's destination page/function will be the same as main menu destination.

0
votes

ok I got the answer myself

// Registering plugin
register_activation_hook(__FILE__, 'myplugin_activate');

//Deactivate plugin
register_deactivation_hook(__FILE__, 'myplugin_deactivate');


function myplugin_activate() {

    global $wpdb;
    $table_name = $wpdb - > prefix.
    'my-plugin-table';


    //nstalled_ver = get_option('my-voting-version');

    if ($wpdb - > get_var("show tables like '$table_name' ") != $table_name) {
        require_once(ABSPATH.
            'wp-admin/upgrade-functions.php');
        $sql = "CREATE TABLE  IF NOT EXISTS `".str_replace('`', '', $table_name).
        "` (
        id mediumint(9) NOT NULL AUTO_INCREMENT,
            time datetime DEFAULT '0000-00-00 00:00:00'
        NOT NULL,
        name tinytext NOT NULL,
        text text NOT NULL,
        url varchar(55) DEFAULT ''
        NOT NULL,
        UNIQUE KEY id(id)
    );
    ";
}
require_once(ABSPATH.
    'wp-admin/includes/upgrade.php');
dbDelta($sql);
}