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>
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>
Share
Improve this question
edited Jul 1, 2020 at 11:49
mozboz
2,6281 gold badge12 silver badges23 bronze badges
asked Jul 1, 2020 at 7:12
BavariusBavarius
1014 bronze badges
6
|
Show 1 more comment
1 Answer
Reset to default 1As @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
?>
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