0

I have a task to create a parser plugin. The task of the parser is that it will load from another website (on the same server where the site is wp ) innerhtml text by className. After parsing data i should create wp post by custom post type and fill ACF( advanced custom field ) field.

NOTE: i use simple_html_dom for parsing ( it's free template for parsing ). On test server ( where i create plugin ) works fine, but when testing an another server and when activate plugin give some error

Error text: The plugin generated 22 characters of unexpected output during activation. If you notice “headers already sent” messages, problems with syndication feeds or other issues, try deactivating or removing this plugin.

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>
6
  • You're missing the closing brace from panda_parser_init - is there any more after that? I can't see the problem but I'd try removing blocks of code e.g. the whole body of panda_parser_init to see if the problem goes away, and then restore half and see if that causes the problem again to try and narrow down what's causing this. You should also be able to get WordPress to show you what those 22 characters are by tracking down the code that's generating the error.
    – Rup
    Commented Jul 1, 2020 at 7:27
  • @Rup This is the complete plugin code, the panda_parser_init function that closes before the last two divs. I included WP_DEBUG (true) in the wp-config.php file, but I couldn’t see the error, how to do this correctly for the plugin?
    – Bavarius
    Commented Jul 1, 2020 at 7:41
  • 1
    "the panda_parser_init function that closes before the last two divs" - you mean the last two divs aren't part of the function? (Why don't the indents line up?) Then that sounds like the problem then: they're being echoed out when you load the plugin, and there's roughly 22 characters there. What are they there for?
    – Rup
    Commented Jul 1, 2020 at 7:44
  • @Rup i change my code, add to last line <?php } ?> for close the_panda_parser_init (). Now give one error ... 1 character of unexpected output during activation ... (( i cut part of my code and test but still 1 character
    – Bavarius
    Commented Jul 1, 2020 at 7:56
  • 1
    I'd drop the trailing ?> and just end on the }. It might be echoing a final newline after the close PHP tag.
    – Rup
    Commented Jul 1, 2020 at 7:57

1 Answer 1

1

As @Rup has said in a plugin PHP file you should not have any HTML or any characters at all outside of the PHP opening and closing tags <?php and ?>.

It's possible sometimes that you have invisible whitespace before or after these tags which can generate this error. One workaround (also as @Rup says) is to makesure your PHP is valid and leave off the closing ?> PHP tag which helps with whitespace at the end of the while.

Adding answer for reference to others with this issue

1
  • Thanks for the answer, as @Rup advised, I changed the format and added closing tags. Thanks to everyone )
    – Bavarius
    Commented Jul 1, 2020 at 11:52

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.