Advertisement
GochiSiyan

author & subscriber

Feb 14th, 2021
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.54 KB | None | 0 0
  1. add_action('jnews_after_account_nav',function (){
  2. $user = wp_get_current_user(get_current_user_id());
  3. if (user_can( $user, "subscriber" )){
  4. ?>
  5. <div class="frontend-submit-button" id="change-to-author">
  6. <a class="button" href="#"><i class="fa fa-file-text-o"></i>Switch to Author</a>
  7. </div>
  8. <script>
  9. (function ($){
  10. element = document.getElementById('change-to-author')
  11. element.addEventListener('click',function (e){
  12. e.preventDefault()
  13. $.ajax({
  14. url:jnews_ajax_url,
  15. method:'post',
  16. dataType: 'json',
  17. data: {
  18. action:'subscribe_to_author',
  19. user:<?=$user->ID?>,
  20. },
  21. }).success(function (data){
  22. console.log('success')
  23. window.location.reload()
  24. })
  25. })
  26. })(jQuery)
  27. </script>
  28. <?php
  29. } elseif (user_can( $user, "author" )) {
  30. ?>
  31. <div class="frontend-submit-button" id="change-to-author">
  32. <a class="button" href="#"><i class="fa fa-file-text-o"></i>Subscriber</a>
  33. </div>
  34. <script>
  35. (function ($){
  36. element = document.getElementById('change-to-author')
  37. element.addEventListener('click',function (e){
  38. e.preventDefault()
  39. $.ajax({
  40. url:jnews_ajax_url,
  41. method:'post',
  42. dataType: 'json',
  43. data: {
  44. action:'author_to_subscriber',
  45. user:<?=$user->ID?>,
  46. },
  47. }).success(function (data){
  48. console.log('success')
  49. window.location.reload()
  50. })
  51. })
  52. })(jQuery)
  53. </script>
  54. <?php
  55. }
  56. });
  57.  
  58. add_action('jnews_ajax_subscribe_to_author',function (){
  59. $user = wp_get_current_user($_REQUEST['user']);
  60. $user->remove_role('subscriber');
  61. $user->add_role('author');
  62. wp_send_json('');
  63. });
  64.  
  65. add_action('jnews_ajax_author_to_subscriber',function (){
  66. $user = wp_get_current_user($_REQUEST['user']);
  67. $user->remove_role('author');
  68. $user->add_role('subscriber');
  69. wp_send_json('');
  70. });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement