Plugin Submenu Blank But Code Works As Its Own Plugin

I am creating a plugin for a client that displays environment information for their particular cloud stack that’s hosted on AWS. This all works great. I need to add in the option to make the URL’s relative.

The issue is, either in the admin page or even as a submenu page, when I add in the code for the relative URL’s toggle, the page goes blank.

However, if I take that exact same code and make it its own plugin it works great. I can make it a plugin with an admin page or a subpage of WP Tools or WP Settings with no issue.

I’ve been trying to figure out why this keeps happening for days now and I can’t seem to figure it out. Can someone take a look and please help?

Here’s the plugin admin page file:

add_action( ‘admin_menu’, ‘my_admin_menu’ );

function my_admin_menu() {
add_menu_page( ‘My Control Panel’, ‘My Control Panel’, ‘manage_options’, __FILE__, ‘my_admin_page’, ‘dashicons-cloud’, 0 );

add_submenu_page( __FILE__, ‘My Settings’, ‘Settings’, ‘manage_options’, __FILE__.’/settings’, ‘my_options_page’ );
}

function my_admin_page(){
?>
<div class=”wrap”>
<img src=”Logo.png” />
<div class=”section group”>
<?php include(‘AccordionScale.php’);?>
<div class=”col span_1_of_3″>
<h3>AWS_ENV_PATH</h3>
<p><?php echo getenv(‘AWS_ENV_PATH’); ?></p>
</div>
<div class=”col span_1_of_3″>
<h3>AWS_REGION</h3>
<p><?php echo getenv(‘AWS_REGION’); ?></p>
</div>
<div class=”col span_1_of_3″>
<h3>My Database Endpoints</h3>
<p><strong>Write:</strong> <?php echo getenv(‘MY_DB_WRITE_NODE’); ?></p>
<p><strong>Read:</strong> <?php echo getenv(‘MY_DB_READ_NODE’); ?></p>
</div>
</div>
</div>

<style>
.wrap h2 { color:#002756; }
.span_1_of_3 h3 { padding-left:20px !important; color:#002756; font-size:18px; }
.span_1_of_3 p { padding-left:20px !important; }

/* SECTIONS */
.section {
clear: both;
padding: 0px;
margin: 0px;
}

/* COLUMN SETUP */
.col {
display: block;
float:left;
margin: 1%;
}
.col:first-child { margin-left: 0; }

/* GROUPING */
.group:before,
.group:after { content:””; display:table; }
.group:after { clear:both;}
.group { zoom:1; /* For IE 6/7 */ }

/* GRID OF THREE */
.span_3_of_3 { width: 100%; }
.span_2_of_3 { width: 30%; border: 2px solid #002756; border-radius:3%; }
.span_1_of_3 { width: 30%; border: 2px solid #002756; border-radius:3%; }

/* GO FULL WIDTH BELOW 480 PIXELS */
@media only screen and (max-width: 480px) {
.col { margin: 1% 0 1% 0%; }
.span_3_of_3, .span_2_of_3, .span_1_of_3 { width: 100%; }
}
</style>

<?php
}

function my_options_page( ) {

include( plugin_dir_path( __FILE__ ) . ‘makerelativeurls.php’);

?>

<?php
}
add_action( ‘wp_dashboard_setup’, ‘register_my_dashboard_widget’ );
function register_my_dashboard_widget() {
global $wp_meta_boxes;
wp_add_dashboard_widget(
‘my_dashboard_widget’,
‘My Control Panel Preview’,
‘my_dashboard_widget_display’
);

$dashboard = $wp_meta_boxes[‘dashboard’][‘normal’][‘core’];

$my_widget = array( ‘my_dashboard_widget’ => $dashboard[‘my_dashboard_widget’] );
unset( $dashboard[‘my_dashboard_widget’] );

$sorted_dashboard = array_merge( $my_widget, $dashboard );
$wp_meta_boxes[‘dashboard’][‘normal’][‘core’] = $sorted_dashboard;
}

add_action( ‘wp_dashboard_setup’, ‘register_my_dashboard_widget’ );

function my_dashboard_widget_display() {

?>
<p>Welcome to the My Control dashboard! Need help? Contact Us <a href=”http://www.mywebsite.com”>here</a>.</p>
<p><a href=”admin.php?page=my-plugin.php”>Update your settings</a></p>
<p><strong>Your Environment:</strong> <?php echo getenv(‘HOSTNAME’);?></p>
<?

}

function plugin_add_settings_link( $links ) {
$settings_link = ‘<a href=”admin.php?page=my-plugin.php”>’ . __( ‘Settings’ ) . ‘</a>’;
array_push( $links, $settings_link );
return $links;
}
$plugin = plugin_basename( __FILE__ );
add_filter( “plugin_action_links_$plugin”, ‘plugin_add_settings_link’ );

And here is the makerelativeurls.php file that causes the page to go blank but when as its own plugin, works as intended.

add_action( ‘admin_menu’, ‘test_plugin_add_admin_menu’ );
add_action( ‘admin_init’, ‘test_plugin_settings_init’ );

function test_plugin_add_admin_menu( ) {

add_submenu_page( ‘options-general.php’, ‘Another Test Plugin’, ‘Another Test Plugin’, ‘manage_options’, __FILE__, ‘test_plugin_options_page’ );

}

function test_plugin_settings_init( ) {

register_setting( ‘pluginPage’, ‘test_plugin_settings’ );

add_settings_section(
‘test_plugin_pluginPage_section’,
__( ‘Turn on/off settings here.’, ‘Settings Control’ ),
‘test_plugin_settings_section_callback’,
‘pluginPage’
);

add_settings_field(
‘test_plugin_select_field_0’,
__( ‘Do you want relative URLs?’, ‘Test Plugin’ ),
‘test_plugin_select_field_0_render’,
‘pluginPage’,
‘test_plugin_pluginPage_section’
);

}

function test_plugin_select_field_0_render( ) {

$options = get_option( ‘test_plugin_settings’ );
?>
<select name=’test_plugin_settings[test_plugin_select_field_0]’>
<option value=’1′ <?php selected( $options[‘test_plugin_select_field_0′], 1 ); ?>>Yes</option>
<option value=’2’ <?php selected( $options[‘test_plugin_select_field_0’], 2 ); ?>>No</option>
</select>

<?php

}

function test_plugin_settings_section_callback( ) {

echo __( ‘Turn on/off settings here.’, ‘Settings Control’ );

}

function test_plugin_options_page( ) {

?>
<form action=’options.php’ method=’post’>

<?php
settings_fields( ‘pluginPage’ );
do_settings_sections( ‘pluginPage’ );
submit_button();
?>

</form>
<?php

}

;

if ($options[‘test_plugin_select_field_0’] = 1) {
add_action( ‘template_redirect’, ‘relative_url’ );

include( plugin_dir_path( __FILE__ ) . ‘relativeurls.php’);

}

if ($options[‘test_plugin_select_field_0’] = 2) {
remove_action( ‘template_redirect’, ‘relative_url’ );

;
}

Read more here:: Plugin Submenu Blank But Code Works As Its Own Plugin

Leave a Reply

Your email address will not be published. Required fields are marked *