dongxiejie9387 2015-05-28 20:28 采纳率: 100%
浏览 46

插件在激活期间生成N个意外输出字符[重复]

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 ?

</div>
  • 写回答

1条回答 默认 最新

  • drwn65609 2015-11-19 20:30
    关注

    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...

    评论

报告相同问题?

悬赏问题

  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 目详情-五一模拟赛详情页
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b