I want to add a page that I have already to appear in the WP-admin, so I added a new item to 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");
}
Notice here in my test_init I just throw the file via file_get_contents
And my form2 file contents HTML headers:
<!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/[email protected]/css/froala_editor.pkgd.min.css" rel="stylesheet" type="text/css" />
<script type="text/javascript" src="https://cdn.jsdelivr.net/npm/[email protected]/js/froala_editor.pkgd.min.js"></script>
<style>
...
</style>
</head>
<body>
<!-- HTML for FORM code goes here -->
</body>
Now my 2 questions are :
1-Is this the right way to do this? is there any WordPress-ish way to do this properly using file_get_contents
won't execute the PHP parts of the file (HTML code printed with a PHP loop)...
2-Also, does the fact that my custom HTML/PHP file has some styles and JS imported on its own cause any conflict with the entire site and the theme? or are these only used locally in the context of the form?
Edit: based on the comments, include
will do the job for question 1, and as I suspected having nested HTML tags is indeed a bad idea, so I need to remove body, HTML, head tags, etc and leave the form only but how do I include style and script tags only for this particular page and not the entire site if I remove the head tags? In HTML it is not a good practice to put the style tag (and <link rel="stylesheet"
) in the body and I don't want to slow down the entire site by using enqueue and adding the JS/CSS files everywhere
file_get_contents
won't execute any PHP code in that file, and you'll have nested HTML tags and body tags. It's also bad practice and insecure to make direct requests to PHP files in your plugin/theme from a browser, be that for form handling or for AJAX/JS requestsinclude
. That’s basic PHP and it works the same with WordPress.