以编程方式在WordPress中编辑PHP文件
问题描述:
I am trying to edit a few PHP codes lines in my WordPress theme files with a snippet of PHP code, though I am stuck and have been struggling to do this for a while with this code.
I have been trying to tweak this piece of code to do the job but unfortunately without luck.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
function update_GTour_theme_files() { $new_update = file_get_contents(__DIR__ . \"/../../themes/grandtour/header.php\"); $new_update = preg_replace(\'/$page_menu_transparent = 1/\',\'$page_menu_transparent = 0\',$new_update); $new_update = preg_replace(\'/$grandtour_page_menu_transparent = 1/\',\'$grandtour_page_menu_transparent = 0\',$new_update); file_put_contents (__DIR__ . \"/../../themes/grandtour/header.php\", $new_update); if ( file_exists (__DIR__ . \"/../../themes/grandtour/header.php\") && is_writable (__DIR__ . \"/../../themes/grandtour/header.php\") ){ update_GTour_theme_files(); echo \'</br><span style=\"color:green;font-weight:bold;\">Changes were applied successfully.</span>\'; } else { echo \'</br><span style=\"color:red;font-weight:bold;\">Error occured while applying the changes.</span>\'; } } |
I am expecting that this code should replace some text as illustrated in the file with the described path but it doesn\'t work.
网友观点:
Where do I start... let me count the ways.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
function update_GTour_theme_files() { $new_update = file_get_contents(__DIR__ . \\\"/../../themes/grandtour/header.php\\\"); $new_update = preg_replace(\'/$page_menu_transparent = 1/\',\'$page_menu_transparent = 0\',$new_update); $new_update = preg_replace(\'/$grandtour_page_menu_transparent = 1/\',\'$grandtour_page_menu_transparent = 0\',$new_update); file_put_contents (__DIR__ . \\\"/../../themes/grandtour/header.php\\\", $new_update); if ( file_exists (__DIR__ . \\\"/../../themes/grandtour/header.php\\\") && is_writable (__DIR__ . \\\"/../../themes/grandtour/header.php\\\") ){ update_GTour_theme_files(); echo \'</br><span style=\\\"color:green;font-weight:bold;\\\">Changes were applied successfully.</span>\'; } else { echo \'</br><span style=\\\"color:red;font-weight:bold;\\\">Error occured while applying the changes.</span>\'; } } |
Lets see when you run this it does:
1 2 |
file_put_contents (__DIR__ . \\\"/../../themes/grandtour/header.php\\\", $new_update); |
Then it checks:
1 2 3 4 5 6 |
if ( file_exists (__DIR__ . \\\"/../../themes/grandtour/header.php\\\") && is_writable (__DIR__ . \\\"/../../themes/grandtour/header.php\\\") ){ update_GTour_theme_files(); echo \'</br><span style=\\\"color:green;font-weight:bold;\\\">Changes were applied successfully.</span>\'; } |
Which is obviously true, or we would have gotten some errors already. So that is basically always true. Which means you call this update_GTour_theme_files();
itself again. Repeat the above steps a infinite number of times.
So that is obviously wrong. If you are calling this I would expect your browser to lockup.
So lets fix this up (single file):
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
function update_GTour_theme_files($file) { //fail early if (!file_exists ($file) || !is_writable ($file) ) die(\\\"File $file Does not exists or is not writable\\\"); $new_update = file_get_contents($file); $new_update = preg_replace(\'/\\$page_menu_transparent\\s*=\\s*1;/\',\'$page_menu_transparent = 0;\',$new_update); $new_update = preg_replace(\'/\\$grandtour_page_menu_transparent\\s*=\\s*1;/\',\'$grandtour_page_menu_transparent = 0;\',$new_update); if(file_put_contents ($file, $new_update)){ echo \'</br><span style=\\\"color:green;font-weight:bold;\\\">Changes were applied successfully.</span>\'; }else{ echo \'</br><span style=\\\"color:red;font-weight:bold;\\\">Error occured while applying the changes.</span>\'; } } update_GTour_theme_files(__DIR__ . \\\"/../../themes/grandtour/header.php\\\"); |
This will only update the one file, to do more then that you need to use scandir
, glob
or SPL DirectoryIterator / FilesystemIterator
.
PS your \\\"main\\\" or \\\"big\\\" problem (besides the recursion) is right here:
1 2 3 |
$new_update = preg_replace(\'/$page_menu_transparent = 1/\',\'$page_menu_transparent = 0\',$new_update); $new_update = preg_replace(\'/$grandtour_page_menu_transparent = 1/\',\'$grandtour_page_menu_transparent = 0\',$new_update); |
These $
in /$page_menu_transparent
are not escaped so the are treated as REGEX. Which means they match the end of the string, which makes no sense. I also added some vaiable space \\s*=\\s*
and the ;
semi-colon otherwise $page_menu_transparent = 1345;
will become $page_menu_transparent = 0;
. This may have some impact if it\'s in ()
or an array etc. (anything without the ;
)
For all files in a given folder and it\'s subfolders
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
function update_GTour_theme_files($dir) { if (!file_dir($dir) || is_writable ($dir) ) die(\\\"Dir $dir Does not exists or is not writable\\\"); $Iterator = new RecursiveIteratorIterator( new RecursiveDirectoryIterator( $dir, RecursiveDirectoryIterator::SKIP_DOTS|RecursiveDirectoryIterator::UNIX_PATHS ) ); foreach($Iterator as $fileInfo){ if($fileInfo->isDir() || $fileInfo->getExtension() != \'php\') continue; $file = $fileInfo->getPathname(); $new_update = file_get_contents($file); $new_update = preg_replace(\'/\\$page_menu_transparent\\s*=\\s*1;/\',\'$page_menu_transparent = 0;\',$new_update); $new_update = preg_replace(\'/\\$grandtour_page_menu_transparent\\s*=\\s*1;/\',\'$grandtour_page_menu_transparent = 0;\',$new_update); if(file_put_contents ($file, $new_update)){ echo \'</br><span style=\\\"color:green;font-weight:bold;\\\">Changes were applied successfully.</span>\'; }else{ echo \'</br><span style=\\\"color:red;font-weight:bold;\\\">Error occured while applying the changes.</span>\'; } } } update_GTour_theme_files(__DIR__ . \\\"/../../themes/grandtour/\\\"); |
This uses RecursiveDirectoryIterator
so it should look through all sub folders.
But this is all untested so be very careful. If you mess your files up don\'t come blaming me, you were warned.
That said Enjoy~
1. 带 [亲测] 说明源码已经被站长亲测过!
2. 下载后的源码请在24小时内删除,仅供学习用途!
3. 分享目的仅供大家学习和交流,请不要用于商业用途!
4. 本站资源售价只是赞助,收取费用仅维持本站的日常运营所需!
5. 本站所有资源来源于站长上传和网络,如有侵权请邮件联系站长!
6. 没带 [亲测] 代表站长时间紧促,站长会保持每天更新 [亲测] 源码 !
7. 盗版ripro用户购买ripro美化无担保,若设置不成功/不生效我们不支持退款!
8. 本站提供的源码、模板、插件等等其他资源,都不包含技术服务请大家谅解!
9. 如果你也有好源码或者教程,可以到审核区发布,分享有金币奖励和额外收入!
10.如果您购买了某个产品,而我们还没来得及更新,请联系站长或留言催更,谢谢理解 !
GG资源网 » 以编程方式在WordPress中编辑PHP文件