我正在從事一個項目,其中我有MediaWiki PHP Scripts,它將DB的出版物信息導入出版物頁面。如何將PHP腳本從MediaWiki更改爲Wordpress插件?
我需要將這些腳本轉換爲Wordpress插件,但我不知道最好的方法來做到這一點。現在相當丟失,我嘗試做Tutorial: Writing a simple WordPress plugin from scratch,但我沒有成功,我仍然沒有這個工作。
原始鏈接到MediaWiki代碼
在這裏你會看到我原來鏈接到MediaWiki代碼:
<?php
# the function registered by the extension gets the text between the
# tags as input and can transform it into arbitrary HTML code.
# Note: The output is not interpreted as WikiText but directly
# included in the HTML output. So Wiki markup is not supported.
# To activate the extension, include it from your LocalSettings.php
# with: include("extensions/YourExtensionName.php");
$wgExtensionFunctions[] = "wfExampleExtension";
//---------------------------------------------------------------------------
//---------------------------------------------------------------------------
//
// Register the extension with the WikiText parser.
// The first parameter is the name of the new tag. In this case it defines
// the tag:
// <server-id> ... </server-id>
// The second parameter is the callback function for processing the text
// between the tags.
//
function wfExampleExtension() {
global $wgParser; // MediaWiki global variable
$wgParser->setHook("server-id", "renderSERVERID");
}
//---------------------------------------------------------------------------
//---------------------------------------------------------------------------
//
// The callback function for converting the input text to HTML output.
// The function registered by the extension gets the text between the
// tags as $input and transforms it into arbitrary HTML code.
// Note: the output is not interpreted as WikiText but directly included in
// the HTML output. So Wiki markup is not supported.
//
// To activate the extension, include it from your LocalSettings.php
// with: include("extensions/YourExtensionName.php");
//
// $argv is an array containing any arguments passed to the extension like:
// <server-id what="foo" bar>..
//
// According to the metawiki, this works in MediaWiki 1.5.5.
// <server-id what="person" id="62">This text is not actually used</server-id>
//
// Personal information:
// <server-id what='person' id='62'></server-id>
//
// Information for a group:
// <server-id what='publications' cc='IP02'></server-id>
//
function renderSERVERID($input, $argv) {
// connect to the database
$idDBLink = odbc_connect('SERVER ID', 'some_db', 'some_db_pw');
if (!$idDBLink) { exit("Connection to database failed! Please contact [email protected]"); }
$html = "";
if ($argv['what'] == 'person') {
$id = split(",", trim($argv["id"]));
if ($id != '') {
// information about someone:
// 1. personal contacts and summary
// 2. publications by person
// 3. advisory work by person
//
$html .= personById($idDBLink, $id[0]);
$internalIds = authorIdByNumber($idDBLink, $id); // use all Ids
$html .= pubsById($idDBLink, $internalIds);
$html .= advisingById($idDBLink, $internalIds);
}
}
else if ($argv['what'] == 'advising') {
$id = split(",", trim($argv["id"]));
if ($id != '') {
$internalIds = authorIdByNumber($idDBLink, $id); // use all Ids
$html .= iconv('latin1', 'UTF-8', advisingById($idDBLink, $internalIds));
}
}
else if ($argv['what'] == 'publications') {
// information about some "centro de custo":
// 1. currently, only a list of publications
//
$cc = trim($argv["cc"]);
$id = trim($argv["id"]);
if ($cc != '') {
$html .= iconv('latin1', 'UTF-8', pubsByCC($idDBLink, $cc));
}
else if ($id != '') {
$html .= iconv('latin1', 'UTF-8', pubsById($idDBLink, authorIdByNumber($idDBLink, array($id))));
}
}
/*else if ($argv['what'] == 'publications') {
// information about some "centro de custo":
// 1. currently, only a list of publications
//
$cc = trim($argv["cc"]);
if ($cc != '') {
$html .= pubsByCC($idDBLink, $cc);
}
}*/
else if ($argv['what'] == 'calls') {
// information about some "centro de custo":
// 1. currently, only a list of publications
//
$cc = trim($argv["cc"]);
$showClosed = isset($argv['showclosed']) ? trim($argv['showclosed']) : "";
if ($cc != '') {
$html .= iconv('latin1', 'UTF-8', callsByCC($idDBLink, $cc, $showClosed == "yes"));
}
}
else {
// oops! no text...
}
odbc_close($idDBLink);
return $html;
}
?>
我的WordPress嘗試版本
在這裏,你會看到我嘗試用WordPress的代碼做:
<?php
// ==================================================
// WordPress Plugin
// ==================================================
/*
Plugin Name: Publications Importer
Plugin URI: http://someperson.me/downloads/publications-importer
Description: Integrates the Publications Importer plugin into your WordPress install.
Version: 0.0.1
Author: Someone
Author URI: http://someperson.me/
*/
require_once 'server-id-config.php';
require_once 'server-id-util.php';
require_once 'server-id-people.php';
require_once 'server-id-pubs.php';
require_once 'server-id-advising.php';
defined('ABSPATH') or die('Plugin file cannot be accessed directly.');
if (! class_exists('Publication')) {
class Publication
{
/**
* Tag identifier used by file includes and selector attributes.
* @var string
*/
protected $tag = 'publications-importer';
/**
* User friendly name used to identify the plugin.
* @var string
*/
protected $name = 'Publications Importer';
/**
* Current version of the plugin.
* @var string
*/
protected $version = '0.0.1';
public function __construct()
{
add_shortcode($this->tag, array(&$this, 'shortcode'));
}
public function shortcode($atts, $content = null)
{
extract(shortcode_atts(array(
'what' => false,
'cc' => false
), $atts));
$styles = array();
if (is_numeric($what)) {
$styles[] = esc_attr('what: ' . $what);
}
$classes = array(
$this->tag
);
if (!empty($cc)) {
$classes[] = esc_attr($cc);
}
ob_start();
?><pre cc="<?php esc_attr_e(implode(' ', $classes)); ?>"<?php
echo (count($styles) > 0 ? ' style="' . implode(' ', $styles) . '"' : '');
?>><p><?php echo $content; ?></p></pre><?php
return ob_get_clean();
}
}
new Publication;
}
// ==================================================
// END WordPress Plugin
// ==================================================
# the function registered by the extension gets the text between the
# tags as input and can transform it into arbitrary HTML code.
# Note: The output is not interpreted as WikiText but directly
# included in the HTML output. So Wiki markup is not supported.
# To activate the extension, include it from your LocalSettings.php
# with: include("extensions/YourExtensionName.php");
$wgExtensionFunctions[] = "wfExampleExtension";
//---------------------------------------------------------------------------
//---------------------------------------------------------------------------
//
// Register the extension with the WikiText parser.
// The first parameter is the name of the new tag. In this case it defines
// the tag:
// <server-id> ... </server-id>
// The second parameter is the callback function for processing the text
// between the tags.
//
function wfExampleExtension() {
global $wgParser; // MediaWiki global variable
$wgParser->setHook("server-id", "renderSERVERID");
}
//---------------------------------------------------------------------------
//---------------------------------------------------------------------------
//
// The callback function for converting the input text to HTML output.
// The function registered by the extension gets the text between the
// tags as $input and transforms it into arbitrary HTML code.
// Note: the output is not interpreted as WikiText but directly included in
// the HTML output. So Wiki markup is not supported.
//
// To activate the extension, include it from your LocalSettings.php
// with: include("extensions/YourExtensionName.php");
//
// $argv is an array containing any arguments passed to the extension like:
// <server-id what="foo" bar>..
//
// According to the metawiki, this works in MediaWiki 1.5.5.
// <server-id what="person" id="62">This text is not actually used</server-id>
//
// Personal information:
// <server-id what='person' id='62'></server-id>
//
// Information for a group:
// <server-id what='publications' cc='IP02'></server-id>
//
function renderSERVERID($input, $argv) {
// connect to the database
$idDBLink = odbc_connect('SERVER ID', 'some_db', 'some_db_pw');
if (!$idDBLink) { exit("Connection to database failed! Please contact [email protected]"); }
$html = "";
if ($argv['what'] == 'person') {
$id = split(",", trim($argv["id"]));
if ($id != '') {
// information about someone:
// 1. personal contacts and summary
// 2. publications by person
// 3. advisory work by person
//
$html .= personById($idDBLink, $id[0]);
$internalIds = authorIdByNumber($idDBLink, $id); // use all Ids
$html .= pubsById($idDBLink, $internalIds);
$html .= advisingById($idDBLink, $internalIds);
}
}
else if ($argv['what'] == 'advising') {
$id = split(",", trim($argv["id"]));
if ($id != '') {
$internalIds = authorIdByNumber($idDBLink, $id); // use all Ids
$html .= iconv('latin1', 'UTF-8', advisingById($idDBLink, $internalIds));
}
}
else if ($argv['what'] == 'publications') {
// information about some "centro de custo":
// 1. currently, only a list of publications
//
$cc = trim($argv["cc"]);
$id = trim($argv["id"]);
if ($cc != '') {
$html .= iconv('latin1', 'UTF-8', pubsByCC($idDBLink, $cc));
}
else if ($id != '') {
$html .= iconv('latin1', 'UTF-8', pubsById($idDBLink, authorIdByNumber($idDBLink, array($id))));
}
}
/*else if ($argv['what'] == 'publications') {
// information about some "centro de custo":
// 1. currently, only a list of publications
//
$cc = trim($argv["cc"]);
if ($cc != '') {
$html .= pubsByCC($idDBLink, $cc);
}
}*/
else if ($argv['what'] == 'calls') {
// information about some "centro de custo":
// 1. currently, only a list of publications
//
$cc = trim($argv["cc"]);
$showClosed = isset($argv['showclosed']) ? trim($argv['showclosed']) : "";
if ($cc != '') {
$html .= iconv('latin1', 'UTF-8', callsByCC($idDBLink, $cc, $showClosed == "yes"));
}
}
else {
// oops! no text...
}
odbc_close($idDBLink);
return $html;
}
?>
所以我究竟需要知道的是:
1)不應該WordPress的能夠解釋(例如鏈接到MediaWiki標籤:<server-id what='publications' cc='IP02'></server-id>
),並自動做到這一點?
2)哪裏可以找到關於這種遷移的更多文檔?
3)我做錯了嗎?
謝謝你的回答。我真正需要的是從數據庫中提取信息,並使用科學出版物訪問並將其打印到WordPress頁面中。我的問題是,我已經完成了PHP腳本和MediaWiki一樣簡單,我希望它們也可以使用WordPress,但我不知道如何。 –
現有PHP腳本的有用部分可能是對數據庫的查詢。其餘的對我來說恐怕沒什麼用處。但是,這是可以的,因爲WordPress提供了一個插件框架,可以爲你做很多工作。首先開始處理一個簡單的插件,然後你可以使用[wp db](https://codex.wordpress.org/Class_Reference/wpdb)對象來執行你的查詢並獲得你需要的數據。 – jezza101
你有幾個選擇如何在你的文章中顯示它,一個[簡碼](https://codex.wordpress.org/Shortcode_API)可能是一個很好的選擇,否則對[內容](https: //codex.wordpress.org/Plugin_API/Filter_Reference/the_content)可以用來自動將內容注入到您的文章中,也許您也會添加一個自定義元屬性來控制您要顯示的內容。我認爲在這一點上,你將不得不離開並閱讀,WP文檔非常棒! – jezza101