I’m trying to create a plugin for wordpress that automatically posts data from an API.
I’ve put the code that generates the HTML in a class:
class Poster{
public function generateHTML($data){
ob_start();
/*
some html and php code
*/
$html = ob_get_contents();
ob_end_flush();
return $html;
}
}
The event is triggered on the admin_menu action:
add_action('admin_menu', function(){
/*
get data from API
*/
$poster = new Poster();
$html = $poster->generateHTML($data);
$post = array(
'post_title' => $title,
'post_content' => $html,
'post_type' => 'post',
'comment_status' => 'open',
'ping_status' => 'open',
'post_status' => 'publish'
);
wp_insert_post($post, $wp_error, true);
});
Is there something wrong with my code?
It works sometimes but most of the time it doesn’t.
And by ‘it works’ I mean the html is returned from the method and then stored in the $html variable. But most of the time the html returned from the method is the only one that’s being outputted and it outputs in the admin panel(the rest of the admin panel is not outputted only the contents generated from the method is outputted).
So is there anything wrong with how I approach this?
Is there an alternative that I can do to achieve the same result? Thanks in advance!
Put a error_log message to find out the path and when it does display only your text in admin then catch it from there. Difficult to guess but you need debugging with error log.