首页 > 解决方案 > Wordpress 我的第一个插件 - 在管理员中显示 - 在前端显示“Hello World”

问题描述

我创建了我的第一个插件,它在 WordPress 管理员中显示一个菜单

有 2 个问题我无法理解或如何在 WordPress 中修复它。

  1. 该插件Zach First Plugin在管理员中显示名称很好,但是当我单击它时,它会显示实际的上传表单。如何从管理员那里隐藏该表单,它应该只显示在带有简码的前端(无论我在哪里添加它)。添加了屏幕截图
  2. 当我使用此短代码在任何帖子中添加插件时,[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>";
}

?>

标签: wordpress

解决方案


推荐阅读