0

I try to display a piece of text (without plugin) for example "Quote of the day: Some text" on all user profile pages (.../wp-admin/profile.php).

This Text should managable/editable from the Profile of the ADMIN. In the Admins Profile Page should be a Text Input field where i can change the Text and save.

Since yesterday i was not able to find any hint to achive this. I found a lot of tutorials to create custom User Field for the profile Page - but nothing like i need.

So far i was able to create a text input field where i can save the text (input) in the database and display it only in the admins profile page with:

function add_qoute_of_the_day_backend( $user ) { 
// Visible only for admins
if( current_user_can( 'administrator' ) ) { 
?>
<div class="visible-only-for-admin">
    <h3>Quote of the Day Input Field</h3>
    <table class="form-table" >
        <tr>
            <th><label for="qoute_of_the_day">Qoute of the Day</label></th>
            <td><input type="text" name="qoute_of_the_day" value="<?php echo esc_attr(get_the_author_meta( 'qoute_of_the_day', $user->ID )); ?>" class="regular-text" /></td>
        </tr>
    </table>
</div>
<?php } ?>
<?php }
add_action( 'show_user_profile', 'add_qoute_of_the_day_backend', 10 );
add_action( 'edit_user_profile', 'add_qoute_of_the_day_backend', 10 );

function save_qoute_of_the_day_backend( $user_id ) {
    if ( !current_user_can( 'edit_user', $user_id ) )
    return false;       
    update_user_meta( $user_id,'qoute_of_the_day', sanitize_text_field ( $_POST['qoute_of_the_day'] ) );
}
add_action( 'personal_options_update', 'save_qoute_of_the_day_backend' );
add_action( 'edit_user_profile_update', 'save_qoute_of_the_day_backend' );

The code above works, but how can i display the input of the admin, in Text Form (just: "Quote of the day: Some Text") in all other user profiles? Maybe any way to create a simple input field just for the admin Role and display the input in all profiles?

Visual - what im trying to achive:

INPUT FIELD in the profile page of the admin:

enter image description here

DISPLAY Text in all other user profiles:

enter image description here

Any help would be greatly appreciated

1

You just need to move the capability check "inwards":

function wpse_230369_quote_of_the_day( $user ) { 
    $quote = esc_attr( get_option( 'quote_of_the_day' ) );
    ?>

<div class="visible-only-for-admin">
    <h3>Quote of the Day Input Field</h3>
    <table class="form-table" >
        <tr>
            <th><label for="quote_of_the_day">Quote of the Day</label></th>
            <td>
                <?php if ( current_user_can( 'administrator' ) ) : ?>

                    <input type="text" name="quote_of_the_day" value="<?php echo $quote ?>" class="regular-text" />

                <?php else : ?>

                    <?php echo $quote ?>

                <?php endif ?>
            </td>
        </tr>
    </table>
</div>

<?php
}

add_action( 'show_user_profile', 'wpse_230369_quote_of_the_day', 10 );
add_action( 'edit_user_profile', 'wpse_230369_quote_of_the_day', 10 );

function wpse_230369_save_quote_of_the_day( $user_id ) {
    if ( isset( $_POST['quote_of_the_day'] ) && current_user_can( 'administrator' ) )
        update_option( 'quote_of_the_day', sanitize_text_field( wp_unslash( $_POST['quote_of_the_day'] ) ) );
}

add_action( 'edit_user_profile_update', 'wpse_230369_save_quote_of_the_day' );
add_action( 'personal_options_update',  'wpse_230369_save_quote_of_the_day' );
  • Hi, it does not echo the $quote in the users profiles...? The Admin part looks fine. – NewUser Jun 21 '16 at 17:48
  • Are you sure $quote has a value. – TheDeadMedic Jun 21 '16 at 19:23
  • Yes - echo $quote returns the input - BUT only in the Profile Page of the admin! (i chceked it) In the Profiles of the other users it does NOT work. Any idea? thx – NewUser Jun 21 '16 at 20:40
  • Because the quote is stored as a per-user setting? – TheDeadMedic Jun 21 '16 at 20:47
  • Look at the code above function save_qoute_of_the_day_backend( $user_id ) {... I have no idea how to fix this. Any help would be greatly appreciated – NewUser Jun 21 '16 at 21:40

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.