0

I am trying to create table on plugin activation, I tried making the same function inside the class and calling it from the constructor, and I tried calling it outside the class and finally i tried to make another php file for calling it using the register_activation_hook, but nothing worked

plugin-code:

<?php
/**
*@package pageauthors
**/

/** 
* Plugin Name: Page Authors
* Plugin URI: https://www.localhost.com/plugin
* Description: Allows you to add multiple authors to your wordpress post.
* Version: 1.0.0 
* Author: Raphael Eid.
* Author URI: https://www.localhost.com/
* License: GPL v2 or Later 
* Text Domain: PageAuthors
*/

$db = mysqli_connect('localhost','x','x','x');
//We can use require_once('../../../wp-config.php');
//mysql_connect(DB_HOST, DB_USER, DB_PASSWORD);

defined('ABSPATH') or die('Error my friend');

class pageauthors{

    function register(){
        //Call the Scripts
        add_action('admin_enqueue_scripts', array($this, 'enqueue'));

        //Create in the admin function
        add_action('admin_menu', array($this, 'add_admin_pages'));
    }   
    
    public function add_admin_pages(){
        //add_menu_page( string $page_title, string $menu_title, string $capability, string $menu_slug, callable $function = '', string $icon_url = '', int $position = null )
        add_menu_page('Add Author', 'Add Author', 'manage_options', 'raphaelprefix_plugin', array($this, 'admin_index'), 'dashicons-insert', 110);
    }

    public function admin_index(){
        define( 'MY_PLUGIN_PATH', plugin_dir_path( __FILE__ ) );
        include( MY_PLUGIN_PATH . 'templates/index.php');
        // include( MY_PLUGIN_PATH . 'index.php');
        include( MY_PLUGIN_PATH . 'templates/showAuthors.php');
        // require_once plugin_dir_path(__FILE__) . 'templates/index.php';
    }    

    function deactivate(){
        global $wpdb;
        $table_name = $wpdb->prefix . 'postsauthors';
        $sql = "DROP TABLE IF EXISTS $table_name";
        $wpdb->query($sql);
        delete_option("my_plugin_db_version");
    }

    function enqueue(){
        //Here we can enqueue and we should create the assets (css and js)
        wp_enqueue_style('mypluginstyle', plugins_url('/assets/mystyle.css', __FILE__) );
        wp_enqueue_script('mypluginstyle', plugins_url('/assets/mystyle.js', __FILE__) );
    }

}

if(class_exists('pageauthors')){
    $raphplugin = new pageauthors();
    $raphplugin->register();
    // $raphplugin->activate();
    $raphplugin->deactivate();

}

function installer(){
    include('installer.php');
}

//Activate - Register Activation Hook(__FILE__, array($instance, function))
register_activation_hook(__FILE__, 'installer');

//Deactivate - Register Deactivation Hook(__FILE__, array($instance, function))
register_deactivation_hook(__FILE__, array($raphplugin, 'deactivate') );

?>

installer.php

<?php
$db = mysqli_connect('localhost','x','x','x');
    global $wpdb;
    require_once(ABSPATH . 'wp-admin/includes/upgrade.php');

    if (count($wpdb->get_var('SHOW TABLES LIKE "wp_postsauthors"')) == 0){

        $sql = "CREATE TABLE `wp_postsauthors` 
        ( `ID` INT(200) NOT NULL AUTO_INCREMENT , `post_ID` INT(200) NOT NULL , 
        `author_name` VARCHAR(200) NOT NULL , `title` VARCHAR(200) NOT NULL , 
        `description` VARCHAR(200) NOT NULL , PRIMARY KEY (`ID`)) ENGINE = InnoDB;";

dbDelta($sql);

}

Nothing is working even tho I am typing everything correctly

3
  • 1
    dbDelta is extremely fussy about how the SQL is formatted, did you go through each requirement in the documentation step by step? I notice you do not have the correct number of spaces around the primary key statement. dbDelta also checks if the table exists so the if check with the SHOW TABLES query is unnecessary. Also, if your plugin is deactivated by accident the user will suffer total data loss, you should not drop the table on deactivation – Tom J Nowell Apr 6 at 14:47
  • 1
    Also you don't need a custom table to implement multiple authors. Coauthors/Publishpress authors/Authorship don't use a custom table to do this, relying instead on post meta or taxonomies – Tom J Nowell Apr 6 at 14:49
  • BTW. Do never put your code in php file like you did in installer.php. It’s plain code and you include it inside a function, assuming only you will call it. But this file is public - if anyone will request it directly, the code will run... And no - you don’t need to create your own MySQL connection... – Krzysiek Dróżdż Apr 7 at 6:31

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.