1

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

enter image description here

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.

1 Answer 1

1

it didn't work for you because hook admin_head is designed to add <link> tags inside <head> section. So even if you will render something there it won't be visible, because it is outside <body>.

If you want to render something after admin head bar, you should use wp_after_admin_bar_render hook:

https://developer.wordpress.org/reference/hooks/wp_after_admin_bar_render/

To determine if you are on a page where your tab should be visible, you can use the function get_current_screen():

https://developer.wordpress.org/reference/functions/get_current_screen/

Inside this link, you have an array with description what code you should use for certain pages.

Probably you will need additionally to check post type in edition page, but you can do this using the same function like that:

if ( 'your-custom-post-type' !== $screen->post_type ) { 
  //this is not our custom post, so let's exit
  return;
}

Hope this will help.

3
  • Thanks i used the wp_after_admin_bar_render hook and it even behaves better than admin_notices for example which kind of solved my problem but i had to use additional css. With wp_after_admin_bar_render - Screen Options are pushed below - which is exactly what i wanted (with admin_notices Screen Options was displayed on top of my header).
    – Ion T
    Mar 20 at 8:20
  • I used the following code to make sure the header is displayed only on the pages related to my plugin: global $pagenow, $typenow; ?> <?php if ( $typenow === 'custom-post-type' || 'post.php' === $pagenow) { ?> <?php include plugin_dir_path( __FILE__ ) . '/Views/CustomHeaderView.php'; ?> <?php } ?> <?php
    – Ion T
    Mar 20 at 8:21
  • Found another hook that does the same job : in_admin_header developer.wordpress.org/reference/hooks/in_admin_header
    – Ion T
    Mar 20 at 9:15

Your Answer

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

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