我建議不要以編程方式檢查一些插件的存在,下載,安裝,並從任何主題文件中激活它們。您必須考慮到每次加載給定頁面時都會運行檢查,並可能導致大量多餘的代碼和不必要的活動。
相反,我的建議是將你的主題所依賴的任何插件作爲主題本身的一部分進行打包,而不是作爲插件。插件應該由用戶自行決定安裝。如果一個主題取決於一個插件的正確或有效運作,那麼它確實應該與主題一起打包和下載。
但直接回答你的問題:
- 可能。這當然是可以做到的。
- 是的。
- 看上面。您可能會遇到更多問題,通過不斷檢查插件並根據這些條件運行一系列操作,而不是僅僅包含所需的所有內容。
Plenty of research
我不能強調不夠,但是,一個插件的目的是給用戶的選項擴展給定主題的功能。如果你的主題的功能取決於現有的插件,那麼當有人下載你的主題時,你真的應該包括所有的文件。
雖然如果您覺得您的方法以我可能會丟失的方式來使您的主題受益,請隨時按照您的喜好寫下它。
完整答案:我決定幫助爲您創建一個概念驗證,因爲我感到無聊和好奇。這很大程度上應該是自我解釋。添加這些功能:
function mm_get_plugins($plugins)
{
$args = array(
'path' => ABSPATH.'wp-content/plugins/',
'preserve_zip' => false
);
foreach($plugins as $plugin)
{
mm_plugin_download($plugin['path'], $args['path'].$plugin['name'].'.zip');
mm_plugin_unpack($args, $args['path'].$plugin['name'].'.zip');
mm_plugin_activate($plugin['install']);
}
}
function mm_plugin_download($url, $path)
{
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$data = curl_exec($ch);
curl_close($ch);
if(file_put_contents($path, $data))
return true;
else
return false;
}
function mm_plugin_unpack($args, $target)
{
if($zip = zip_open($target))
{
while($entry = zip_read($zip))
{
$is_file = substr(zip_entry_name($entry), -1) == '/' ? false : true;
$file_path = $args['path'].zip_entry_name($entry);
if($is_file)
{
if(zip_entry_open($zip,$entry,"r"))
{
$fstream = zip_entry_read($entry, zip_entry_filesize($entry));
file_put_contents($file_path, $fstream);
chmod($file_path, 0777);
//echo "save: ".$file_path."<br />";
}
zip_entry_close($entry);
}
else
{
if(zip_entry_name($entry))
{
mkdir($file_path);
chmod($file_path, 0777);
//echo "create: ".$file_path."<br />";
}
}
}
zip_close($zip);
}
if($args['preserve_zip'] === false)
{
unlink($target);
}
}
function mm_plugin_activate($installer)
{
$current = get_option('active_plugins');
$plugin = plugin_basename(trim($installer));
if(!in_array($plugin, $current))
{
$current[] = $plugin;
sort($current);
do_action('activate_plugin', trim($plugin));
update_option('active_plugins', $current);
do_action('activate_'.trim($plugin));
do_action('activated_plugin', trim($plugin));
return true;
}
else
return false;
}
...,然後執行,像這樣:
$plugins = array(
array('name' => 'jetpack', 'path' => 'http://downloads.wordpress.org/plugin/jetpack.1.3.zip', 'install' => 'jetpack/jetpack.php'),
array('name' => 'buddypress', 'path' => 'http://downloads.wordpress.org/plugin/buddypress.1.5.5.zip', 'install' => 'buddypress/bp-loader.php'),
array('name' => 'tumblr-importer', 'path' => 'http://downloads.wordpress.org/plugin/tumblr-importer.0.5.zip', 'install' => 'tumblr-importer/tumblr-importer.php')
);
mm_get_plugins($plugins);
「名」可以是任何東西,因爲它提供更加臨時值的。 'path'正是它的外觀,並且是Wordpress服務器上zip文件的直接URL。 'install'值只是包含所有插件信息的主PHP腳本的路徑。您必須知道該特定插件目錄的佈局才能填寫此信息,因爲這也是激活黑客工作所必需的。
激活功能發現這裏(信貸sorich87):https://wordpress.stackexchange.com/questions/4041/how-to-activate-plugins-via-code
警告:這絕不是一個非常安全的方式來做事。我實際上認爲這很容易被濫用,所以我們最好的選擇是以此作爲我們的基準,並從那裏嘗試和改進。
如果你應該決定使用這種方法,我所要求的是我的初始整體腳本以及他的激活過程中的sorich87。
你問的大部分內容都是Wordpress能夠做的事情,這意味着你可以看看他們是如何做到的。例如,wordpress可以讓你安裝插件,激活插件等。看看他們是如何做到的。 – 2012-04-27 15:56:34