問題描述

我基本上想建立一個表 (最好是 AJAXified),讓使用者輸入一行資訊,並能夠新增新的資訊行,並刪除所選的資訊。

我找到了 this postthis post 。設計方面看起來很簡單,但我想知道功能。如何將欄位內容新增到資料庫並召集他們備份?如何將該頁面”plugged in” 整合到 Wordpress?我真的不知道從哪裡開始。

我從一個對 HTML& CSS,而且與 JS /jQuery 相當,但是基本上只是找出了他發現的 PHP 程式碼。

任何幫助將不勝感激,即使告訴我現在超出了我,還要玩 X 外掛。 FWIW,我正在考慮使用自定義帖子型別或 Magic Fields 外掛,但是我想要一個更多的 user-friendly 體驗。

非常感謝!

最佳解決方案

我幾乎開始在同一個地方,而且已經創造了類似的東西。這是我認為你需要知道的。

1) 首先闡述如何建立你的基本問候世界。一個簡單的外掛將包含在插入到您的 plugins 目錄中的 PHP 檔案頂部的幾個註釋。注意呼叫類的變數使其移動。該類的建構函式呼叫 add_top_level_menu,當單擊此處 (參見 $ function variable) 時, 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 . '/' . 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);
    }

參考文獻

注:本文內容整合自 Google/Baidu/Bing 輔助翻譯的英文資料結果。如果您對結果不滿意,可以加入我們改善翻譯效果:薇曉朵技術論壇。