prefix . 'vincent_shortner'; $charset_collate = $wpdb->get_charset_collate(); $sql = "CREATE TABLE $table_name ( id mediumint(9) NOT NULL AUTO_INCREMENT, long_url text NOT NULL, short_code varchar(10) NOT NULL, created_at datetime DEFAULT CURRENT_TIMESTAMP NOT NULL, usage_count int DEFAULT 0 NOT NULL, PRIMARY KEY (id), UNIQUE KEY short_code (short_code) ) $charset_collate;"; require_once(ABSPATH . 'wp-admin/includes/upgrade.php'); dbDelta($sql); // Check if usage_count column exists, if not, add it $row = $wpdb->get_results("SELECT COLUMN_NAME FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_SCHEMA = '" . DB_NAME . "' AND TABLE_NAME = '$table_name' AND COLUMN_NAME = 'usage_count'"); if (empty($row)) { $wpdb->query("ALTER TABLE $table_name ADD usage_count int DEFAULT 0 NOT NULL"); } } register_activation_hook(__FILE__, 'vincent_shortner_install_or_upgrade'); add_action('plugins_loaded', 'vincent_shortner_install_or_upgrade'); // Add menu item to the admin panel function vincent_shortner_menu() { add_menu_page('Vincent Shortner', 'Vincent Shortner', 'manage_options', 'vincent-shortner', 'vincent_shortner_page', 'dashicons-admin-links'); } add_action('admin_menu', 'vincent_shortner_menu'); // Check if URL already exists or conflicts with WordPress permalinks function vincent_shortner_url_exists($long_url, $short_code) { global $wpdb; $table_name = $wpdb->prefix . 'vincent_shortner'; // Check if the long URL already exists $existing_url = $wpdb->get_var($wpdb->prepare("SELECT id FROM $table_name WHERE long_url = %s", $long_url)); if ($existing_url) { return 'The long URL already exists in the system.'; } // Check if the short code conflicts with WordPress permalinks $page = get_page_by_path($short_code); if ($page) { return 'The short code conflicts with an existing url.'; } return false; } // Admin page content function vincent_shortner_page() { global $wpdb; $table_name = $wpdb->prefix . 'vincent_shortner'; $message = ''; // Handle form submission if (isset($_POST['vincent_shortner_submit'])) { $long_url = esc_url_raw($_POST['long_url']); $short_code = sanitize_text_field($_POST['short_code']); if (empty($short_code)) { $short_code = substr(md5(uniqid()), 0, 6); } $url_exists = vincent_shortner_url_exists($long_url, $short_code); if ($url_exists) { $message = '
' . $url_exists . '
Short URL created successfully.
Short URL deleted successfully.
| Long | Short | Date | Clicks | Copy | Delete |
|---|---|---|---|---|---|
| created_at); ?> | usage_count); ?> | Delete |