0

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

8
  • 1
    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 requests
    – Tom J Nowell
    Commented Nov 9, 2022 at 17:20
  • @TomJNowell , Thanks for the comment. I sort of had doubts about everything you mentioned, therefore the question... Any ideas or suggestions on how to achieve this properly? I guess I should start by stripping the external file from body and headed tags, but in that case, how do I add styles for that form only?
    – user206904
    Commented Nov 9, 2022 at 17:28
  • Do people who develop plugins with complex pages in WP_admin really throw all their HTML inside an echo in the same file ?! I am sure they divide things between files...
    – user206904
    Commented Nov 9, 2022 at 17:29
  • “ 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” Yes, it will cause conflicts with the WordPress admin. Commented Nov 10, 2022 at 2:07
  • 1
    @user206904 You can include HTML/PHP from other files with include. That’s basic PHP and it works the same with WordPress. Commented Nov 10, 2022 at 2:08

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Browse other questions tagged or ask your own question.