Plugin Development – Correct way to include external HTML page in wp-admin

I want to add a page that I already want to appear in wp-admin, so I added a new item to the wp admin menu

add_action('admin_menu', 'test_plugin_setup_menu');
 
function test_plugin_setup_menu(){
    add_menu_page( 'Test Plugin Page', ' add conent', 'manage_options', 'test-plugin', 'test_init' );
}
 
function test_init(){
    //echo "<h1>Hello World!</h1>";
    echo file_get_contents("..\wp-content\plugins\my-plugin\\form2.php");

}

Note here in my test_init I throw the file via file_get_contents

and my form 2 file content HTML header:

<!DOCTYPE html>
<html>
<head>
<!-- Latest compiled and minified CSS -->
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous">

<link href="https://cdn.jsdelivr.net/npm/froala-editor@3.0.0/css/froala_editor.pkgd.min.css" rel="stylesheet" type="text/css" />
<script type="text/javascript" src="https://cdn.jsdelivr.net/npm/froala-editor@3.0.0/js/froala_editor.pkgd.min.js"></script>

<style>
 ...
</style>

</head>
<body>
<!-- HTML for FORM code goes here -->
</body>


Now I have 2 questions:

Is this the right way to do this? Is there a WordPress-ish way to use it properly? file_get_contents Won’t execute PHP parts of file (HTML code printed with PHP loop)…

Also, is the fact that my custom HTML/PHP file contains some styles and the JS is imported automatically, causing a conflict with the whole site and theme? Or are these only used locally in the context of the form?

Edit: Additional question, how do I prevent direct access to …my-plugin/form2.php once the above issues are addressed

Leave a Comment