How to Hide All Slugs in WordPress

Home » Snippets » How to Hide All Slugs in WordPress
0

Created with:

Visibility: 

public

Creator: WPTurbo Team

Customize with WPTurbo AI
X

Add Snippet To Project

New Project
Add To Existing Project

Are you looking for a way to hide the slugs of your WordPress website? While slugs can be useful for SEO purposes and URL structure, there may be instances where you want to hide them from being displayed. Whether you’re concerned about the aesthetics of your website or want to keep your content more secure, this article will guide you through the process of hiding all slugs in WordPress. Stay tuned to learn the steps and get started with hiding your slugs effectively.

					function wpturbo_hide_all_slugs() {
    global $wpdb;
    $wpdb->query( $wpdb->prepare( "UPDATE $wpdb->posts SET post_name = ''" ) );
}
add_action( 'wp', 'wpturbo_hide_all_slugs' );
				

The code snippet provided aims to hide all slugs in WordPress. This can be useful in certain situations, such as when you want to display a website without revealing the slugs of the pages or posts.

To achieve this, the code defines a function called wpturbo_hide_all_slugs(). Inside the function, a global variable $wpdb is accessed, which is the WordPress database object.

The next line of code executes a database query using the $wpdb object. The query updates the post_name column in the wp_posts table, setting its value to an empty string. This effectively removes the slugs for all posts and pages in the database.

global $wpdb;
$wpdb->query( $wpdb->prepare( "UPDATE $wpdb->posts SET post_name = ''" ) );

It’s worth noting that this code directly modifies the database, so it’s recommended to have a backup of your database before using it. Additionally, this code will hide the slugs globally and affect all posts and pages on your WordPress site.

To ensure that the wpturbo_hide_all_slugs() function is executed, we need to hook it to a WordPress action. In this case, the code uses the wp action, which runs after WordPress has finished loading but before any content is output. This guarantees that the slug hiding process occurs at the right moment.

add_action( 'wp', 'wpturbo_hide_all_slugs' );

By adding this code to your WordPress theme’s functions.php file, you will hide all slugs on your site. However, it’s important to note that this action is irreversible. Once the slugs are hidden, they cannot be easily restored unless you have a backup of your database.

Register an account to save your snippets or go Pro to get more features.