Created
March 19, 2026 18:44
-
-
Save mralaminahamed/e4586f379d2b7fe75c503b075b1fcf23 to your computer and use it in GitHub Desktop.
Disable public singular access for the 'arts_service' post type.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| /** | |
| * Disable public singular access for the 'arts_service' post type. | |
| * | |
| * Filters registration arguments to remove front-end exposure, flushes | |
| * rewrite rules on theme activation/switch, and cleans up the admin | |
| * edit screen for a consistent editorial experience. | |
| * | |
| * @package YourChildTheme | |
| */ | |
| /** | |
| * Disable public-facing arguments for the arts_service post type. | |
| * | |
| * Runs at priority 20 to ensure it fires after the registering plugin | |
| * (default priority 10), guaranteeing our overrides are applied last. | |
| * | |
| * @param array $args Post type registration arguments. | |
| * @param string $post_type Post type slug. | |
| * | |
| * @return array Modified registration arguments. | |
| */ | |
| function arts_service_disable_public_args( array $args, string $post_type ): array { | |
| if ( 'arts_service' !== $post_type ) { | |
| return $args; | |
| } | |
| $args['public'] = false; | |
| $args['publicly_queryable'] = false; | |
| $args['rewrite'] = false; | |
| $args['query_var'] = false; | |
| $args['show_in_rest'] = false; // Prevent REST API exposure. | |
| return $args; | |
| } | |
| add_filter( 'register_post_type_args', 'arts_service_disable_public_args', 20, 2 ); | |
| /** | |
| * Flush rewrite rules when the child theme is activated or switched to, | |
| * ensuring stale arts_service rewrite rules are purged immediately. | |
| * | |
| * Hooked to `after_switch_theme` which fires once on activation. | |
| * Avoid calling flush_rewrite_rules() on every request — it is expensive. | |
| * | |
| * @return void | |
| */ | |
| function arts_service_flush_rewrite_rules(): void { | |
| flush_rewrite_rules(); | |
| } | |
| add_action( 'after_switch_theme', 'arts_service_flush_rewrite_rules' ); | |
| /** | |
| * Remove the "View Post" link from the post list table row actions. | |
| * | |
| * Prevents editors from following a now-inaccessible front-end URL | |
| * directly from the admin list screen. | |
| * | |
| * @param array $actions Existing row action links. | |
| * @param WP_Post $post Current post object. | |
| * | |
| * @return array Modified row actions. | |
| */ | |
| function arts_service_remove_row_view_action( array $actions, WP_Post $post ): array { | |
| if ( 'arts_service' === $post->post_type ) { | |
| unset( $actions['view'] ); | |
| } | |
| return $actions; | |
| } | |
| add_filter( 'post_row_actions', 'arts_service_remove_row_view_action', 10, 2 ); | |
| /** | |
| * Remove the permalink HTML from the classic post editor title area. | |
| * | |
| * Suppresses the "Permalink:" line rendered beneath the post title input | |
| * on the arts_service edit screen, keeping the interface clean and | |
| * avoiding confusion over a non-functional URL. | |
| * | |
| * @param string $html Existing permalink HTML markup. | |
| * @param int $post_id Current post ID. | |
| * | |
| * @return string Empty string for arts_service; original markup otherwise. | |
| */ | |
| function arts_service_remove_permalink_html( string $html, int $post_id ): string { | |
| if ( 'arts_service' === get_post_type( $post_id ) ) { | |
| return ''; | |
| } | |
| return $html; | |
| } | |
| add_filter( 'get_sample_permalink_html', 'arts_service_remove_permalink_html', 10, 2 ); | |
| /** | |
| * Intercept any residual front-end requests for arts_service posts. | |
| * | |
| * Acts as a defence-in-depth measure in case a cached or externally | |
| * generated URL reaches the front end before rewrite rules are flushed, | |
| * or if another plugin re-enables publicly_queryable at runtime. | |
| * | |
| * @return void | |
| */ | |
| function arts_service_block_singular_requests(): void { | |
| if ( ! is_singular( 'arts_service' ) ) { | |
| return; | |
| } | |
| global $wp_query; | |
| $wp_query->set_404(); | |
| status_header( 404 ); | |
| nocache_headers(); | |
| include get_query_template( '404' ); | |
| exit; | |
| } | |
| add_action( 'template_redirect', 'arts_service_block_singular_requests' ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment