插件激活错误-插件在激活过程中生成22个字符的意外输出

时间:2020-07-01 作者:Bavarius

我的任务是创建一个解析器插件。解析器的任务是,它将从另一个网站加载(在该网站为wp的同一服务器上)按类名加载innerhtml文本。解析数据后,我应该通过自定义帖子类型和填充创建wp帖子ACF(高级自定义字段)字段。

NOTE: 我使用simple\\u html\\u dom进行解析(它是用于解析的免费模板)。在测试服务器(我创建插件的地方)上运行良好,但在测试另一台服务器和激活插件时会出现一些错误

Error text:该插件在激活期间生成了22个字符的意外输出。如果您注意到“headers ready sent”消息、联合提要问题或其他问题,请尝试停用或删除此插件。

Plugin.php

add_action(\'admin_menu\', \'panda_parser_setup_menu\');
 
function panda_parser_setup_menu(){
        add_menu_page( \'Panda Parser Plugin\', \'Panda Parser\', \'manage_options\', \'panda-parser-plugin\', \'panda_parser_init\' );

     //Init admin css
    function panda_admin_view() {
        wp_register_style(\'panda-admin-view\', plugins_url(\'/view/panda-admin-view.css\',__FILE__ ), array(), time());
        wp_enqueue_style(\'panda-admin-view\');
    }

    add_action( \'admin_init\',\'panda_admin_view\');

}
 
function panda_parser_init(){ ?>
        <div class="panda-container">
                <div class="panda-plugin-box">
                    <div class="panda-header">
                        <img src="<?php echo plugins_url(\'/view/panda.svg\',__FILE__ )?>" alt="panda" width=\'50\' height=\'50\'>
                        <div class="panda-info">
                            <div class="panda-header-text"><strong>Panda Parser</strong></div>
                            <div>                               
                                <div><strong>version:</strong> 1.0</div>
                                <div><strong>author:</strong> Dev Team</div>
                                <div><strong>company:</strong> <a href="#" target="_blank" >Company</a></div>
                                <div><strong>description:</strong> Custom HTML parser for wp posts</div>    
                            </div>
                        </div>
                    </div>  
                    <form name="form" method="post" class="panda-form">
                        <input type="text" name="vacancy" value="" class="get_url" id="get_url">
                        <button type="submit" class="create-btn">Create</button>
                    </form>

                    <?php
                        include("simple_html_dom.php");

                        if(isset($_POST[\'vacancy\'])) {

                            $vacancy_url = $_POST[\'vacancy\'];

                            $data = file_get_html($vacancy_url);

                            $css_class = [
                                \'jobtitle\', 
                                \'jobsubtitle\', 
                                \'jobintro\', 
                                \'joboffer\', 
                                \'jobrequirement\', 
                                \'jobcompanydescription\', 
                                [\'name\' => \'contact-left\', \'type\' => \'img\'], 
                                \'contact-right\',
                                [\'link_btn\' => \'recruiter-box\', \'type\' => \'href\']
                            ];

                            $foundData = [];

                            if ($data) {
                                foreach ($css_class as $value) {
                                    if (!is_array($value) && $data->find( "." . $value, 0)) {
                                        $foundData[$value] = $data->find( "." . $value, 0)->innertext;
                                    } elseif (is_array($value) && $value[\'type\'] == \'img\') {
                                        $foundData[$value[\'name\']] = rtrim($vacancy_url , \'/\') . \'/\' . $data->find( "." . $value[\'name\'] . \' > img\', 0)->src;
                                    } elseif (is_array($value) && $value[\'type\'] == \'href\') {
                                        $foundData[$value[\'link_btn\']] = $data->find( "." . $value[\'link_btn\'], 0)->href;
                                    }
                            }
                            } else {
                                echo \'Parse error \';
                            }

                            if (empty($foundData)) {
                                echo \'Content not found\';
                            }

                            //Create new post
                            $post_data = array(

                                \'post_type\' => \'jobs\',
                                \'post_status\'   => \'publish\',
                                \'post_title\'    => $foundData[\'jobtitle\'],
                                \'post_author\'   => $user_ID,
                                \'post_category\' => 15
                            );

                            $post_id = wp_insert_post( $post_data );


                            //Update ACF fields
                            $acf_keys = [ 
                                \'location\', 
                                \'job_intro\', 
                                \'job_offer\', 
                                \'job_requirement\', 
                                \'text_c2\', 
                                \'foto\', 
                                \'contact_details\',
                                \'btn_url\' 
                            ];

                            if(is_array($acf_keys)) {
                                foreach ($acf_keys as $value) {
                                    switch ($value) {
                                        case \'location\':
                                            update_field( $value, $foundData[\'jobsubtitle\'], $post_id );
                                            break;
                                        case \'job_intro\':
                                            update_field( $value, $foundData[\'jobintro\'], $post_id );
                                            break;
                                        case \'job_offer\':
                                            update_field( $value, $foundData[\'joboffer\'], $post_id );
                                            break;
                                        case \'job_requirement\':
                                            update_field( $value, $foundData[\'jobrequirement\'], $post_id );
                                            break;
                                        case \'text_c2\':
                                            update_field( $value, $foundData[\'jobcompanydescription\'], $post_id );
                                            break;
                                        case \'foto\':
                                            update_field( $value, $foundData[\'contact-left\'], $post_id );
                                            break;
                                        case \'contact_details\':
                                            update_field( $value, $foundData[\'contact-right\'], $post_id );
                                            break;
                                        case \'btn_url\':
                                            update_field( $value, $foundData[\'recruiter-box\'], $post_id );
                                            break;
                                        default:
                                            break;
                                    }
                                }

                            }
                            
                            echo "<p class=\'panda-success\'>";
                            echo "Your post was created successfully :)";
                            echo \'</p>\';
                        }   
                    }?>
                </div>
            </div>

1 个回复
SO网友:mozboz

正如@Rup在一个插件PHP文件中所说的,在PHP打开和关闭标记之外,您不应该有任何HTML或任何字符<?php?>.

有时可能在这些标记之前或之后有不可见的空格,这会产生此错误。一个解决方法(也如@Rup所说)是确保您的PHP有效,并且不必关闭?> PHP标记,有助于在while结束时使用空格。

正在添加答案,以供有此问题的其他人参考