$conf, $runtime; function_exists('chdir') AND chdir(APP_PATH); $r = 'mysql' == $conf['cache']['type'] ? website_set('runtime', $runtime) : cache_set('runtime', $runtime); } function runtime_truncate() { global $conf; 'mysql' == $conf['cache']['type'] ? website_set('runtime', '') : cache_delete('runtime'); } register_shutdown_function('runtime_save'); ?>uploads - Custom delete option button in plugin settings|Programmer puzzle solving
最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

uploads - Custom delete option button in plugin settings

matteradmin6PV0评论

I would like to add Delete button next to my file Upload option in my plugin settings.

At this moment I am using Settings API, using form below.

<form method="post" action="options.php" enctype="multipart/form-data">
    <?php
        settings_fields("mw_options");
        do_settings_sections("mw_options");      
        submit_button(); 
    ?>          
</form>

It works great, I have about 10 fields in the form, one of them is file upload that adds attachments to a Woocommerce e-mails. Everything works great, but I would like to add Delete button next to the Upload button in my form, so I can use updated_option or delete_option hook, or something more suited.

I can not figure out, how to do that.

Now I use my own delete_attachment.php and formaction on the button to get the job done, but I would like to do more of this later and I feel there is a better way.

function mw_options_wc_order_attachement_1()
{
    ?>
        <input type="file" name="mw_options_wc_order_attachement_1" id="mw_options_wc_order_attachement_1" value="<?php echo get_option('mw_options_wc_order_attachement_1'); ?>" />
        <?php echo get_option("mw_options_wc_order_attachement_1"); ?>
        <input type="submit" name="mw_options_wc_order_attachement_1" formaction="<?php echo plugins_url( 'delete_attachment.php' , __FILE__ );?>" formmethod="post" value="Delete">
    <?php
}

How can I create another button within the main option's area that would execute some delete script I have?

I would like to add Delete button next to my file Upload option in my plugin settings.

At this moment I am using Settings API, using form below.

<form method="post" action="options.php" enctype="multipart/form-data">
    <?php
        settings_fields("mw_options");
        do_settings_sections("mw_options");      
        submit_button(); 
    ?>          
</form>

It works great, I have about 10 fields in the form, one of them is file upload that adds attachments to a Woocommerce e-mails. Everything works great, but I would like to add Delete button next to the Upload button in my form, so I can use updated_option or delete_option hook, or something more suited.

I can not figure out, how to do that.

Now I use my own delete_attachment.php and formaction on the button to get the job done, but I would like to do more of this later and I feel there is a better way.

function mw_options_wc_order_attachement_1()
{
    ?>
        <input type="file" name="mw_options_wc_order_attachement_1" id="mw_options_wc_order_attachement_1" value="<?php echo get_option('mw_options_wc_order_attachement_1'); ?>" />
        <?php echo get_option("mw_options_wc_order_attachement_1"); ?>
        <input type="submit" name="mw_options_wc_order_attachement_1" formaction="<?php echo plugins_url( 'delete_attachment.php' , __FILE__ );?>" formmethod="post" value="Delete">
    <?php
}

How can I create another button within the main option's area that would execute some delete script I have?

Share Improve this question asked Dec 17, 2017 at 10:01 MikeMike 12110 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 1

I managed to add delete button this way, it also may be used to add any extra submit button.

Create a submit button with name and array attribute:

function mw_options_wc_order_attachement_1()
{
    ?>
        <input type="file" name="mw_options_wc_order_attachement_1" id="mw_options_wc_order_attachement_1" value="<?php echo get_option('mw_options_wc_order_attachement_1'); ?>" />
        <?php echo get_option("mw_options_wc_order_attachement_1"); ?>
        <input type="submit" name="submit[delete_attachment_1]" class="button button-primary" value="Delete" />
    <?php
}

Register setting with callback function, mw_options_wc_order_attachement_1_handle is the name of the function in my case.

register_setting("mw_options", "mw_options_wc_order_attachement_1", "mw_options_wc_order_attachement_1_handle");

Make the callback function something like this:

function mw_options_wc_order_attachement_1_handle($options)
{

    if (isset($_POST["submit"]))
    {
        $submit_button = $_POST["submit"];

        if (isset($submit_button["delete_attachment_1"])) 
        {
            // delete file
            wp_delete_file('YOUR-FILE-SERVER-PATH');
            return ''; // returns empty option value to settings
        }
    }

 }
Post a comment

comment list (0)

  1. No comments so far