WordPress中PHP函数导致网站崩溃问题求助
问题解决步骤
1. 立即还原WordPress核心文件
你之前错误修改了class-wp-hook.php,这是WordPress核心文件,绝对不能随意改动。立刻找到该文件,删除你添加的所有代码,恢复到原始状态,否则会引发更多致命错误。
2. 排查functions.php语法问题
致命错误提示找不到enqueue_custom_calculator_assets函数,大概率是你粘贴到functions.php的代码存在语法错误,导致PHP无法正确解析函数定义。常见问题包括HTML部分的转义引号(")引发解析错误,或是PHP标签闭合/开启异常。
3. 修正后的完整代码
将以下代码替换你之前在functions.php中添加的内容:
function enqueue_custom_calculator_assets() { // 加载计算器样式 wp_enqueue_style('custom-calculator-styles', get_template_directory_uri() . '/assets/css/Custom/troopRss.css'); // 加载计算器脚本,将jQuery作为依赖自动加载 wp_enqueue_script('custom-calculator-script', get_template_directory_uri() . '/assets/js/Custom/troopRss.js', array('jquery'), null, true); } add_action('wp_enqueue_scripts', 'enqueue_custom_calculator_assets'); function custom_calculator_shortcode() { ob_start(); ?> <div class="calculator"> <div class="inputs"> <div class="total-container"> <label class="output-label" for="Food">Food</label> <label class="output-label" for="Wood">Wood</label> <label class="output-label" for="Stone">Stone</label> <input type="text" id="Food" class="output-field-total" readonly placeholder="0"> <input type="text" id="Wood" class="output-field-total" readonly placeholder="0"> <input type="text" id="Stone" class="output-field-total" readonly placeholder="0"> <label class="output-label" for="Ore">Ore</label> <label class="output-label" for="Gold">Gold</label> <label class="output-label" for="Power">Power</label> <input type="text" id="Ore" class="output-field-total" readonly placeholder="0"> <input type="text" id="Gold" class="output-field-total" readonly placeholder="0"> <input type="text" id="Power" class="output-field-total" readonly placeholder="0"> </div> <br><br> <button type="button" id="newTroopButton">New Troop</button> <div id="troopList"></div> </div> </div> <?php return ob_get_clean(); } add_shortcode('custom_calculator', 'custom_calculator_shortcode');
4. 额外检查项
- 确认
troopRss.css和troopRss.js文件确实存在于主题目录的assets/css/Custom/和assets/js/Custom/路径下,路径错误会导致资源加载失败 - 如果使用的是子主题,请将
get_template_directory_uri()替换为get_stylesheet_directory_uri(),确保加载的是子主题的资源文件
内容的提问来源于stack exchange,提问作者ChazzAyG
相关产品推荐
相关产品推荐

