How can I add a simple custom field to my plugin?

admin2025-06-02  1

I have a custom plugin that I'm trying to edit. I didn't develop the plugin. I just need to add a textarea to it and save the contents. I have the field added, but I can't get it to save. This simply needs to be on the backend in the admin. This field will not be visible anywhere but this page.

Here's my code:

<form method="post">
    <table class="widefat">
        <thead>
            <tr>
                <th style="width: 15%;">CHEF Account Info</th>
                <th></th>
            </tr>
        </thead>
        <tbody>
            <tr>
                <th><label><?php echo _e( 'Status', 'chef' ); ?></label></th>
                <td>
                    <input type="text" readonly="" onfocus="this.select()" value="<?php echo $chef_config->get_chef_status_title( $user->chef_status ); ?>" class="regular-text">
                    <?php if ( $user->chef_status == 'rejected' ): ?>
                        <br><br><textarea name="chef_status_rejection_notes" readonly="" onfocus="this.select()" class="um-forms-field um-long-field" rows="6"><?php echo $user->chef_status_rejection_notes; ?></textarea>
                    <?php endif; ?>
                </td>
            </tr>
            <tr>
                <th><label><?php echo _e( 'User Role', 'chef' ); ?></label></th>
                <td><input type="text" readonly="" onfocus="this.select()" value="<?php echo $chef_config->get_chef_role_title( $user->roles[0] ); ?>" class="regular-text"></td>
            </tr>
            <tr>
                <th><label><?php echo _e( 'Experience Level', 'chef' ); ?></label></th>
                <td><input type="text" readonly="" onfocus="this.select()" value="<?php echo $user->chef_experience_level; ?>" class="regular-text"></td>
            </tr>
            <tr>
                <th><label><?php echo _e( 'Registered', 'chef' ); ?></label></th>
                <td><input type="text" readonly="" onfocus="this.select()" value="<?php echo date_format( date_create( $user->user_registered ), "F j\, Y @ h:i:s A" ); ?>" class="regular-text"></td>
            </tr>
            <tr>
                <th><label><?php echo _e( 'Resources Access', 'chef' ); ?></label></th>
                <td>
                    <?php
                        $terms = get_terms('resource_category');
                        $chef_profile_resources_access = $user->chef_profile_resources_access;
                        if ( empty( $chef_profile_resources_access ) ) {
                            $chef_profile_resources_access = [];
                        }
                    ?>
                    <?php $terms = get_terms('resource_category'); ?>
                    <select name="chef_profile_resources_access[]" multiple>
                        <?php foreach ( $terms as $term ): ?>
                            <option value="<?php echo $term->term_id; ?>" <?php if ( in_array( $term->term_id, $chef_profile_resources_access ) ): echo 'selected'; endif; ?>><?php echo $term->name; ?></option>
                        <?php endforeach; ?>
                    </select>
                </td>
            </tr>
            <tr>
                <th><label><?php echo _e( 'Reporting Form', 'chef' ); ?></label></th>
                <td>
                    <?php
                        $form_pages = get_pages( ['parent' => 1520 ] );
                        $chef_profile_reporting_form = $user->chef_profile_reporting_form;
                        if ( empty( $chef_profile_reporting_form ) ) {
                            $chef_profile_reporting_form = '';
                        }
                    ?>
                    <select name="chef_profile_reporting_form">
                        <option value="">None</option>
                        <?php foreach ( $form_pages as $page ): ?>
                            <option value="<?php echo $page->ID; ?>" <?php if ( $page->ID == $chef_profile_reporting_form ): echo 'selected'; endif; ?>><?php echo $page->post_title; ?></option>
                        <?php endforeach; ?>
                    </select>
                </td>
            </tr>

            <tr>
                <th><label><?php echo _e( 'CHEF Notes', 'chef' ); ?></label></th>
                <td><textarea id="chef_profile_notes" rows="5" onfocus="this.select()" value="<?php echo $user->chef_profile_notes; ?>" class="regular-text" /> </textarea></td>
            </tr>

        </tbody>
    </table>
    <div class="user-approve-reject-actions">
        <input type="hidden" name="action" value="user_permissions" />
        <button class="button button-primary">Save changes</button>
    </div>
</form>

My textarea is at the bottom. What do I need to do to get it to save?

I have a custom plugin that I'm trying to edit. I didn't develop the plugin. I just need to add a textarea to it and save the contents. I have the field added, but I can't get it to save. This simply needs to be on the backend in the admin. This field will not be visible anywhere but this page.

Here's my code:

<form method="post">
    <table class="widefat">
        <thead>
            <tr>
                <th style="width: 15%;">CHEF Account Info</th>
                <th></th>
            </tr>
        </thead>
        <tbody>
            <tr>
                <th><label><?php echo _e( 'Status', 'chef' ); ?></label></th>
                <td>
                    <input type="text" readonly="" onfocus="this.select()" value="<?php echo $chef_config->get_chef_status_title( $user->chef_status ); ?>" class="regular-text">
                    <?php if ( $user->chef_status == 'rejected' ): ?>
                        <br><br><textarea name="chef_status_rejection_notes" readonly="" onfocus="this.select()" class="um-forms-field um-long-field" rows="6"><?php echo $user->chef_status_rejection_notes; ?></textarea>
                    <?php endif; ?>
                </td>
            </tr>
            <tr>
                <th><label><?php echo _e( 'User Role', 'chef' ); ?></label></th>
                <td><input type="text" readonly="" onfocus="this.select()" value="<?php echo $chef_config->get_chef_role_title( $user->roles[0] ); ?>" class="regular-text"></td>
            </tr>
            <tr>
                <th><label><?php echo _e( 'Experience Level', 'chef' ); ?></label></th>
                <td><input type="text" readonly="" onfocus="this.select()" value="<?php echo $user->chef_experience_level; ?>" class="regular-text"></td>
            </tr>
            <tr>
                <th><label><?php echo _e( 'Registered', 'chef' ); ?></label></th>
                <td><input type="text" readonly="" onfocus="this.select()" value="<?php echo date_format( date_create( $user->user_registered ), "F j\, Y @ h:i:s A" ); ?>" class="regular-text"></td>
            </tr>
            <tr>
                <th><label><?php echo _e( 'Resources Access', 'chef' ); ?></label></th>
                <td>
                    <?php
                        $terms = get_terms('resource_category');
                        $chef_profile_resources_access = $user->chef_profile_resources_access;
                        if ( empty( $chef_profile_resources_access ) ) {
                            $chef_profile_resources_access = [];
                        }
                    ?>
                    <?php $terms = get_terms('resource_category'); ?>
                    <select name="chef_profile_resources_access[]" multiple>
                        <?php foreach ( $terms as $term ): ?>
                            <option value="<?php echo $term->term_id; ?>" <?php if ( in_array( $term->term_id, $chef_profile_resources_access ) ): echo 'selected'; endif; ?>><?php echo $term->name; ?></option>
                        <?php endforeach; ?>
                    </select>
                </td>
            </tr>
            <tr>
                <th><label><?php echo _e( 'Reporting Form', 'chef' ); ?></label></th>
                <td>
                    <?php
                        $form_pages = get_pages( ['parent' => 1520 ] );
                        $chef_profile_reporting_form = $user->chef_profile_reporting_form;
                        if ( empty( $chef_profile_reporting_form ) ) {
                            $chef_profile_reporting_form = '';
                        }
                    ?>
                    <select name="chef_profile_reporting_form">
                        <option value="">None</option>
                        <?php foreach ( $form_pages as $page ): ?>
                            <option value="<?php echo $page->ID; ?>" <?php if ( $page->ID == $chef_profile_reporting_form ): echo 'selected'; endif; ?>><?php echo $page->post_title; ?></option>
                        <?php endforeach; ?>
                    </select>
                </td>
            </tr>

            <tr>
                <th><label><?php echo _e( 'CHEF Notes', 'chef' ); ?></label></th>
                <td><textarea id="chef_profile_notes" rows="5" onfocus="this.select()" value="<?php echo $user->chef_profile_notes; ?>" class="regular-text" /> </textarea></td>
            </tr>

        </tbody>
    </table>
    <div class="user-approve-reject-actions">
        <input type="hidden" name="action" value="user_permissions" />
        <button class="button button-primary">Save changes</button>
    </div>
</form>

My textarea is at the bottom. What do I need to do to get it to save?

Share Improve this question asked Mar 10, 2019 at 22:23 Lz430Lz430 1378 bronze badges 6
  • Set a name for your textarea - <textarea name="chef_profile_notes"...>. – Sally CJ Commented Mar 10, 2019 at 22:29
  • Unfortunately that didn't work. Every other field in that form works. This is a brand new field...am I missing a hook or action somewhere? – Lz430 Commented Mar 10, 2019 at 22:43
  • The textarea should also be in this format: <textarea name="chef_profile_notes" ...><?php echo esc_textarea( $user->chef_profile_notes ): ?></textarea> – Sally CJ Commented Mar 10, 2019 at 22:45
  • That did it! Thank you! – Lz430 Commented Mar 10, 2019 at 22:57
  • You're welcome, but I have written a proper answer and I hope it helps you more. – Sally CJ Commented Mar 10, 2019 at 23:20
 |  Show 1 more comment

2 Answers 2

Reset to default 2

Your textarea needs to have a proper name in order for the browser to send it to the server for processing such as be saved to a database.

Secondly, textarea fields do not have a value and they also need a closing tag </textarea> because textarea is a multi-line form field.

So the proper format is:

<textarea name="chef_profile_notes" id="chef_profile_notes" ...other attributes...>
  <?php echo esc_textarea( $user->chef_profile_notes ): ?><!-- the field value -->
</textarea>

And you can see I also use esc_textarea() which is to secure the output sent/displayed to the user. See Data Validation for more details.

This plugin use ajax to save data, ajax call the user_permissions function that is already defined in the plugin classes, if you can share this function, may you found how data is structured and saved and you could add yours.

转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1748821432a314006.html

最新回复(0)