wordpress - Wordpress 我的第一个插件 - 在管理员中显示 - 在前端显示“Hello World”
问题描述
我创建了我的第一个插件,它在 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>";
}
?>
解决方案
推荐阅读
- swift - 如何通过忽略时区将 UTC 字符串转换为日期?
- c# - 在事件处理程序中绑定 Button.text 属性
- scikit-learn - 具有 95% 置信区间的多类 AUC
- php - 为什么 Laravel vendor:publish 返回“无法找到可发布的资源”?
- c++ - 编译器是否有用 C++ 中的推断类型替换 auto 的功能?
- react-native - ReactNative 无法从 > 不同组件的函数体内更新组件
- r - R创建为每个不同组提供分组编号的列
- python - 外部for循环和内部if循环的时间复杂度
- postgresql - 从 spring-boot 连接到 PostgreSQL 副本
- vue.js - [Vue 警告]:“更新用户”的事件处理程序出错:“TypeError:无法设置未定义的属性‘名称’”