这个插件的代码实际上非常简单。init函数循环遍历所有侧栏(存储在wp\\u选项表中),并在wordpress中注册它们:
<?php
function init(){
//go through each sidebar and register it
$sidebars = sidebar_generator::get_sidebars();
if(is_array($sidebars)){
foreach($sidebars as $sidebar){
$sidebar_class = sidebar_generator::name_to_class($sidebar);
register_sidebar(array(
\'name\'=>$sidebar,
\'before_widget\' => \'<li id="%1$s" class="widget sbg_widget \'.$sidebar_class.\' %2$s">\',
\'after_widget\' => \'</li>\',
\'before_title\' => \'<h2 class="widgettitle sbg_title">\',
\'after_title\' => \'</h2>\',
));
}
}
}
?>
侧栏分别使用add\\u sidebar()和remove\\u sidebar()方法添加/删除。$\\u POST[\'sidebar\\u name\']作为边栏名称存储在$name变量中:
<?php
function add_sidebar(){
$sidebars = sidebar_generator::get_sidebars();
$name = str_replace(array("\\n","\\r","\\t"),\'\',$_POST[\'sidebar_name\']);
$id = sidebar_generator::name_to_class($name);
if(isset($sidebars[$id])){
die("alert(\'Sidebar already exists, please use a different name.\')");
}
$sidebars[$id] = $name;
sidebar_generator::update_sidebars($sidebars);
$js = "
var tbl = document.getElementById(\'sbg_table\');
var lastRow = tbl.rows.length;
// if there\'s no header row in the table, then iteration = lastRow + 1
var iteration = lastRow;
var row = tbl.insertRow(lastRow);
// left cell
var cellLeft = row.insertCell(0);
var textNode = document.createTextNode(\'$name\');
cellLeft.appendChild(textNode);
//middle cell
var cellLeft = row.insertCell(1);
var textNode = document.createTextNode(\'$id\');
cellLeft.appendChild(textNode);
//var cellLeft = row.insertCell(2);
//var textNode = document.createTextNode(\'[<a href=\\\'javascript:void(0);\\\' onclick=\\\'return remove_sidebar_link($name);\\\'>Remove</a>]\');
//cellLeft.appendChild(textNode)
var cellLeft = row.insertCell(2);
removeLink = document.createElement(\'a\');
linkText = document.createTextNode(\'remove\');
removeLink.setAttribute(\'onclick\', \'remove_sidebar_link(\\\'$name\\\')\');
removeLink.setAttribute(\'href\', \'javacript:void(0)\');
removeLink.appendChild(linkText);
cellLeft.appendChild(removeLink);
";
die( "$js");
}
function remove_sidebar(){
$sidebars = sidebar_generator::get_sidebars();
$name = str_replace(array("\\n","\\r","\\t"),\'\',$_POST[\'sidebar_name\']);
$id = sidebar_generator::name_to_class($name);
if(!isset($sidebars[$id])){
die("alert(\'Sidebar does not exist.\')");
}
$row_number = $_POST[\'row_number\'];
unset($sidebars[$id]);
sidebar_generator::update_sidebars($sidebars);
$js = "
var tbl = document.getElementById(\'sbg_table\');
tbl.deleteRow($row_number)
";
die($js);
}
?>
使用get\\u侧栏方法将侧栏加载并注册到主题中
<?php
function get_sidebar($name="0"){
if(!is_singular()){
if($name != "0"){
dynamic_sidebar($name);
}else{
dynamic_sidebar();
}
return;//dont do anything
}
global $wp_query;
$post = $wp_query->get_queried_object();
$selected_sidebar = get_post_meta($post->ID, \'sbg_selected_sidebar\', true);
$selected_sidebar_replacement = get_post_meta($post->ID, \'sbg_selected_sidebar_replacement\', true);
$did_sidebar = false;
//this page uses a generated sidebar
if($selected_sidebar != \'\' && $selected_sidebar != "0"){
echo "\\n\\n<!-- begin generated sidebar -->\\n";
if(is_array($selected_sidebar) && !empty($selected_sidebar)){
for($i=0;$i<sizeof($selected_sidebar);$i++){
if($name == "0" && $selected_sidebar[$i] == "0" && $selected_sidebar_replacement[$i] == "0"){
//echo "\\n\\n<!-- [called $name selected {$selected_sidebar[$i]} replacement {$selected_sidebar_replacement[$i]}] -->";
dynamic_sidebar();//default behavior
$did_sidebar = true;
break;
}elseif($name == "0" && $selected_sidebar[$i] == "0"){
//we are replacing the default sidebar with something
//echo "\\n\\n<!-- [called $name selected {$selected_sidebar[$i]} replacement {$selected_sidebar_replacement[$i]}] -->";
dynamic_sidebar($selected_sidebar_replacement[$i]);//default behavior
$did_sidebar = true;
break;
}elseif($selected_sidebar[$i] == $name){
//we are replacing this $name
//echo "\\n\\n<!-- [called $name selected {$selected_sidebar[$i]} replacement {$selected_sidebar_replacement[$i]}] -->";
$did_sidebar = true;
dynamic_sidebar($selected_sidebar_replacement[$i]);//default behavior
break;
}
//echo "<!-- called=$name selected={$selected_sidebar[$i]} replacement={$selected_sidebar_replacement[$i]} -->\\n";
}
}
if($did_sidebar == true){
echo "\\n<!-- end generated sidebar -->\\n\\n";
return;
}
//go through without finding any replacements, lets just send them what they asked for
if($name != "0"){
dynamic_sidebar($name);
}else{
dynamic_sidebar();
}
echo "\\n<!-- end generated sidebar -->\\n\\n";
return;
}else{
if($name != "0"){
dynamic_sidebar($name);
}else{
dynamic_sidebar();
}
}
}
?>
实际上,我们从wp\\u options表中加载边栏数组,其中包含以下内容:
<?php
function get_sidebars(){
$sidebars = get_option(\'sbg_sidebars\');
return $sidebars;
}
?>
一旦您了解了PHP,这段代码就非常简单了,所以我建议您花点时间熟悉一下它?这真是一项伟大的技能。希望这能帮助你找到正确的方向。