I need to display custom block of php that contains a header with logo, buttons etc just below the wpadminbar ideally in the wpcontent div position as a relative element.
The respective header needs to be displayed on all pages related to my plugin only:
- Custom post list - edit.php?post_type=cpt;
- Custom post edit page - post.php?
- Settings page edit.php?post_type=cpt&page=cpt_settings
Here is what i tried so far but with no luck:
Approach #1
public function init() {
/**
* This general class is always being instantiated as requested in the Bootstrap class
*
* @see Bootstrap::__construct
*
* Add plugin code here
*/
add_action( 'admin_head', [ $this, 'filterPluginAdminPages' ] );
}
/**
* CPT admin page
*
* @since 1.0.0
*/
function filterPluginAdminPages() {
global $pagenow, $typenow;
if ( $typenow === 'custom-post-type' || 'post.php' === $pagenow) {
ob_start();
include plugin_dir_path( __FILE__ ) . '/Views/HeaderView.php';
return ob_get_clean();
}
}
Approach #2
public function init() {
/**
* This general class is always being instantiated as requested in the Bootstrap class
*
* @see Bootstrap::__construct
*
* Add plugin code here
*/
add_action( 'admin_head', [ $this, 'filterPluginAdminPages' ] );
}
function filterPluginAdminPages() {
echo '<script>';
echo 'document.addEventListener("DOMContentLoaded", function() {';
echo 'var wpcontent = document.getElementById("wpcontent");';
echo 'var AdminHeader = document.createElement("div");';
echo 'AdminHeader.setAttribute("class", "admin-bar");';
echo 'AdminHeader.innerHTML = "'.addslashes( file_get_contents( plugin_dir_path(__FILE__) . 'HeaderView.php' ) ).'";';
echo 'wpcontent.insertBefore(AdminHeader, wpcontent.firstChild);';
echo '});';
echo '</script>';
}
In the end i want to achieve a similar header section like the one from Advanced Custom Fields plugin
I also tried doing it via notices hook and admin_footer but i don't like the involvement of JS to change the position of the header - you can do it without any WP hooks, i want to do it the php/right way.