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 usedadd_menu_page()
oradd_submenu_page()
to create a new screen (and hence screen_id), make sure your menu slug conforms to the limits ofsanitize_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
}