0

Description: I am facing an issue with my WordPress plugin where the table creation process upon activation is not working as expected. Here are the details:

What I Tried:

Checked the SQL query: Verified that the SQL query for table creation works when executed manually. Debugged with echo: Added an echo statement to output the table name during the activation process to ensure it's correctly generated. Reviewed the debug log: Checked the WordPress debug log for any errors during the activation process. Expectation: I expected the table creation to be successful, and the echo statement to display the table name in the debug log.

Actual Result: The table is not being created, and there is no output from the echo statement. The debug log also does not show any errors related to the table creation process.

Relevant Code: Here are the relevant portions of my code:

Main file (comment-reactions-plugin.php):

// ... (plugin initialization code)

// Include plugin-setting.php and db.php
require plugin_dir_path(__FILE__).'inc/plugin-setting.php';
require plugin_dir_path(__FILE__).'inc/db.php';
db.php (included file):

// ... (error reporting and constant checks)
// Creating tables in WordPress
function wp_comment_reactions() {
    global $wpdb;
    $table_name = $wpdb->prefix . "comment_reactions_plugin";
    echo "Table Name: $table_name";  // This line is not producing any output
    $charset_collate = $wpdb->get_charset_collate();
    $sql = "CREATE TABLE $table_name (
        id mediumint(9) NOT NULL AUTO_INCREMENT,
        time timestamp DEFAULT CURRENT_TIMESTAMP NOT NULL,
        user_id mediumint(9) NOT NULL,
        post_id mediumint(9) NOT NULL,
        like_count mediumint(9) NOT NULL,
        PRIMARY KEY  (id)
    ) $charset_collate;";

    require_once(ABSPATH . 'wp-admin/includes/upgrade.php');
    $result = dbDelta($sql);
    if ($result === false) {
        echo $wpdb->last_error;  // This line does not produce any output in my case
    }
}
register_activation_hook(__FILE__, 'wp_comment_reactions');

I appreciate any insights into what might be causing this issue. Thank you!

1 Answer 1

1

Not sure if this helps but try doing this in the main plugin file:

require plugin_dir_path(__FILE__).'inc/plugin-setting.php';
require plugin_dir_path(__FILE__).'inc/db.php';

// Register activation hook in the main plugin file
register_activation_hook(__FILE__, 'wp_comment_reactions');

Move your function wp_comment_reactions to the db.php file and call it from the main plugin file.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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