Home > OS >  Change placeholder text input form
Change placeholder text input form

Time:01-07

I am trying to change the placeholder text of my input for the searchform.

    <input type="search"  placeholder="<?php echo esc_attr_x( 'Waar ben je naar op zoek?', 'placeholder', 'fancy-lab' ); ?>" value="<?php echo get_search_query(); ?>" name="s" />

The string 'Waar ben je naar op zoek?' needs to be changed to something more personal when the user is logged in. We already are adding these changes to other parts of the site and I am using this code for other parts:

<?php global $current_user; wp_get_current_user(); ?>
    <?php if ( is_user_logged_in() ) { 
        echo $current_user->user_login; 
    }
    ?>

When I've added this I am getting multiple errors, the first one is due to the global part. So when changing the code to this:

<input type="search"  placeholder="<?php echo esc_attr_x( global $current_user; wp_get_current_user();
                                if ( is_user_logged_in() ) { 
                                    echo $current_user->user_login; 
                                }
                                , 'placeholder', 'fancy-lab' ); ?>" value="<?php echo get_search_query(); ?>" name="s" />

I am getting the error: syntax error, unexpected 'global' (T_GLOBAL)

Can anyone help me? Trying to understand the syntax correctly!

CodePudding user response:

In you functions.php create 1 method which will return logged user username or string and use this in the placeholder:

function searchFormPlaceholder(){
    if (is_user_logged_in()) {
        $current_user = wp_get_current_user();
        echo $current_user->display_name;
    } else {
        echo 'Not logged';
    }
}

And then inside placeholder tag you call your function:

<input type="search"  placeholder="<?php searchFormPlaceholder(); ?>" value="<?php echo get_search_query(); ?>" name="s" />
  •  Tags:  
  • Related