douzao1119 2019-03-22 23:18
浏览 129
已采纳

以编程方式在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.

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.

  • 写回答

1条回答 默认 最新

  • double0201 2019-03-22 23:55
    关注

    Where do I start... let me count the ways.

    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:

     file_put_contents (__DIR__ . "/../../themes/grandtour/header.php", $new_update);
    

    Then it checks:

     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):

    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:

    $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

    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~

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 wifi 图标不见了 不知道怎么办 上不了网 变成小地球了
  • ¥50 STM32单片机传感器读取错误
  • ¥50 power BI 从Mysql服务器导入数据,但连接进去后显示表无数据
  • ¥15 (关键词-阻抗匹配,HFSS,RFID标签)
  • ¥50 sft下载大文阻塞卡死
  • ¥15 机器人轨迹规划相关问题
  • ¥15 word样式右侧翻页键消失
  • ¥15 springboot+vue 集成keycloak sso到阿里云
  • ¥15 win7系统进入桌面过一秒后突然黑屏
  • ¥30 backtrader对于期货交易的现金和资产计算的问题