• I’m trying to create a plugin in which I want the option of uploading pdf document file. My code is as follows:

    How can I proceed further? What function should I use in register_setting and how do I use file_upload function

    class pdfPlugin {
        function __construct() {
            add_action( 'admin_menu', array($this, 'pdfPlugin'));
            add_action( 'admin_init', array($this, 'settings'));
        }
    
        function settings() {
            add_settings_section( 'pdf-first-section', null, null, 'pdf-plugin-settings');
    
            add_settings_field( 'document_name', 'Document Name', array($this, 'docHTML'), 'pdf-plugin-settings', 'pdf-first-section');
            register_setting( 'pdf_plugin_group', 'document_name', array('sanitize_callback' => 'sanitize_text_field', 'default' => 'My Document'));
    
            add_settings_field( 'pdf_upload', 'Upload PDF', array($this, 'uploadHTML'), 'pdf-plugin-settings', 'pdf-first-section');
            register_setting( 'pdf_plugin_group', 'pdf_upload', array($this, 'pdfUploadFile'));
        }
    
        function docHTML() { ?>
            <input type="text" name="document_name" value="<?php echo esc_attr(get_option('document_name'))?>">
        <?php   }
    
        function pdfPlugin() {
            add_options_page( 'Pdf Plugin Settings', 'Pdf Plugin', 'manage_options', 'pdf-plugin-settings', array($this, 'pluginHTML'));
        }
    
        function pluginHTML() { ?>
            <div class="wrap">
                <h1>PDF Plugin Settings Page</h1>
                <form action="options.php" method="POST">
                    <?php
                        settings_fields( 'pdf_plugin_group' );
                        do_settings_sections( 'pdf-plugin-settings' );
                        submit_button();
                    ?>
                </form>
            </div>
        <?php   }
    }
    
    $PdfPlugin = new pdfPlugin();
Viewing 2 replies - 1 through 2 (of 2 total)
  • Moderator threadi

    (@threadi)

    Take a look at the manual for plugin development: https://developer.ww.wp.xz.cn/plugins/ – it describes how you can set up a plugin.

    Moderator bcworkz

    (@bcworkz)

    AFAIK you cannot use the settings API to do file uploads because all settings are saved as option values. In your form’s code, include a file input field that’s outside of the Settings API scheme, but within the same form. Find an appropriate action hook from which to handle the field’s data. “admin_init” perhaps? Have your callback utilize wp_handle_upload() to properly handle the upload data and create a related attachment post.

Viewing 2 replies - 1 through 2 (of 2 total)

The topic ‘How to add Upload File Function in WordPress’ is closed to new replies.