'How to assign a fixed value to a custom field

hope someone can help. I found the following code on the web and it works well to create a new custom field which I can input from the WP 'users' tab. Yet I need to give this field manually a value in php instead trough an input form. Thanks!

function custom_user_profile_fields($user){
    if(is_object($user))
        $company = esc_attr( get_the_author_meta( 'company', $user->ID ) );
    else
        $company = null;
    ?>
    <h3>Extra profile information</h3>
    <table class="form-table">
        <tr>
            <th><label for="company">Company Name</label></th>
            <td>
                <input type="text" class="regular-text" name="company" value="<?php echo $company; ?>" id="company" /><br />
                <span class="description">Where are you?</span>
            </td>
        </tr>
    </table>
<?php
}
add_action( 'show_user_profile', 'custom_user_profile_fields' );
add_action( 'edit_user_profile', 'custom_user_profile_fields' );
add_action( "user_new_form", "custom_user_profile_fields" );




function save_custom_user_profile_fields($user_id){
    # again do this only if you can
    if(!current_user_can('manage_options'))
        return false;
 
    # save my custom field
    update_user_meta($user_id, 'company', $_POST['company']);
}
add_action('user_register', 'save_custom_user_profile_fields');
add_action('profile_update', 'save_custom_user_profile_fields');


Solution 1:[1]

If you are looking to save a fixed value to each company field to the usermeta table in the WordPress database, you can modify your saving function, to provide a fixed value instead of the POST field, you are assigning in your code snippet right now.

update_user_meta(
    user_id: $user_id, 
    meta_key: 'company', 
    meta_value: 'your-custom-fiexed-value-here', // <--- here you go
);

(using named parameters requires PHP ^8.0.0)

WordPress docs: https://developer.wordpress.org/reference/functions/update_user_meta/

Also, note that there is a special Stack Exchange for WordPress Q&A: https://wordpress.stackexchange.com/

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 David Wolf