Get current user’s display name upon button click
-
I am brand new to website building with little to no coding experience. I have a page I am building that uses a custom post type and template. I need help with a piece I can’t seem to accomplish and I have exhausted other resources. The goal is that when a user clicks a button their display name populates a table field. A couple of nuances: 1) the theme I am using seems to require me to use html files for the display page. Originally, I had this in a php file and could get the function to work with the following (this is embedded <script>):
// Function to get the display name of the current user function getCurrentUserDisplayName() { // Replace this code with your own implementation to retrieve the display name of the current user // You can use WordPress functions or any other method specific to your setup return "<?php echo do_shortcode('[current_user_display_name]'); ?>"; }However, when I converted this to html the php obviously started just rendering the code rather than the actual display name.
Since, I have tried leveraging a number of different methods but have not gotten the result I need. Currently, I am here since the internet guided me down trying this path. In the functions.php file I have the following function and registering the endpoint to the rest api:
// Register REST API route add_action('rest_api_init', function () { register_rest_route('mycustomapp/v1', '/current-user/', array( 'methods' => 'GET', 'callback' => 'display_current_user_display_name', )); }); // Function to return the current user's display name function display_current_user_display_name() { $current_user = wp_get_current_user(); $display_name = $current_user->display_name; // Wrap the display name in an array or object before returning return rest_ensure_response(array('display_name' => $display_name)); }Additionally, in the html within the embedded <script> I have the following:
// Function to retrieve the current user's display name using AJAX async function getCurrentUserDisplayName() { try { const response = await fetch('/wp-json/mycustomapp/v1/current-user/'); const data = await response.json(); return data.display_name || 'Guest'; // Default value if the user is not logged in } catch (error) { console.error('Error fetching current user display name:', error); return 'Guest'; // Default value if there's an error } } // Function to update the owner's column in the positional tables dynamically async function updateOwnerColumn() { const ownerCells = document.querySelectorAll('.owner-cell'); for (const ownerCell of ownerCells) { try { const displayName = await getCurrentUserDisplayName(); ownerCell.textContent = displayName; } catch (error) { console.error('Error updating owner column:', error); ownerCell.textContent = 'Guest'; // Default value if there's an error } } }And the the associated listeners and calls to call the display name when the buttons are pressed. However, this does not work in calling the logged in user. Again, I am brand new and may be missing something obvious or dumb. Someone please help it’s driving me crazy I can’t unwind this.
Other context if helpful – my theme is a Buildapp theme, I am using the Ultimate Member plugin to assist with user registration etc. anything else needed just ask. Thanks in advance.
-
Hi there,
I do not see any sites under your WordPress.com account when I check on our end. You can confirm by visiting this page: https://wordpress.com/sites.
Is it possible you created your site under a different login instead? What is the URL of the site you are needing help with?
Happy to check in our system so we can take a closer look.
- The topic ‘Get current user’s display name upon button click’ is closed to new replies.