Skip to main content

Posts

Showing posts from June, 2024

WordPress custom menu page, fetch data from custom MySQL table and export to CSV

 To create a custom menu page in WordPress, retrieve custom table data from MySQL, and display it with the ability to export to CSV/Excel, you can follow these steps: 1. Create a custom table in your WordPress database to store your data. You can use the $wpdb global variable to interact with custom tables in WordPress. Here's an example of creating a custom table: <?php global $wpdb ; $table_name = $wpdb -> prefix . 'custom_data' ; $sql = "CREATE TABLE IF NOT EXISTS $table_name ( id INT(11) NOT NULL AUTO_INCREMENT, name VARCHAR(255) NOT NULL, email VARCHAR(255) NOT NULL, phone VARCHAR(20) NOT NULL, PRIMARY KEY (id) ) $charset_collate ;" ; require_once (ABSPATH . 'wp-admin/includes/upgrade.php' ); dbDelta( $sql ); ?> 2. Add the following code to your theme's functions.php file or create a custom plugin file to define the custom menu page: <?php // Add menu page function custom_menu_page () {