3

I want to create a wordpress plugin by just following the example listed here based on a class OOP architecture with an external setup object, and adapting the source code on my own way like this: main plugin file:

<?php
/*
Plugin Name: My Plugin
Description: My Plugin
Version: 1.0
Author: drwhite
Author URL:  drwhite-site
Plugin URL:  drwhite-site/video-ad-overlay
*/

register_activation_hook(__FILE__, array( 'VAO_Setup_File_Inc', 'on_activation'));
register_deactivation_hook(__FILE__, array( 'VAO_Setup_File_Inc', 'on_deactivation'));
register_uninstall_hook(__FILE__, array( 'VAO_Setup_File_Inc', 'on_uninstall'));

add_action('plugins_loaded', array( 'VAO_Setup_File', 'init'));
class VAO_Setup_File{

    protected static $instance;

    public static function init()
    {
        is_null( self::$instance ) AND self::$instance = new self;
        return self::$instance;
    }

    public function __construct()
    {
        add_action( current_filter(), array( $this, 'load_files' ));
    }

    public function load_files()
    {
        foreach ( glob( plugin_dir_path( __FILE__ ).'includes/*.php' ) as $file ){
            include_once $file;
        }
    }
}

In my plugin root directory i have created a subdirectory called includes within i put the setup file to be loaded on plugin load called setup.class.php:

<?php
class VAO_Setup_File_Inc
{
    public static function on_activation()
    {
        if ( ! current_user_can( 'activate_plugins' ) )
            return;
        $plugin = isset( $_REQUEST['plugin'] ) ? $_REQUEST['plugin'] : '';
        check_admin_referer( "activate-plugin_{$plugin}" );
    }

    public static function on_deactivation()
    {
        if ( ! current_user_can( 'activate_plugins' ) )
            return;
        $plugin = isset( $_REQUEST['plugin'] ) ? $_REQUEST['plugin'] : '';
        check_admin_referer( "deactivate-plugin_{$plugin}" );
    }

    public static function on_uninstall()
    {
        if ( ! current_user_can( 'activate_plugins' ) )
            return;
        check_admin_referer( 'bulk-plugins' );

        // Important: Check if the file is the one
        // that was registered during the uninstall hook.
        if ( __FILE__ != WP_UNINSTALL_PLUGIN )
            return;
    }
}

When i activate the plugin i got an error like the following: enter image description here

I have read several questions posted by other users here and this may be duplicated question, but any of suggested answer worked for me including:

  • Remove space from start of tags and even remove the php end tag: nothing changed

  • in wp_config.php file i set wp_DEBUG to true , but it doesn't show errors

  • I have converted the file to UTF8 (without BOM) nothing changed

Have you put the eye in the issue ?

Community
  • 1
  • 1
Drwhite
  • 1,545
  • 4
  • 21
  • 44
  • Are there other PHP files than `setup.class.php` included by way of the `include_once $file` line? – Jeff Sisson May 29 '15 at 00:53
  • @JeffSisson Actually, there is any. But i certainly add them later for optimal construction. – Drwhite May 29 '15 at 00:58
  • Try deleting all the code within on_activation(), deactivate, then reactivate the plugin, then tell us what happens. [If that does something, go through the function line by line.] – Jim Maguire Sep 07 '15 at 01:14

1 Answers1

0

You are getting this error because your plugin is generating a PHP error that is being outputted to the page and causing the headers sent error you see... The problem with your code is that your function

public function load_files()
    {
        foreach ( glob( plugin_dir_path( __FILE__ ).'includes/*.php' ) as $file ){
            include_once $file;
        }
    }

is not being called in time, so

register_activation_hook(__FILE__, array( 'VAO_Setup_File_Inc', 'on_activation'));

is looking for a function that doesn't exist, inside a class that doesn't exist. Move your

foreach ( glob( plugin_dir_path( __FILE__ ).'includes/*.php' ) as $file ){
            include_once $file;
        }

outside the class altogether, and it'll load just fine. It may require you to rethink your use of

add_action('plugins_loaded', array( 'VAO_Setup_File', 'init'));

and the way your plugin is being created, but it's a step in the right direction. If you copy and paste the code from the link you got this code from, his code displays the same problem...

Daniel C
  • 2,105
  • 12
  • 18