WordPress 插件导致整个站点崩溃并出现 500 Internal Server Error

WordPress plugin crashes entire site with 500 Internal Server Error

我正在尝试制作 BuddyPress 群组扩展插件。我使用了以下改编自 Group Extension API 页面的代码(直接复制初始注释块之后的所有内容)-

<?php
/*
Plugin Name: Group Emails
Description: Add group email functionality to BuddyPress
Version: 0.1.0
Author: SDGApps
Author URI: https://sdgapps.com
*/

/**
 * The bp_is_active( 'groups' ) check is recommended, to prevent problems 
 * during upgrade or when the Groups component is disabled
 */
if ( bp_is_active( 'groups' ) ) :

class Group_Extension_Example_2 extends BP_Group_Extension {
    /**
     * Here you can see more customization of the config options
     */
    function __construct() {
        $args = array(
            'slug' => 'group-extension-example-2',
            'name' => 'Group Extension Example 2',
            'nav_item_position' => 105,
            'screens' => array(
                'edit' => array(
                    'name' => 'GE Example 2',
                    // Changes the text of the Submit button
                    // on the Edit page
                    'submit_text' => 'Submit, suckaz',
                ),
                'create' => array(
                    'position' => 100,
                ),
            ),
        );
        parent::init( $args );
    }

    function display( $group_id = NULL ) {
        $group_id = bp_get_group_id();
        echo 'This plugin is 2x cooler!';
    }

    function settings_screen( $group_id = NULL ) {
        $setting = groups_get_groupmeta( $group_id, 'group_extension_example_2_setting' );

        ?>
        Save your plugin setting here: <input type="text" name="group_extension_example_2_setting" value="<?php echo esc_attr( $setting ) ?>" />
        <?php
    }

    function settings_screen_save( $group_id = NULL ) {
        $setting = isset( $_POST['group_extension_example_2_setting'] ) ? $_POST['group_extension_example_2_setting'] : '';
        groups_update_groupmeta( $group_id, 'group_extension_example_2_setting', $setting );
    }

    /**
     * create_screen() is an optional method that, when present, will
     * be used instead of settings_screen() in the context of group
     * creation.
     *
     * Similar overrides exist via the following methods:
     *   * create_screen_save()
     *   * edit_screen()
     *   * edit_screen_save()
     *   * admin_screen()
     *   * admin_screen_save()
     */
    function create_screen( $group_id = NULL ) {
        $setting = groups_get_groupmeta( $group_id, 'group_extension_example_2_setting' );

        ?>
        Welcome to your new group! You are neat.
        Save your plugin setting here: <input type="text" name="group_extension_example_2_setting" value="<?php echo esc_attr( $setting ) ?>" />
        <?php
    }

}
bp_register_group_extension( 'Group_Extension_Example_2' );

endif;

我是 WordPress 和 PHP 的新手(是的,谁不知道 PHP :-) 所以你介意告诉我为什么加载这个插件会导致 500 内部服务器错误将整个网站关闭?

您的代码抛出 502 错误,因为您在定义函数之前使用了“bp_is_active”函数。您需要先允许 BuddyPress 函数在您的脚本之前加载。

有一种方法可以确保BP功能已经加载,请使用bp_include操作。

看到这个link:https://codex.buddypress.org/plugindev/checking-buddypress-is-active/

开发新插件时不要忘记启用 WP_DEBUG 或至少在 php.ini 中打开错误报告。

谢谢:)