First of all, I want to say sorry for this very big question, but all these questions were disturbing my little brain (or no brain!) for the past couple of days as I was not getting any suitable answer. I will request to first read this slowly, and whoever knows any part of it, please try to answer it for that part only. I will really be grateful to anybody who answers any part of it.
I am developing a Plugin (using OOP) where Players can get registered here & play some sort of rugby game online. For this I created a database table (wp_rugby_players
) in such a way that when using the API of registering Hooks, this table will automatically get created & dropped when activating & deactivating the Plugin, respectively.
However, I must mention that this sort of creating database table is a bad way of using WordPress as it itself has provided lots of options (which include using Options, Custom Fields, Taxonomies, ...) to be used for such purposes.
Still as this is my first time, I would like to know how to proceed in such cases using OOP way of programming WordPress plugins. Problem is my registration of hooks isn't working totally, and there are some problems arising. So if anybody could help me out please, it will be just great. Following is my code snippet:-
// Placeholder - #1
class class_play_rugby_game
{
function __construct() {
// Placeholder - #2
register_activation_hook( __FILE__, array( &$this, 'database_setup' ) );
register_deactivation_hook( __FILE__, array( &$this, 'database_uninstall' ) );
add_action( 'admin_menu', array( &$this, 'admin_actions' ) );
}
function database_setup() {
global $wpdb;
// Placeholder - #3
$table_name = $wpdb->prefix . "top_rugby_players";
$current_year = date('Y');
if($wpdb->get_var("SHOW TABLES LIKE '$table_name'") != $table_name) {
$sql = "CREATE TABLE `".$table_name."`......"; // Database Table Creation Code correct, as I have checked it using "phpMyAdmin"
/**
* This code snippet is actually taken from a non-OOP Plugin code,
* but still I would want to use it in case anything goes wrong.
*/
if( !require_once(ABSPATH . 'wp-admin/includes/upgrade.php') ) {
die('Foolish Plugin has added its own maybe_upgrade* Function');
}
// Placeholder - #4
dbDelta($sql);
$welcome_name = "Mr. Wordpress";
$welcome_text = "Congratulations, you just completed the installation!";
// Just for testing
$rows_affected = $wpdb->insert( $table_name, array(
'name' => $welcome_name,
'time' => current_time('mysql'),
'text' => $welcome_text
) );
}
}
function database_uninstall() {
global $wpdb;
// Placeholder - #5
$table_name = $wpdb->prefix . "top_rugby_players";
$wpdb->query("DROP TABLE IF EXISTS $table_name");
}
function admin_actions() {
// Placeholder - #6
if( function_exists('add_menu_page') ) {
add_menu_page( __("Play Rugby Game"), __("Top Rugby Players"), 'edit_plugins', "list-top-players" );
}
// Placeholder - #7
if( function_exists('add_submenu_page') ) {
add_submenu_page( "list-top-players", __("List of Top Players"), __("Top Rugby Players"), 'edit_plugins', "list-top-players", array($this, "list_top_players_page"));
add_submenu_page( "list-top-players", __("Add Top Players"), __("Add New"), 'edit_plugins', "add-new-player-page", array($this, "add_new_player_page"));
}
}
}
$obj_play_rugby_game = new class_play_rugby_game();
Now I will mention all the problems, one by one.
Placeholder - #1
:-
Is there any issues with my Class naming structure? If yes, please let me know,so that I can follow the standards.
Placeholder - #2
:-
I am a little bit confused as to where & how to properly use the object of this class in the functions "register_activation_hook
" & "register_deactivation_hook
". Also are they syntactically positioned in a right manner inside the constructor?
Placeholder - #3
& Placeholder - #5
:-
There are multiple instances when I have to write same string (in this case table name) in each method, although I would very much like this to store in some class property instead. What would be the feasibility of this workaround for working with the APIs of "register_activation_hook
", "register_deactivation_hook
", "add_action
", "add_filter
", "add_menu_page
", and "add_submenu_page
"? Will the value of the class properties be available in these when calling the just-mentioned APIs.
Placeholder - #4
:-
How to know whether dbDelta($sql)
is working or not? If not, how to make it work in this class method?
Placeholder - #6
& Placeholder - #7
:-
I saw in a non-OOP plugin using these 2 checks "function_exists('add_menu_page')
" & "function_exists('add_submenu_page')
". Are these checks working & is it worthwhile to check these two APIs?
Placeholder - #6
& Placeholder - #7
:-
As I said before that there are multiple instances of same text here & there. Some more examples of them are the main menu's "menu_slug" parameter & the sub-menu's "parent_slug" parameter. Also same case goes for the menu's "capability" parameter. If possible, how can I use the class property values here, instead of directly writing the same text over & over again?
Some general questions:-
- I need to have a player registration form in the Admin panel, so I will require a view & controller & a model probably. So can anyone please suggest a possible way of doing this in a proper manner, as this is the most trivial part of this plugin?
- Whenever I'm activating the Plugin from the Plugin Manager area, I am getting the following message "
The plugin generated 585 characters of unexpected output during activation. If you notice “headers already sent” messages, problems with syndication feeds or other issues, try deactivating or removing this plugin.
" Can anybody please tell from the written above as to what can cause this sort of a warning message?
Anybody who knows any little part of it, please answer that part only. I am scratching my head over the past couple of days with no answer to these above questions. Please help me out. Thanks in advance.