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 . '

'; } else { $wpdb->insert( $table_name, array( 'long_url' => $long_url, 'short_code' => $short_code, 'usage_count' => 0 ) ); $message = '

Short URL created successfully.

'; } } // Handle delete action if (isset($_GET['action']) && $_GET['action'] == 'delete' && isset($_GET['id'])) { $id = intval($_GET['id']); $wpdb->delete($table_name, array('id' => $id)); $message = '

Short URL deleted successfully.

'; } // Fetch all short URLs $short_urls = $wpdb->get_results("SELECT * FROM $table_name ORDER BY created_at DESC"); // Display the admin page ?>

Vincent Shortner

Existing Short URLs

Long Short Date Clicks Copy Delete
long_url); ?> short_code); echo '/' . esc_html($url->short_code) . ''; ?> created_at); ?> usage_count); ?> Delete
prefix . 'vincent_shortner'; $request_uri = trim($_SERVER['REQUEST_URI'], '/'); if (!empty($request_uri)) { $short_code = sanitize_text_field($request_uri); $result = $wpdb->get_row($wpdb->prepare("SELECT id, long_url, usage_count FROM $table_name WHERE short_code = %s", $short_code)); if ($result) { // Update usage count $wpdb->update( $table_name, array('usage_count' => $result->usage_count + 1), array('id' => $result->id) ); // Redirect to the long URL wp_redirect(esc_url_raw($result->long_url)); exit; } } } add_action('init', 'vincent_shortner_redirect');