Skip to content
Read For Learn
Read For Learn
  • Database
    • Oracle
    • SQL
  • C
  • C++
  • Java
  • Java Script
  • jQuery
  • PHP
Read For Learn
  • Database
    • Oracle
    • SQL
  • C
  • C++
  • Java
  • Java Script
  • jQuery
  • PHP

automated tests as a user?

$user_id = $this->_make_user('author', 'user_login_name', 'password', '[email protected]');
wp_set_current_user( $user_id );

as long as the class that you define the testcase in inherits from WPTestCase, then you can use the _make_user function. The function is found in this svn repository, and is defined in wp-testlib/base.php line 380.

NEW:

I switched to using wp_insert_user.

the following is in my setUp:

    $this->user_id = wp_insert_user(array('user_login'=>'test_administrator', 'role'=>'administrator'));
    wp_set_current_user( $this->user_id , '');

and the following is in my teardown:

    wp_delete_user($this->user_id);
    $this->user_id = 0;

Related Posts:

  1. Changing user_nicename
  2. User management system similar to wordpress one?
  3. Programmatic Login from 3rd Party site
  4. Change CSS based on is_user_logged_in
  5. Admin Panel 404 Error after login
  6. How to block specific user id in custom login form?
  7. Call WP Rest-Api to GET /users/me returned NOTHING in console
  8. is_user_logged_in() not working in homepage
  9. List users in a dropdown for login
  10. PHP unit testing WordPress?
  11. How to change the wp-login.php page title?
  12. How build a custom login/register form with error handling?
  13. Add extra field to users
  14. Integrating WordPress to my website, while keeping my own authentication system
  15. What is the most efficient way to search users by their display name?
  16. Network installation giving errors when viewing or adding a user
  17. Showing user ID on user main page from screen options
  18. Getting the Current User
  19. Activate different theme for temporary preview
  20. How do I hide specific user profile fields?
  21. WordPress 4 invalid username special charachters issue
  22. How to turn off redirection from ‘domain.com/login’ to ‘domain.com/wp-login.php’
  23. Authenticating to WordPress, using my own authentication two-factor system
  24. How to resolve error “Cookies are blocked due to unexpected output.”?
  25. How can I default to all WordPress roles when parameter is not included in shortcode?
  26. there’s a way to include a minimal WP for check only the current user, its roles (caps?) and then release/free it?
  27. Remove username in emails or swap username for email
  28. Delete option value from array using update_option()
  29. woocommerce and is_user_logged_in() if not redirect to homepage
  30. get_users(…) only returns one user
  31. Check if user is logged in when clicking certain links on certain pages
  32. List User order by ID in Descending order (Backend)
  33. Get all user meta by meta key instead of ID
  34. How to get a list of all possible values of a specific user meta key?
  35. Is it necessary to sanitize wp_set_password user input?
  36. How to give new users two specific user role options upon WordPress user registration
  37. Wp-login appears White Screen, Error: Cannot modify header information
  38. Login/logout in header
  39. Custom user login page by creating a plugin
  40. Cannot access wp-admin/wp-login.php (WordPress backend) anymore, what could be wrong?
  41. How to debug my custom login form looping intermittently
  42. user_profile_update_errors hook not executing
  43. Get users that likes the post
  44. add_cap not working
  45. wordpress archive a user
  46. Login to wordpress by clicking a link and specifying usernaname and password in url
  47. Call to undefined function get_userdata() in plugin
  48. Log in / Log Out Custom Button
  49. How can I open up my administrative panel to everyone?
  50. How do you create two separate Register pages?
  51. How can I add a new row in a separate database when someone registers via WordPress?
  52. Lost in trying to create user database system
  53. Get the users that my following users are following
  54. create front-end users post list by specific category
  55. Shortcode to log user into current URL
  56. PHP getting error when trying to access WP-Admin Dashboard
  57. Change homepage content if user is logged in – BuddyPress
  58. php return username of currently viewed author profile
  59. Redirect to current user page upon link click
  60. Use of antispambot with $curauth->email
  61. Uses for function: wp_update_user
  62. Generate a QR code when creating a new WordPress user
  63. Change wordpress’ database data using ajax – how to get current user id?
  64. Allow a user or role to view drafts and previews, but not other admin privileges?
  65. Storing huge number of users in wordpress
  66. PHP warning – Use of undefined constant ‘FORCE_SSL_LOGIN’ ‘FORCE_SSL_ADMIN’ on wp-config.php
  67. Get current user id in function php
  68. how can I call a function when time expire
  69. Having trouble creating two shortcodes, one for logged in user and one for visitors
  70. Uploading/integrating custom user registration page
  71. Restrict wordpress access to logged users only
  72. Page template not working when logged out
  73. Display current user metadata on WordPress page
  74. Refresh page after login with litespeed cache
  75. Redirect after login depending on the URL
  76. Need help with AJAX login to call php in functions.php to handle redirects based on user cap (role)
  77. Add another role to a user when they click a button?
  78. wordpress more than one ajax request at the same time issue
  79. Can’t get_users info by using json_encode
  80. Check if a user is logged into my WordPress site which is on a different server
  81. Changing WordPress author name in database
  82. Newbie question. Login/Registration. New PHP page
  83. Loop 1 user randomly
  84. How to set max users to 17.000
  85. Not logged in when using http
  86. add bootstrap modal after login in wordpress
  87. How to ‘remember’ a site member’s last visited page?
  88. How to connect database table to each registered wordpress user.
  89. Display a list of users with avatar filterable with alphabets
  90. Redirect users not logged in to the standard login page (and back) from some posts and pages
  91. Parse error: syntax error, unexpected ‘}’ in C:\wamp64\www\Proiect\aplicatie\user_check.php on line 18 [closed]
  92. Check if username doesn’t exists
  93. WP query with variables gives no result for specific user
  94. How to create custom user role without plugin?
  95. wp_login_url always redirects me to wp-admin
  96. PHP Works only for logged in users: [closed]
  97. Custom Admin Menu Report for Specific User ID
  98. change div text and link for logged in users
  99. wp-login – unable to redirect user to a custom login url
  100. Duplicated WP Multisite for testing environment is redirecting to production site
Categories PHP Tags login, php, testing, users
WordPress with CAS+LDAP and standard WP accounts
Why load_textdomain work but not load_plugin_textdomain?

Recommended Hostings

Cloudways: Realize Your Website's Potential With Flexible & Affordable Hosting. 24/7/365 Support, Managed Security, Automated Backups, and 24/7 Real-time Monitoring.

FastComet: Fast SSD Hosting, Free Migration, Hack-Free Security, 24/7 Super Fast Support, 45 Day Money Back Guarantee.

Recent Added Topics

  • Bug in translation system: load_theme_textdomain() returns true, files are available and accessible but the language defaults to english
  • Custom Elementor controls not appearing in the widget Advanced tab using injection hooks
  • Get the name of the template/*html file used
  • Trying to Add Paging to Single Post Page
  • Sharing media files between live and staging servers
  • How to display the description of a custom post type in the dashboard?
  • Critical error on image display
  • Copying WP data and files into new install?
  • How to determine the DirectAdmin WordPress backup date?
  • How to get list of ALL tables in the database?
© 2026 Read For Learn
  • Database
    • Oracle
    • SQL
  • algorithm
  • asp.net
  • assembly
  • binary
  • c#
  • Git
  • hex
  • HTML
  • iOS
  • language angnostic
  • math
  • matlab
  • Tips & Trick
  • Tools
  • windows
  • C
  • C++
  • Java
  • javascript
  • Python
  • R
  • Java Script
  • jQuery
  • PHP
  • WordPress