我计划在商业上开发wordpress主题。。我只是想知道在我正在开发的wordpress主题中添加插件的简单方法。。谁能给我建议一下简单的步骤吗。。我需要有效使用的可能方法。。
如何在WordPress主题中添加插件?
2 个回复
SO网友:Ján Bočínec
您可以在主题函数中始终包含插件的文件。php文件。当然,您应该将其放入一些合理的结构中,以避免主题因文件和代码而膨胀:)。
- https://stackoverflow.com/questions/6974006/wordpress-package-plugin-with-theme
- How to bundle a plugin with a theme, or vice versa
/**
* Load theme plugins
*
**/
function cfct_load_plugins() {
$files = cfct_files(CFCT_PATH.\'plugins\');
if (count($files)) {
foreach ($files as $file) {
if (file_exists(CFCT_PATH.\'plugins/\'.$file)) {
include_once(CFCT_PATH.\'plugins/\'.$file);
}
// child theme support
if (file_exists(STYLESHEETPATH.\'/plugins/\'.$file)) {
include_once(STYLESHEETPATH.\'/plugins/\'.$file);
}
}
}
}
/**
* Get a list of php files within a given path as well as files in corresponding child themes
*
* @param sting $path Path to the directory to search
* @return array Files within the path directory
*
**/
function cfct_files($path) {
$files = apply_filters(\'cfct_files_\'.$path, false);
if ($files) {
return $files;
}
$files = wp_cache_get(\'cfct_files_\'.$path, \'cfct\');
if ($files) {
return $files;
}
$files = array();
$paths = array($path);
if (STYLESHEETPATH.\'/\' != CFCT_PATH) {
// load child theme files
$paths[] = STYLESHEETPATH.\'/\'.str_replace(CFCT_PATH, \'\', $path);
}
foreach ($paths as $path) {
if (is_dir($path) && $handle = opendir($path)) {
while (false !== ($file = readdir($handle))) {
$path = trailingslashit($path);
if (is_file($path.$file) && strtolower(substr($file, -4, 4)) == ".php") {
$files[] = $file;
}
}
closedir($handle);
}
}
$files = array_unique($files);
wp_cache_set(\'cfct_files_\'.$path, $files, \'cfct\', 3600);
return $files;
}
。。。然后使用函数cfct_load_plugins();
在主题初始化期间。SO网友:perfwill
最简单的解决方案就是使用普通的wordpress插件^。为你自己的主题编写一个专门的插件系统是完全没有必要的,这会使你的主题更加臃肿,并增加开发和维护成本。在这种情况下KISS 原则获胜<问候,
海
用自定义方法替换插件时要考虑的另一点是更新。许多插件更新是为了解决安全问题,或者解决WordPress更新引起的问题。
结束