0

I'm creating a plugin for a small business. I need the plugin to do a few things:

  • Deactivate + delete pre-installed plugins
  • Install Kadence (including kadence theme, child theme and kadence blocks)
  • Removing default posts e.g. 'Hello World!'
  • Remove old themes e.g. Twenty Twenty Four
  • Add plugins such as SVG support

For the most part the plugin works, however when I try to install and activate a plugin I get an error of ob_end_clean(): Failed to delete buffer. No buffer to delete in /var/www/htdocs/wp-admin/includes/plugin.php on line 740 and Cannot modify header information - headers already sent by (output started at /var/www/htdocs/wp-includes/functions.php:5420) in /var/www/htdocs/wp-includes/pluggable.php on line 1435.

All the functions are ran in the plugin's activator file as I need everything to run when the plugin is activated.

I'm using a custom plugin that has been built via https://wppb.me/

Here is the code I'm running when installing Kadence:

    public static function add_kadence() {
        include_once(ABSPATH . 'wp-admin/includes/class-wp-upgrader.php');

        $kadence_blocks_slug = 'kadence-blocks';
        $kadence_theme_slug = 'kadence';
        $child_theme_slug = $kadence_theme_slug . '-child';
    
        // Theme installation
        $kadence_theme_installed = wp_get_theme($kadence_theme_slug);
        if (!$kadence_theme_installed->exists()) {
            // Theme URL
            $kadence_theme_url = 'https://downloads.wordpress.org/theme/' . $kadence_theme_slug . '.zip';
    
            // Create instance of theme installer
            $kadence_theme_installer = new Theme_Upgrader();
    
            // Install Theme
            $installation = $kadence_theme_installer->install($kadence_theme_url);
            if (is_wp_error($installation)) {
                error_log('Kadence theme installation failed: ' . $installation->get_error_message());
                return;
            }
        }
    
        // Child Theme installation
        $child_theme_installed = wp_get_theme($child_theme_slug);
        if (!$child_theme_installed->exists()) {
            // Child Theme URL
            $kadence_child_theme_url = 'https://dl.dropboxusercontent.com/scl/fi/zca2facxnwbl3le9jg6ke/' . $kadence_theme_slug . '-child.zip?rlkey=fo2sjq4fbmwhctb677u559w2a&dl=0';
    
            // Create instance of theme installer
            $kadence_child_theme_installer = new Theme_Upgrader();
    
            // Install Theme
            $installation = $kadence_child_theme_installer->install($kadence_child_theme_url);
            if (is_wp_error($installation)) {
                error_log('Kadence child theme installation failed: ' . $installation->get_error_message());
                return;
            }
    
            // Switch to Child Theme
            switch_theme($child_theme_slug);
        }
    
        Plugin installation and activation
        $kadence_blocks_installed = is_plugin_active($kadence_blocks_slug . '/' . $kadence_blocks_slug . '.php');
        if (!$kadence_blocks_installed) {
            // Plugin URL
            $kadence_blocks_url = 'https://downloads.wordpress.org/plugin/' . $kadence_blocks_slug . '.zip';
    
            // Create instance of the plugin installer
            $kadence_blocks_installer = new Plugin_Upgrader();
    
            // Install Plugin
            $installation = $kadence_blocks_installer->install($kadence_blocks_url);
            if (is_wp_error($installation)) {
                error_log('Kadence Blocks plugin installation failed: ' . $installation->get_error_message());
                return;
            }
    
            // Activate Plugin
            activate_plugin($kadence_blocks_slug . '/' . $kadence_blocks_slug . '.php');
        }
    }

This is ran within the activate function using self::add_kadence();.

Edit: I have read through similar questions but none are regarding custom plugins within wordpress and doesn't help solve my problem.

8
  • PHP send the HTTP headers when the PHP page produces output, even if is output is a line break. The error states: > headers already sent by (output started at /var/www/htdocs/wp-includes/functions.php:5420) So, look in functions.php, line 5420 to check what is done there. My guess is an empty line at the end of file, after a ?>. Remove the ?> if it's the last line of the file. This should fix the issue (note that this issue can be caused by a space or a line break before <?php). Generally, opened <?php shouldn't be closed, to prevent outputting unwanted line breaks. Commented Apr 22, 2024 at 14:00
  • Line 5420 is where ob_end_flush() is called and there's no closing ?> tag in the file. Commented Apr 22, 2024 at 14:03
  • The headers error will probably be because of the error message about ob_end_clean(), which obviously creates output. So if you fix that, per the many existing suggestions online about that error, it's likely to resolve both. I don't know that it being wordpress should make a great deal of difference...that function works the same in all of php Commented Apr 22, 2024 at 14:34
  • The reason why iI suspect that it's to do with wordpress is the error only occurs when installing a plugin. The plugin is installed and activated but still returns that error. Normally when installing a plugin you go to the plugin page within the wordpress repository. Commented Apr 22, 2024 at 14:43
  • 1
    Easy way to test. Change it to a default theme and try installing different plugins. Then swap the theme back and try a plugin that works. If it fails its caused by the theme. If not, test the custom plugin and see if the result is the same. If it fails the plugin is the cause. Commented Apr 22, 2024 at 15:27

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.