Я создал свой первый плагин, который отображает меню в админке WordPress
Есть 2 проблемы, которые я не могу понять или как исправить в WordPress.
- Плагин отображает имя
Zach First Plugin
в админке, что нормально, но когда я нажимаю на него, он отображает фактическую форму загрузки. Как скрыть эту форму от администратора, она должна отображаться только во внешнем интерфейсе с шорткодом (куда бы я ни добавил его). Снимок экрана добавлен - Когда я добавляю плагин в любой пост с этим коротким кодом
[pdf id=""]
, он не отображает форму загрузки, просто отображает Hello world, не уверен, какие изменения кода я должен сделать.
Вот мой код
<?php
/*
Plugin Name: Zach First Plugin
Description: Plugin to create a menu in WP
Author: Zach
Version: 0.1
*/
//Call function to create an Admin menu element
add_action('admin_menu', 'test_plugin_setup_menu');
function test_plugin_setup_menu(){
add_menu_page( 'Zach Plugin Page', 'Zach Plugin', 'manage_options', 'test-plugin', 'test_init' );
}
//This function is called to handle the click function on the Admin menu element
function test_init(){
test_handle_post();
?>
<h1>Hello World!</h1>
<h2>Upload a File</h2>
<!-- Form to handle the upload - The enctype value here is very important -->
<form method="post" enctype="multipart/form-data">
<input type='file' id='test_upload_pdf' name='test_upload_pdf'></input>
<?php submit_button('Upload') ?>
</form>
<?php
}
// Handle the file upload
function test_handle_post(){
// First check if the file appears on the _FILES array
if(isset($_FILES['test_upload_pdf'])){
$pdf = $_FILES['test_upload_pdf'];
// Use the wordpress function to upload
// test_upload_pdf corresponds to the position in the $_FILES array
// 0 means the content is not associated with any other posts
$uploaded=media_handle_upload('test_upload_pdf', 0);
// Error checking using WP functions
if(is_wp_error($uploaded)){
echo "Error uploading file: " . $uploaded->get_error_message();
}else{
echo "File upload successful!";
}
}
}
//Add a Shortcode
add_shortcode("pdf", "test_process_shortcode");
function test_process_shortcode(){
return "<span style='color:blue;'>Hello world!</span>";
}
?>