我基本上想创建一个表(最好是 AJAXified),让用户输入一行信息,并能够添加新的信息行,并删除选定的信息。
我找到了这篇文章和这篇文章。 设计方面看起来很简单,但我想了解功能。 我如何将字段内容添加到数据库中并重新调用它们? 如何将该页面作为一个整体“插入”到 WordPress 中? 我真的不知道从哪里开始。
我是从对 HTML 和 CSS 有信心的人的角度出发的,对 JS/jQuery 也相当有信心,但基本上只是破解他在周围找到的 PHP 代码。
任何帮助将不胜感激,甚至告诉我它现在超出了我的范围,并且可以继续使用 X 插件。 FWIW,我正在考虑使用自定义帖子类型或 Magic Fields 插件来执行此操作,但我想要一种更加用户友好的体验。
非常感谢!
不久前,我几乎和你在同一个地方开始,并创造了类似的东西。 这是我认为你需要知道的。
1) 弄清楚如何首先创建基本的 hello world。 一个简单的插件将由放在 plugins 目录中的 PHP 文件顶部的一些注释组成。 注意调用类的变量使其移动。 该类的构造函数调用 add_top_level_menu,当它被点击时(参见 $function 变量),display_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 . "https://wordpress.stackexchange.com/" . 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_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);
}
嘿,我建议使用 WPAlchemy MetaBoxes。 非常易于使用,它应该可以帮助您解决问题。
但是请记住,它还不是一个插件,因此您不会有一个“一键式设置”来开始。
http://www.farinspace.com/wpalchemy-metabox/
