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.
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_开发者_StackOverflow中文版_, 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
:-
register_activation_hook
" & "register_deactivation_hook
". Also are they syntactically positioned in a right manner inside the constructor?
Placeholder - #3
& Placeholder - #5
:-
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
:-
dbDelta($sql)
is working or not? If not, how to make it work in this class method?
Placeholder - #6
& Placeholder - #7
:-
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
:-
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.
The plugin generated 583 characters ...
During the activation process, you should not output any text - if you do, WordPress will display this message to the user. When developing a plugin, you should always do so with the WP_DEBUG define turned on in wp-config.php to ensure you see all problems and warning text displayed.
dbdelta() is a bit of a law unto itself - if you search for 'dbdelta sandbox' you should be able to find a way to run and test it on it's own. Obviously you can see if it's working when it creates/modifies your tables. I tend to prefer to keep the schema code in a file and delete comment lines before feeding to dbdelta():
$sql = file_get_contents(MYPLUGIN_PATH . "/schema.sql");
if (empty($sql))
die("Could not read schema.sql file");
// Remove comments and localize wp_ prefix
$sql = preg_replace('/^#.*\n/m', '', $sql);
$sql = preg_replace(
'/^(CREATE TABLE .*`)wp_/m',
'\1' . $wpdb->prefix,
$sql
);
// Auto-magically create/update mysql table structure
require_once(admin_path('includes/upgrade.php'));
dbDelta($sql);
The way to learn how to write a plugin is to read other plugin code, and to read good articles and tutorials. Definitely read Ozh's article on 10 mistakes plugin authors make, but the single most helpful thing to me was the book "Professional WordPress Plugin Development" by Williams, Richards and Tadlock. It's well written and thoroughly describes most of the things you need to know in a logical, clear and sensible way.
Any methods in your class that you'll use as callback function for hooks, should have public visibility - or the WP code won't have access to call them (e.g. database_setup & database_uninstall, admin_action). i.e.:
public function database_setup() ...
精彩评论