| 1 |
<?php |
| 2 |
|
| 3 |
/* |
| 4 |
Plugin Name: wpda test |
| 5 |
*/ |
| 6 |
|
| 7 |
/* |
| 8 |
* This is a mini plugin showing how to use Wp Data Access directly from your php code. |
| 9 |
* |
| 10 |
* Following these steps: |
| 11 |
* (1) Copy this example file to your plugin folder and remove the .txt file extention. |
| 12 |
* (2) Make sure WP Data Access plugin is installed and activated. |
| 13 |
* (3) Grant access to a table you like to use for this test: Wp Data Access > Manage Plugin > Back-end Settings. |
| 14 |
* (4) Change 'wp_customers' (table_name) to the name of the table you selected in the previous step. |
| 15 |
* (5) Activate wpda test plugin. |
| 16 |
* (6) Check your WP Data Access menu (or other menu if you changed the code below). |
| 17 |
* (7) Sub menu "Added by code" should be available with list table and simple form (if activated). |
| 18 |
*/ |
| 19 |
|
| 20 |
function add_wpda_menu() { |
| 21 |
|
| 22 |
// (2) Add your menu |
| 23 |
add_action('admin_menu', 'add_app_menu'); |
| 24 |
|
| 25 |
} |
| 26 |
|
| 27 |
function add_app_menu(){ |
| 28 |
|
| 29 |
if (!class_exists('WPDataAccess\List_Table\WPDA_List_View')) { |
| 30 |
// Is WP Data Access deactivated or uninstalled? |
| 31 |
return; |
| 32 |
} |
| 33 |
|
| 34 |
// (3) Create a (sub) menu handle |
| 35 |
// Works for add_menu_page and add_submenu_page equally |
| 36 |
$wpda_table_list_menu = add_submenu_page( |
| 37 |
'wpda', // Set to your own menu slug |
| 38 |
'WPDA menu added by code', // Your page title |
| 39 |
'Added by code', // Your menu title |
| 40 |
'manage_options', // Set your authorization here |
| 41 |
'wpda_test', // Menu slug of your page |
| 42 |
'show_page' // Calls function below |
| 43 |
); |
| 44 |
|
| 45 |
global $wpda_table_list_view; |
| 46 |
// (4) Add Instantiate class WPDA_List_View |
| 47 |
// This allows you to set screen options |
| 48 |
// If screen options are not available, something went wrong here... |
| 49 |
$wpda_table_list_view = new WPDataAccess\List_Table\WPDA_List_View( |
| 50 |
[ |
| 51 |
'page_hook_suffix' => $wpda_table_list_menu, // Handle to (sub) |
| 52 |
'table_name' => 'wp_customers' // Database table in your wordpress schema (CHECK!!!) |
| 53 |
] |
| 54 |
); |
| 55 |
|
| 56 |
} |
| 57 |
|
| 58 |
function show_page(){ |
| 59 |
|
| 60 |
global $wpda_table_list_view; |
| 61 |
// (5) Show menu page |
| 62 |
$wpda_table_list_view->show(); |
| 63 |
|
| 64 |
} |
| 65 |
|
| 66 |
// (1) Wait untill all plugins are loaded |
| 67 |
// You need to do this to get access to WP Data Access classes |
| 68 |
add_action('plugins_loaded', 'add_wpda_menu'); |
| 69 |
|