简体   繁体   中英

How to add and remove a value in a dropdown list within settings of a wordpress plugin

I am creating my first plugin. It creates two custom columns for a woocomerce product. The first column takes values from a textfield and the second from a dropdown list. The first has SKUs the second has Suppliers.

I then created a settings page for my plugin. I would like to be able to add a new supplier and be able to remove it. Is it possible?

This is the part of the settings:

add_action( 'admin_menu', 'wpcfqe_add_admin_menu' );
add_action( 'admin_init', 'wpcfqe_settings_init' );


function wpcfqe_add_admin_menu(  ) { 

add_options_page( 'Custom Fields and Quick Edit', 'Custom Fields and Quick Edit', 'manage_options', 'custom_fields_and_quick_edit', 'wpcfqe_options_page' );

}


function wpcfqe_settings_init(  ) { 

register_setting( 'pluginPage', 'wpcfqe_settings' );

add_settings_section(
    'wpcfqe_pluginPage_section', 
    __( 'Add new Supplier', 'wordpress' ), 
    'wpcfqe_settings_section_callback', 
    'pluginPage'
);

add_settings_field( 
    'wpcfqe_text_field_0', 
    __( 'Supplier', 'wordpress' ), 
    'wpcfqe_text_field_0_render', 
    'pluginPage', 
    'wpcfqe_pluginPage_section' 
);


}


function wpcfqe_text_field_0_render(  ) { 

$options = get_option( 'wpcfqe_settings' );
?>
<input type='text' name='wpcfqe_settings[wpcfqe_text_field_0]' value='<?php echo $options['wpcfqe_text_field_0']; ?>'>
<?php

}


function wpcfqe_settings_section_callback(  ) { 

echo __( 'Settings for Supplier', 'wordpress' );

}


function wpcfqe_options_page(  ) { 

?>
<form action='options.php' method='post'>

    <h2>Custom Fields and Quick Edit</h2>

    <?php
    settings_fields( 'pluginPage' );
    do_settings_sections( 'pluginPage' );
    submit_button();
    ?>

</form>
<?php

}

How to save new values to the supplier dropdown list

You can modify any wordpress data using $wpdb class. It allows you to create custom WP database queries with which you can modify data in db. It is very useful when it comes to creating a plugin and you should get familiar with it.

https://codex.wordpress.org/Class_Reference/wpdb#

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM