• I have a custom plugin to to display some author’ info in the posts, in this plugin I did a duplicate of a function in order to display some more content, different from the author description, so I tryed with

    // Get author's biographical information or description
    $user_description = get_the_author_meta( 'user_description', $post->post_author );
    
    // Get agency period of rentals
    $periodo_affitti = get_the_author_meta( 'periodo_affitti', $post->post_author );
    

    $periodo_affitti is clearly wrong, but I cant use $user_description because if I do the second content overwrites the first one

    Here is the custom extra field tied to the second function:

    th><label for="periodo_affitti">Periodo affitti</label></th>
                <td>
                   <textarea id="periodo_affitti" name="periodo_affitti"rows="5" cols="30" class="regular-text" ><?php echo esc_attr( get_the_author_meta( 'periodo_affitti', $user->ID ) ); ?></textarea><br />
                    <span class="periodo_affitti">periodo affitti</span>
    

    So which $…?

    Another problem is that I’d like to print those 2 infos in 2 different positions of the posts, how can I manage the position/priority of each post content?

    The page I need help with: [log in to see the link]

Viewing 6 replies - 16 through 21 (of 21 total)
  • Thread Starter sacconi

    (@sacconi)

    Now I can output the option 1 and the option 2 values, but here too I have the same problem than for custom fields in post editor with selector, I suppose I must change something here

    update_user_meta( $user_id, 'saldo', sanitize_text_field( $_POST['saldo'] ) );

    Actually it is not a text field I have to update and sanitize but the value picked from a selector

    <tr>
                <th><label for="saldo">Saldo</label></th>
                <td>
                    <select name="saldo" id="saldo">
    <option value="option0" <?php selected( get_the_author_meta( 'saldo', $user->ID ), 'option0' ); ?>>-</option>
                        <option value="15 gg. prima dell'arrivo" <?php selected( get_the_author_meta( 'saldo', $user->ID ), 'option1' ); ?>>15 gg. prima dell'arrivo</option>
                        <option value="All'arrivo" <?php selected( get_the_author_meta( 'saldo', $user->ID ), 'option2' ); ?>>All'arrivo</option>
                        <!-- Add more options as needed -->
                    </select>
                    <br />
                    <span class="description">Seleziona le condizioni di pagamento per il saldo</span>
                </td>
            </tr>
    

    So how could I fix

    Moderator bcworkz

    (@bcworkz)

    Sorry about the Get saldo code, I didn’t format my reply correctly and the forum’s parser altered the quotes, causing a syntax error. It has now been fixed. To repeat:

    // Get saldo
    $saldo = '<div class="saldo">'. get_the_author_meta( 'saldo', $post->post_author ) .'</div>';

    It’s best to output the elements from PHP in the order you want them. Assuming your HTML is well structured, and if you use the Flex Box CSS model, it is possible to re-order elements. If you want the “saldo” div block to occur before the footer element, the line before return could be:
    $content = $saldo .'<footer class="author_bio_section" >'. $periodo_affitti . $author_details .'</footer>';

    You’ll likely need more styling rules to get the saldo div element to look the way you like.

    While the option selection is what is being saved, the passed option values are textual values. Since this data comes from a browser, a bad actor could pass malicious code in place of the usual saldo selection. For safety, we must always assume any data passed from a browser could be malicious. Thus any input values must always be sanitized and validated before attempting to save it in a DB. Even input from a drop down select field.

    Thread Starter sacconi

    (@sacconi)

    I tryed with:

    $content = $saldo .'<footer class="author_bio_section" >'. $author_details . $titolo_affitti . $periodo_affitti  .'</footer>';

    but I got this result: https://ibb.co/QkpBMYX

    The description and the photogallery of the apartment have disappeared…

    Moderator bcworkz

    (@bcworkz)

    You’re still using this as a filter. I thought it was going to be used as a shortcode instead? For use as a filter you need to do it like this:
    $content = $content . $saldo .'<footer class="author_bio_section" >'. $author_details . $titolo_affitti . $periodo_affitti .'</footer>';

    Thread Starter sacconi

    (@sacconi)

    It works! And if I wanted to print a series of meta key for author one for each line can I write

    // Get saldo
    $saldo = '<div class="saldo">'. get_the_author_meta( 'saldo', 'meta_key_2', 'meta_key_3', 'meta_key_4', $post->post_author ) .'</div>'

    or

    // Get saldo
    $saldo = '<div class="saldo">'. get_the_author_meta( 'saldo', $post->post_author ) . get_the_author_meta( 'meta_key_1', $post->post_author )'.get_the_author_meta( 'meta_key_2', $post->post_author )'.get_the_author_meta( 'meta_key_3', $post->post_author )'.</div>';
    Moderator bcworkz

    (@bcworkz)

    You cannot request multiple meta values with
    get_the_author_meta( 'saldo', 'meta_key_2', 'meta_key_3', 'meta_key_4', $post->post_author ).
    You have to request one at a time with that function. If you use get_user_meta(), you can only pass the user ID without specifying a particular key and the function will return all meta data for the user as an array of arrays. You can then pluck out specific values as needed.

    $all_meta = get_user_meta( $post->post_author );
    $content = $content . '<div class="saldo">'. $all_meta['saldo'][0] .'</div><footer class="author_bio_section" >'. $author_details . $titolo_affitti . $periodo_affitti .'</footer>';

    The [0] is because multiple values can be saved under one key. It’s normally not done. The [0] returns the first and usually the only value for that key.

Viewing 6 replies - 16 through 21 (of 21 total)
  • The topic ‘which is che correct $ ?’ is closed to new replies.