fauzanjeg

JNews || Limit the frontend submit "Add Media" button for Authors to "Insert from URL" only

Aug 10th, 2026
149
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 3.63 KB | None | 0 0
  1. <?php
  2. /**
  3.  * Limit the frontend submit "Add Media" button for Authors to "Insert from URL" only.
  4.  */
  5.  
  6. if ( ! function_exists( 'my_jnews_fs_author_url_only' ) ) {
  7.     /**
  8.      * Check whether the current user has the Author role.
  9.      *
  10.      * @return bool True when the current user is an Author, otherwise false.
  11.      */
  12.     function my_jnews_fs_author_url_only() {
  13.         if ( ! is_user_logged_in() ) {
  14.             return false;
  15.         }
  16.  
  17.         $user = wp_get_current_user();
  18.  
  19.         return $user instanceof WP_User && in_array( 'author', (array) $user->roles, true );
  20.     }
  21. }
  22.  
  23. if ( ! function_exists( 'my_jnews_fs_is_editor_page' ) ) {
  24.     /**
  25.      * Check whether the current page is the JNews frontend submit editor.
  26.      *
  27.      * @return bool True when the current page is the frontend editor, otherwise false.
  28.      */
  29.     function my_jnews_fs_is_editor_page() {
  30.         if ( ! class_exists( 'JNews_Frontend_Endpoint' ) ) {
  31.             return false;
  32.         }
  33.  
  34.         global $wp;
  35.  
  36.         $endpoint = JNews_Frontend_Endpoint::getInstance()->get_endpoint();
  37.  
  38.         return isset( $endpoint['editor']['slug'], $wp->query_vars[ $endpoint['editor']['slug'] ] );
  39.     }
  40. }
  41.  
  42. /**
  43.  * Keep only the "Insert from URL" tab inside the WordPress media popup.
  44.  */
  45. add_filter(
  46.     'media_upload_tabs',
  47.     function ( $tabs ) {
  48.         if ( ! my_jnews_fs_author_url_only() ) {
  49.             return $tabs;
  50.         }
  51.  
  52.         global $pagenow;
  53.  
  54.         if ( 'media-upload.php' !== $pagenow ) {
  55.             return $tabs;
  56.         }
  57.  
  58.         $referer = wp_get_referer();
  59.  
  60.         if ( ! $referer || false === strpos( $referer, '/editor/' ) ) {
  61.             return $tabs;
  62.         }
  63.  
  64.         return array(
  65.             'type_url' => __( 'Insert from URL', 'jnews' ),
  66.         );
  67.     }
  68. );
  69.  
  70. /**
  71.  * Force the media popup to load the "Insert from URL" tab for Authors.
  72.  */
  73. add_action(
  74.     'admin_init',
  75.     function () {
  76.         if ( ! my_jnews_fs_author_url_only() ) {
  77.             return;
  78.         }
  79.  
  80.         global $pagenow;
  81.  
  82.         if ( 'media-upload.php' !== $pagenow ) {
  83.             return;
  84.         }
  85.  
  86.         $referer = wp_get_referer();
  87.  
  88.         if ( ! $referer || false === strpos( $referer, '/editor/' ) ) {
  89.             return;
  90.         }
  91.  
  92.         $_REQUEST['tab'] = 'type_url';
  93.         $_GET['tab']     = 'type_url';
  94.     },
  95.     1
  96. );
  97.  
  98. /**
  99.  * Load Thickbox assets because the frontend editor uses the classic WordPress media popup.
  100.  */
  101. add_action(
  102.     'wp_enqueue_scripts',
  103.     function () {
  104.         if ( ! my_jnews_fs_author_url_only() || ! my_jnews_fs_is_editor_page() ) {
  105.             return;
  106.         }
  107.  
  108.         add_thickbox();
  109.     },
  110.     20
  111. );
  112.  
  113. /**
  114.  * Override the "Add Media" button so it opens the URL-only media popup.
  115.  */
  116. add_action(
  117.     'wp_footer',
  118.     function () {
  119.         if ( ! my_jnews_fs_author_url_only() || ! my_jnews_fs_is_editor_page() ) {
  120.             return;
  121.         }
  122.         $media_upload_url = add_query_arg(
  123.             array(
  124.                 'post_id'    => 0,
  125.                 'TB_iframe'  => 'true',
  126.                 'width'      => 640,
  127.                 'height'     => 517,
  128.                 'tab'        => 'type_url',
  129.             ),
  130.             admin_url( 'media-upload.php' )
  131.         );
  132.         ?>
  133.         <script>
  134.             (function($){
  135.                 "use strict";
  136.  
  137.                 $(function () {
  138.                     var $button = $('#insert-media-button');
  139.                     var customUrl = <?php echo wp_json_encode( $media_upload_url ); ?>;
  140.  
  141.                     // Stop if the editor button is not available.
  142.                     if (!$button.length) {
  143.                         return;
  144.                     }
  145.  
  146.                     // Remove the default WordPress media behavior from this button.
  147.                     $button.removeAttr('data-editor data-wp-media');
  148.                     $button.off('click');
  149.  
  150.                     // Open the URL-only popup instead of the normal media uploader.
  151.                     $button.on('click.jnewsUrlOnly', function (event) {
  152.                         event.preventDefault();
  153.  
  154.                         if (typeof window.tb_show === 'function') {
  155.                             window.tb_show('<?php echo esc_js( __( 'Insert from URL', 'jnews' ) ); ?>', customUrl);
  156.                         }
  157.  
  158.                         return false;
  159.                     });
  160.                 });
  161.             })(jQuery);
  162.         </script>
  163.         <?php
  164.     },
  165.     99
  166. );
Advertisement
Add Comment
Please, Sign In to add comment