• I have some profile extra fields. I edit them from wp-admin/user-edit.php?

    These data are displayed i.e. on all the posts of the same author. But how could I overwrite the same field from the position wp-admin/post-new.php ? Which is the purpose? Imagine that all the apartments (posts) managed from one agency (author) have the same cost for final cleaning, 50 euro. Ok, I can manage it from author/agency level, but if just one apartment has a different cost, i.e 80 euro, I should have the same field at apartment/post level, and this field , which is further downstream, overrides the one which is further upstream. An example of my profile extra fields:

    add_action( 'show_user_profile', 'my_show_extra_profile_fields' );
    add_action( 'edit_user_profile', 'my_show_extra_profile_fields' );
    add_action( "user_new_form",'my_show_extra_profile_fields' );
    function my_show_extra_profile_fields( $user ) { ?>
        <h3>Extra profile information</h3>
        <table class="form-table">
            <tr>
                <th><label for="richiesta">Richiesta</label></th>
                <td>
                   <textarea id="richiesta" name="richiesta"rows="5" cols="30" class="regular-text" ><?php echo esc_attr( get_the_author_meta( 'richiesta', $user->ID ) ); ?></textarea><br />
                    <span class="description">Richiesta del cliente.</span>
                </td>
                <th><label for="cell_two">N.Cellulare 2</label></th>
                <td>
                    <input type="text" name="cell_two" id="cell_two" value="<?php echo esc_attr( get_the_author_meta( 'cell_two', $user->ID ) ); ?>" class="regular-text" /><br />
                    <span class="description">Inserire il numero di cellulare n.2.</span>
                </td>
            </tr>
            <tr>
                <th><label for="cell">N.Cellulare</label></th>
                <td>
                    <input type="text" name="cell" id="cell" value="<?php echo esc_attr( get_the_author_meta( 'cell', $user->ID ) ); ?>" class="regular-text" /><br />
                    <span class="description">Inserire il numero di cellulare.</span>
                </td>
            </tr>
            <tr>
                <th><label for="conditions_of_payment">Conditions of Payment</label></th>
                <td>
                    <select name="conditions_of_payment" id="conditions_of_payment">
                        <option value="option1" <?php selected( get_the_author_meta( 'conditions_of_payment', $user->ID ), 'option1' ); ?>>Option 1</option>
                        <option value="option2" <?php selected( get_the_author_meta( 'conditions_of_payment', $user->ID ), 'option2' ); ?>>Option 2</option>
                        <!-- Add more options as needed -->
                    </select>
                    <br />
                    <span class="description">Select the conditions of payment for this agency.</span>
                </td>
            </tr>
        </table>
    <?php }
    
    // Save the extra fields
    add_action( 'personal_options_update', 'my_save_extra_profile_fields' );
    add_action( 'edit_user_profile_update', 'my_save_extra_profile_fields' );
    function my_save_extra_profile_fields( $user_id ) {
        if ( ! current_user_can( 'edit_user', $user_id ) ) {
            return false;
        }
        update_user_meta( $user_id, 'cell_two', sanitize_text_field( $_POST['cell_two'] ) );
        update_user_meta( $user_id, 'richiesta', sanitize_text_field( $_POST['richiesta'] ) );
        update_user_meta( $user_id, 'cell', sanitize_text_field( $_POST['cell'] ) );
        update_user_meta( $user_id, 'conditions_of_payment', sanitize_text_field( $_POST['conditions_of_payment'] ) );
    }
    

    I suppose I have to create 2 fields with the same Id or meta_key, but how can I tell the system which value must prevail?

    This is an example of a numeric custom field in my post editor

    // ACTION TO ADD A META BOX NUMBER
    
    add_action( 'add_meta_boxes_post', "number_add_meta_box" );
    
    /*
     * Routine to add a meta box.  Called via a filter hook once WordPress is initialized
     */
    function number_add_meta_box(){
        add_meta_box(
            "number_meta_box",  // An ID for the Meta Box.  Make it unique to your plugin
            __( "Distanza dal mare", 'textdomain' ),  // The title for your Meta Box
            "number_meta_box_render",  // A callback function to fill the Meta Box with the desired UI in the editor
            "post",  // The screen name - in this case the name of our custom post type
            "side"  // The context or placement for the meta box.  Choose "normal", "side" or "advanced"
        );
    }
    
    /*
     * Routine to display a custom meta box editor
     * Note: In this example our custom meta data is saved as a row in the postmeta database table.
     * $post PostObject An object representing a WordPress post that is currently being loaded in the post editor.
     */
    function number_meta_box_render( $post ){
    
        // Get the number and display it in a numeric field
        $number = get_post_meta( $post->ID, "function_number", true );
        echo '<div><input type="number" name="function_number" value="'.$number.'" placeholder="Distanza dal mare" /></div>';
     
    }
    
    // Hook into the save routine for posts
    add_action( 'save_post', 'number_meta_box_save');
    
    /*
     * Routine to save the custom post meta data from the editor
     * Note: We retrieve the form data through the PHP Global $_POST
     * $post_id int The ID of the post being saved
     */
    function number_meta_box_save( $post_id ){
         
        // Check to see if this is our custom post type (remove this check if applying the meta box globally)
        if($_POST['post_type'] == "post"){
            
            // Retrieve the values from the form
            $number = $_POST['function_number'];
            
    
            // Clean, sanitize and validate the input as appropriate
    
            // Save the updated data into the custom post meta database table
            update_post_meta( $post_id, "function_number", $number );
            
    
        }     
    }
    
Viewing 1 replies (of 1 total)
  • Moderator bcworkz

    (@bcworkz)

    usermeta and postmeta are different tables, so I think you can use the same key in each table (unverified). Even if possible, I would not advise it. I think it’s confusing. One should clearly be the default and the other the working value.

    In the post meta box, when populating the field’s value, first try to get the post’s meta value. If it does not exist or is empty, get the default value from user meta. If the author does not change the default, it’ll be saved as the de facto working value. If they do change the value, then that’ll be the one saved instead. Either way, all’s good on the front end when template code gets the saved working post meta value.

Viewing 1 replies (of 1 total)
  • The topic ‘overwrite the same field from another position’ is closed to new replies.