abdulkrm

Disable the Keyword Research option from the page/post editor side bar for users with Editor roles

Oct 6th, 2023
128
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.88 KB | None | 0 0
  1. function enqueue_custom_admin_script() {
  2. // Check if we are in the admin area and the current user is an editor but not an administrator
  3. if (is_admin() && current_user_can('editor') && !current_user_can('administrator')) {
  4. wp_enqueue_script('jquery');
  5. add_action('admin_footer', 'hide_elements_on_dom_change');
  6. }
  7. }
  8.  
  9. add_action('admin_enqueue_scripts', 'enqueue_custom_admin_script');
  10.  
  11. function hide_elements_on_dom_change() {
  12. echo '
  13. <script>
  14. jQuery(document).ready(function($) {
  15. // Function to hide the desired elements
  16. function hideElements() {
  17. $(".rank-math-ca-top-section, .rank-math-ca-keywords-wrapper").attr("style", "display: none !important;");
  18.  
  19. // Hide the button with the classes "rank-math-general-tab" and "is-active"
  20. $("button.rank-math-general-tab.is-active").attr("style", "display: none !important;");
  21. }
  22.  
  23. // Check if MutationObserver is supported by the browser
  24. if ("MutationObserver" in window) {
  25. // Observe changes in the entire document
  26. var observer = new MutationObserver(function(mutations) {
  27. mutations.forEach(function(mutation) {
  28. // If the added nodes include a node with the classes "rank-math-content-ai-wrapper" and "is-opened"
  29. if ($(mutation.addedNodes).hasClass("rank-math-content-ai-wrapper") && $(mutation.addedNodes).hasClass("is-opened")) {
  30. hideElements();
  31. }
  32. });
  33. });
  34.  
  35. // Start observing changes
  36. observer.observe(document.body, {
  37. childList: true,
  38. subtree: true
  39. });
  40. }
  41. });
  42. </script>';
  43. }
Advertisement
Add Comment
Please, Sign In to add comment