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 请教:如何用postman调用本地虚拟机区块链接上的合约?
  • ¥15 为什么使用javacv转封装rtsp为rtmp时出现如下问题:[h264 @ 000000004faf7500]no frame?
  • ¥15 乘性高斯噪声在深度学习网络中的应用
  • ¥15 运筹学排序问题中的在线排序
  • ¥15 关于docker部署flink集成hadoop的yarn,请教个问题 flink启动yarn-session.sh连不上hadoop,这个整了好几天一直不行,求帮忙看一下怎么解决
  • ¥15 深度学习根据CNN网络模型,搭建BP模型并训练MNIST数据集
  • ¥15 C++ 头文件/宏冲突问题解决
  • ¥15 用comsol模拟大气湍流通过底部加热(温度不同)的腔体
  • ¥50 安卓adb backup备份子用户应用数据失败
  • ¥20 有人能用聚类分析帮我分析一下文本内容嘛