Finding the screen id of a page generated with add_menu_page

I'm at a basic level of WordPress + PHP and I'm trying to build a pretty simple plugin.

I've set up the plugin correctly, and the plugin page is generated with

function wpplugin_settings_page() {
    add_menu_page(
        'Modal Generator',
        'Modal Generator',
        'manage_options',
        'modal-slug',
        'wpplugin_settings_page_markup',
        'dashicons-format-gallery',
        100
    );
}

Whereas wpplugin_settings_page_markup is a function that generates my HTML on the plugin page.

I copy-pasted a code to create/generate metaboxes and they work great. I can get them to show on posts pages with this code:

function single_repeater_meta_boxes() {
    add_meta_box( 'single-repeater-data', 'Single Repeater', 'single_repeatable_meta_box_callback', 'page', 'normal', 'default');
}

I achieved this by changing the $screen-parameter, as WordPress' official documentation states:

$screen

(string|array|WP_Screen) (Optional) The screen or screens on which to show the box (such as a post type, 'link', or 'comment'). Accepts a single screen ID, WP_Screen object, or array of screen IDs. Default is the current screen. If you have used add_menu_page() or add_submenu_page() to create a new screen (and hence screen_id), make sure your menu slug conforms to the limits of sanitize_key() otherwise the 'screen' menu may not correctly render on your page.

However, I can't get them to show on my dynamically generated plugin page. I somehow need to find the screen ID and use that as the parameter value for $screen. The slug modal-generator shouldn't be an issue. I hope my question makes sense and if not, feel free to ask.

add_action('admin_init', 'single_repeater_meta_boxes', 2);

function single_repeater_meta_boxes() {
    add_meta_box( 'single-repeater-data', 'Single Repeater', 'single_repeatable_meta_box_callback', 'toplevel_page_wp-nordic-modal', 'normal', 'default');
}

/* =========== GENERATING METABOXES =========== */
function single_repeatable_meta_box_callback($post) {

    $single_repeater_group = get_post_meta($post-ID, 'single_repeater_group', true);
    $banner_img = get_post_meta($post-ID,'post_banner_img',true);
    wp_nonce_field( 'repeaterBox', 'formType' );
    ?
    script type=text/javascript
        jQuery(document).ready(function( $ ){
            $( '#add-row' ).on('click', function() {
                var row = $( '.empty-row.custom-repeater-text' ).clone(true);
                row.removeClass( 'empty-row custom-repeater-text' ).css('display','table-row');
                row.insertBefore( '#repeatable-fieldset-one tbodytr:last' );
                return false;
            });

            $( '.remove-row' ).on('click', function() {
                $(this).parents('tr').remove();
                return false;
            });
        });

    /script

    table id=repeatable-fieldset-one width=100%
        tbody
            ?php
            if ( $single_repeater_group ) :
                foreach ( $single_repeater_group as $field ) {
                    ?
                    tr
                        tdinput type=text  style=width:98%; name=title[] value=?php if($field['title'] != '') echo esc_attr( $field['title'] ); ? placeholder=Heading //td
                        tdinput type=text  style=width:98%; name=tdesc[] value=?php if ($field['tdesc'] != '') echo esc_attr( $field['tdesc'] ); ? placeholder=Description //td
                        tda class=button remove-row href=#1Remove/a/td
                    /tr
                    ?php
                }
            else :
                ?
                tr
                    tdinput type=text   style=width:98%; name=title[] placeholder=Heading//td
                    tdinput type=text  style=width:98%; name=tdesc[] value= placeholder=Description //td
                    tda class=button  cmb-remove-row-button button-disabled href=#Remove/a/td
                /tr
            ?php endif; ?
            tr class=empty-row custom-repeater-text style=display: none
                tdinput type=text style=width:98%; name=title[] placeholder=Heading//td
                tdinput type=text style=width:98%; name=tdesc[] value= placeholder=Description//td
                tda class=button remove-row href=#Remove/a/td
            /tr

        /tbody
    /table
    pa id=add-row class=button href=#Add another/a/p
    ?php
}

/* =========== SAVING METABOXES ===========*/
add_action('save_post', 'single_repeatable_meta_box_save');

function single_repeatable_meta_box_save($post_id) {

    if (!isset($_POST['formType'])  !wp_verify_nonce($_POST['formType'], 'repeaterBox'))
        return;

    if (defined('DOING_AUTOSAVE')  DOING_AUTOSAVE)
        return;

    if (!current_user_can('edit_post', $post_id))
        return;

    $old = get_post_meta($post_id, 'single_repeater_group', true);

    $new = array();
    $titles = $_POST['title'];
    $tdescs = $_POST['tdesc'];
    $count = count( $titles );
    for ( $i = 0; $i  $count; $i++ ) {
        if ( $titles[$i] != '' ) {
            $new[$i]['title'] = stripslashes( strip_tags( $titles[$i] ) );
            $new[$i]['tdesc'] = stripslashes( $tdescs[$i] );
        }
    }

    if ( !empty( $new )  $new != $old ){
        update_post_meta( $post_id, 'single_repeater_group', $new );
    } elseif ( empty($new)  $old ) {
        delete_post_meta( $post_id, 'single_repeater_group', $old );
    }
    $repeater_status= $_REQUEST['repeater_status'];
    update_post_meta( $post_id, 'repeater_status', $repeater_status );
}

/* =========== PLUGIN PAGE ===========*/
function wpplugin_settings_page()
{
    add_menu_page(
        'Modal Generator',                  // page title
        'Modal Generator',                  // menu title
        'manage_options',                   // capability
        'wp-nordic-modal',                  // menu_slug
        'wpplugin_settings_page_markup',    // callable function
        'dashicons-format-gallery',         // icon
        100
    );
    add_submenu_page(
      'wp-nordic-modal',
      __( 'Popup Generator', 'wp-nordic-modal' ),
      __( 'Popup', 'wp-nordic-modal' ),
      'manage_options',
      'wpplugin-feature-1',
      'wpplugin_settings_subpage_markup'
    );

}
add_action( 'admin_menu', 'wpplugin_settings_page' );


function wpplugin_settings_page_markup()
{
    // Double check user capabilities
    if ( !current_user_can('manage_options') ) {
      return;
    }?
    div class=wrap
      h1?php esc_html_e( get_admin_page_title() ); ?/h1
      p?php esc_html_e( 'Some content.', 'wpplugin' ); ?/p
    /div
    divsave/div
    ?php
}

function wpplugin_settings_subpage_markup()
{
  // Double check user capabilities
  if ( !current_user_can('manage_options') ) {
      return;
  }
  ?
  div class=wrap
    h1?php esc_html_e( get_admin_page_title() ); ?/h1
    p?php esc_html_e( 'Some content.', 'wpplugin' ); ?/p
  /div
  ?php
}

Topic add-menu-page php metabox plugins Wordpress

Category Web


The function add_menu_page return the hook suffix that inform you about the current page.

A simple example to get the hook suffix.

public function add_menu_page() {
    $page_hook_suffix = add_menu_page(
        esc_html__( 'Admin Page', 'translation-slug' ),
        esc_html__( 'Admin Page', 'translation-slug' ),
        'read',
        'My Name',
        array( $this, 'get_styles' )
    );
    add_action( 'admin_print_scripts-' . $page_hook_suffix, array( $this, 'enqueue_assets' ) );
}

The example above add the scripts only on the custom admin page, because the hook admin_print_scripts-* is fired only on the hook suffix from the return value of add_menu_page.

WP_Screen

Also, you can use the function get_current_screen to identify the page and use the value after identification.

Helpers

There are also helper plugins to find this value, if is not dynamically, like Query Monitor and Debug Objects.

Screenshot

Query Monitor Current Screen

About

Geeks Mental is a community that publishes articles and tutorials about Web, Android, Data Science, new techniques and Linux security.