Support » Developing with WordPress » How to create plugin for storing 3 different values?

  • Hello.

    Two days I’ve been trying to create a plugin with the only function to manually add and store 3 different values. I want to be able to display these 3 values stored in the plugin on the front-page.

    Could someone explain to me how to do this step by step like I was 5?

    Thank you

Viewing 5 replies - 1 through 5 (of 5 total)
  • Why develop a plugin just to store three values?
    Save these values to the options database table using add_option().
    Also, you can retrieve these values and print them on the frontend.

    To Add Value:

    $first = "something";
    add_option( 'first_value', $first );

    To Get Value:
    $first = get_option( 'first_value' );

    Read more about add_option() and get_option().

    Because I need to be able to quickly change the values by using the dashboard

    Moderator bcworkz

    (@bcworkz)

    That does not invalidate Nikhil’s suggestion, which is a good one. Additionally, you need a UI element with which to set these options. To add this to the actual dashboard is a bit involved. You need to create a dashboard widget. The At a Glance widget does have some action hooks where you could add additional content. You still need to handle any changes to the added fields in order to save the changes.

    What would be easier to accomplish is to use the Settings API to add additional settings somewhere. The API then manages saving the values in options table. You just need to get the saved options from the front page template.

    Thank you @bcworkz and @nikhilbhansi

    I currently have this code:

    add_action('admin_menu', 'name_create_menu');
    
    function name_create_menu() {
    
    	add_menu_page('Values Settings', 'Values', 'administrator', __FILE__, 'name_settings_page' , 'dashicons-plugins-checked' );
    
    	add_action( 'admin_init', 'register_name_settings' );
    }
    
    function register_name_settings() {
    
    	register_setting( 'name-settings-group', 'First Value' );
    	register_setting( 'name-settings-group', 'Second Value' );
    	register_setting( 'name-settings-group', 'Third Value' );
    }
    
    function name_settings_page() {
    ?>
    <div class="wrap">
    <h1>Values</h1>
    
    <form method="post" action="options.php">
        <?php settings_fields( 'name-settings-group' ); ?>
        <?php do_settings_sections( 'name-settings-group' ); ?>
        <table class="form-table">
            <tr valign="top">
            <th scope="row">First Value</th>
            <td><input type="text" name="f_value" value="<?php echo esc_attr( get_option('f_value') ); ?>" /></td>
            </tr>
             
            <tr valign="top">
            <th scope="row">Second Value</th>
            <td><input type="text" name="s_value" value="<?php echo esc_attr( get_option('s_value') ); ?>" /></td>
            </tr>
            
            <tr valign="top">
            <th scope="row">Third Value</th>
            <td><input type="text" name="t_value" value="<?php echo esc_attr( get_option('t_value') ); ?>" /></td>
            </tr>
        </table>
        
        <?php submit_button(); ?>
    
    </form>
    </div>
    <?php } ?>

    Do you want to help me add an option for selecting a picture? I want inside this “plugin” or what is the correct name for it to be able not only to add these three values but also select a picture. This picture will be used as a background on the page I display the three values or just displayed as a picture.

    Regards,
    Uzername

    Moderator bcworkz

    (@bcworkz)

    A picture selected through the media library? I’ve no experience with that aspect, but this SE answer should get you started:
    https://wordpress.stackexchange.com/questions/235406/how-do-i-select-an-image-from-media-library-in-my-plugin#answer-236296

Viewing 5 replies - 1 through 5 (of 5 total)
  • You must be logged in to reply to this topic.