更新:有人回答并建议我写一个插件,但这是一个远远超过我的技能水平的解决方案。有更简单的方法吗?
我有一个小程序,它使用jQuery / Ajax通过php脚本Shoutbox.php与数据库进行交互。
这是一个非常简单的设置(非常适合新手理解)。
但是,我想将程序放在WordPress CMS中,这对我来说更复杂。谁能就这三个问题给我建议?
a)我会在wordpress文件中放入shoutbox.php(见下文)?我可以添加一个额外的文件,还是会进入另一个文件,比如custom_functions.php?
example.com/wp-content/themes/thesis_18/custom/custom_functions.php
b)如何从shoutbox.php结束的地方连接到数据库?
c)我如何引用javascript中的路径? javascript将在自定义函数文件中,但是如何获取shoutbox.php所在的路径?
jquery代码移动到插件文件夹中的php文件中。
<?php
/*
Plugin Name: Shoutbox plugin
Plugin URI: http://www.eslangel.com/aboutmyplugin
Description: Shoutbox plugin
Author: Me!
Version: 1.0
Author URI: http://www.eslangel.com
*/
function my_function (){ ?>
<script type="text/javascript">
$(document).ready(function(){
//global vars
var inputUser = $("#nick");
var inputMessage = $("#message");
var loading = $("#loading");
var messageList = $(".content > ul");
//functions
function updateShoutbox(){
//just for the fade effect
messageList.hide();
loading.fadeIn();
//send the post to shoutbox.php
$.ajax({
type: "POST", url: "<?php echo $pluginDirectory = dirname(plugin_basename(__FILE__));?>/shoutbox.php", data: "action=update",
complete: function(data){
loading.fadeOut();
messageList.html(data.responseText);
messageList.fadeIn(2000);
}
});
}
//check if all fields are filled
function checkForm(){
if(inputUser.attr("value") && inputMessage.attr("value"))
return true;
else
return false;
}
//Load for the first time the shoutbox data
updateShoutbox();
//on submit event
$("#form").submit(function(){
if(checkForm()){
var nick = inputUser.attr("value");
var message = inputMessage.attr("value");
//we deactivate submit button while sending
$("#send").attr({ disabled:true, value:"Sending..." });
$("#send").blur();
//send the post to shoutbox.php
$.ajax({
type: "POST", url: "<?php echo $pluginDirectory = dirname(plugin_basename(__FILE__));?>/shoutbox.php", data: "action=insert&nick=" + nick + "&message=" + message,
complete: function(data){
messageList.html(data.responseText);
updateShoutbox();
//reactivate the send button
$("#send").attr({ disabled:false, value:"Shout it!" });
}
});
}
else alert("Please fill all fields!");
//we prevent the refresh of the page after submitting the form
return false;
});
});
</script>
<?php
}
add_action('wp_head', 'my_function');
Shoutbox.php
<?php
define("HOST", "localhost:8889");
define("USER", "root");
define("PASSWORD", "root");
define("DB", "shoutbox");
/************************
FUNCTIONS
/************************/
function connect($db, $user, $password){
$link = @mysql_connect($db, $user, $password);
if (!$link)
die("Could not connect: ".mysql_error());
else{
$db = mysql_select_db(DB);
if(!$db)
die("Could not select database: ".mysql_error());
else return $link;
}
}
function getContent($link, $num){
$res = @mysql_query("SELECT date, user, message FROM shoutbox ORDER BY date DESC LIMIT ".$num, $link);
if(!$res)
die("Error: ".mysql_error());
else
return $res;
}
function insertMessage($user, $message){
$query = sprintf("INSERT INTO shoutbox(user, message) VALUES('%s', '%s');", mysql_real_escape_string(strip_tags($user)), mysql_real_escape_string(strip_tags($message)));
$res = @mysql_query($query);
if(!$res)
die("Error: ".mysql_error());
else
return $res;
}
/******************************
MANAGE REQUESTS
/******************************/
if(!$_POST['action']){
//We are redirecting people to our shoutbox page if they try to enter in our shoutbox.php
header ("Location: index.html");
}
else{
$link = connect(HOST, USER, PASSWORD);
switch($_POST['action']){
case "update":
$res = getContent($link, 20);
while($row = mysql_fetch_array($res)){
$result .= "<li><strong>".$row['user']."</strong><img src=\"css/images/bullet.gif\" alt=\"-\" />".$row['message']." <span class=\"date\">".$row['date']."</span></li>";
}
echo $result;
break;
case "insert":
echo insertMessage($_POST['nick'], $_POST['message']);
break;
}
mysql_close($link);
}
我从原始应用程序中的index.html页面获取了html,并将其放在侧栏中的文本小部件中。
<a id="logo" title="Go to eslangel!" href="http://www.eslangel.com"><img src="http://eslangel.com/wp-content/plugins/myplugin/css/images/logo.jpg" alt="eslangel.com" /></a>
<form method="post" id="form">
<table>
<tr>
<td><label>User</label></td>
<td><input class="text user" id="nick" type="text" MAXLENGTH="25" /></td>
</tr>
<tr>
<td><label>Message</label></td>
<td><input class="text" id="message" type="text" MAXLENGTH="255" /></td>
</tr>
<tr>
<td></td>
<td><input id="send" type="submit" value="Shout it!" /></td>
</tr>
</table>
</form>
<div id="container">
<ul class="menu">
<li>Shoutbox</li>
</ul>
<span class="clear"></span>
<div class="content">
<h1>Latest Messages</h1>
<div id="loading"><img src="http://eslangel.com/wp-content/plugins/myplugin/css/images/loading.gif" alt="Loading..." /></div>
<ul>
<ul>
</div>
</div>
答案 0 :(得分:2)
我会创建一个插件。由于一些原因,这非常有用
阅读有关Wordpress插件开发的更多信息! http://codex.wordpress.org/Writing_a_Plugin
创建插件实际上非常简单。只需转到位于wp-content目录中的plugins文件夹并创建一个新文件夹即可。在其中放置一个myplugin.php文件,顶部带有以下注释结构
/*
Plugin Name: Shoutbox plugin
Plugin URI: http://www.mywebsite.com/aboutmyplugin
Description: Shoutbox plugin
Author: Me!
Version: 1.0
Author URI: http://www.mywebsite.com
*/
将shoutbox.php复制到此文件夹中。不要在上面包含标题信息。
现在在主文件中你只需做两件事。
将myavascript添加到myplugin.php中,将javascript注入每个页面 -
add_action('wp_head', 'my_function');
function my_function() {
//your javascript from above. make sure it is php escaped
}
将ajax位指向shoutbox.php文件,以便它可以完成它的工作。
//change
url: "shoutbox.php"
//to
<?php $pluginDirectory = dirname(plugin_basename(__FILE__)); ?>
url: <?php $pluginDirectory + /shoutbox.php; ?>
确保从wordpress网站的插件页面激活插件。整个序列基本上做的是: