最合适的回答,由SO网友:Squiggs. 整理而成
不久前,我和你差不多是从同一个地方开始的,并且创造了一些类似的东西。这是我认为你需要知道的。
1) 首先,找出如何创建基本的hello world。一个简单的插件将由放置在插件目录中的PHP文件顶部的一些注释组成。注意调用类的变量使其移动。该类的构造函数调用add\\u top\\u level\\u菜单,当单击该菜单时(请参见$函数变量),display\\u page()函数将启动,开始构建页面。
<?php
/*
Plugin Name: Your plugin name
Description: Description
Version: 1.0
Author: Your Name
Author URI: http://yourweb.com
*/
$myplugvariable = new yourpluginname();
class yourpluginname
{
function __construct(){
add_action( \'admin_menu\', array( &$this, \'add_top_level_menu\' ) );
}
function add_admin_scripts(){
//adds javavascript files for this plugin.
wp_enqueue_script(\'my-script-name\', WP_PLUGIN_URL . \'/\' . dirname(plugin_basename(__FILE__)) . \'/js/javascript.js\', array(\'jquery\'), \'1.0\');
wp_localize_script(\'my-script-name\', \'MyScriptAjax\', array(\'ajaxUrl\' => admin_url(\'admin-ajax.php\')));
}
function add_top_level_menu()
{
// Settings for the function call below
$page_title = \'Plugin Name\';
$menu_title = \'Plugin Name\';
$menu_slug = \'plugin-name\';
$function = array( &$this, \'display_page\' );
$icon_url = NULL;
$position = \'\';
// Creates a top level admin menu - this kicks off the \'display_page()\' function to build the page
$page = add_menu_page($page_title, $menu_title, $this->capability, $menu_slug, $function, $icon_url, 10);
// Adds an additional sub menu page to the above menu - if we add this, we end up with 2 sub menu pages (the main pages is then in sub menu. But if we omit this, we have no sub menu
// This has been left in incase we want to add an additional page here soon
//add_submenu_page( $menu_slug, $page_title, $page_title, $capability, $menu_slug . \'_sub_menu_page\', $function );
}
function display_page()
{
if (!current_user_can($this->capability ))
wp_die(__(\'You do not have sufficient permissions to access this page.\'));
//here comes the HTML to build the page in the admin.
echo(\'HELLO WORLD\');
}
}
?>
2)一旦创建了返回数据的内部函数,不管是什么。(使用全局wordpress数据函数,例如$wpdb->get\\u results($sql)。
3) 管理员内部的AJAX与您通常使用它的方式略有不同。所有wordpress AJAX调用都挂接到admin AJAX中。php。我发现了这个:http://www.garyc40.com/2010/03/5-tips-for-using-ajax-in-wordpress/#js-global 比较善于解释事情。
4) 如果您正在创建表:下面的内容将为您完成这项工作。在codex中搜索dbDelta。
function plugin_install()
{
global $wpdb;
$table_name_prefix = "plugin-name";
$table_name = $wpdb->prefix . "plugin_name";
$sql = "CREATE TABLE " . $table_name . " (
id mediumint(9) NOT NULL AUTO_INCREMENT,
post_id mediumint(9) NOT NULL,
score mediumint(9) NOT NULL
);";
require_once(ABSPATH . \'wp-admin/includes/upgrade.php\');
dbDelta($sql);
}