How To Remove Page Using Slug in WordPress Tutorial

Reading Time: 4 minutes
129 Views

WordPress is a versatile and user-friendly platform for managing the content of your website. While it is usual to create pages and posts, there may come a moment when you need to remove a certain page. We will walk you through the process of removing a page in WordPress using its slug as a reference in this article.

You’ll learn how to locate the page in your WordPress admin dashboard, confirm its identity using the slug, and proceed with the removal process.

Read More: Concept of Deactivation Hook in WordPress Tutorial

Let’s get started.

What is a Page Slug in WordPress?

A “page slug” in WordPress is the user-friendly, URL-friendly, and often lowercase version of a WordPress page or post’s title. The slug is an important part of the URL (permalink) for that page or post.

Concept of “Page Title”

Each page or post in WordPress has a title, which is typically set by the user when creating the content. The title can contain spaces, capitalization, and special characters.

Concept of “Page Slug”

WordPress will generate a slug for you depending on the title. It lowercases the title, removes spaces, and replaces special characters with hyphens. As a result, the title is clean and URL-friendly.

Example:

Title: "About Us"

Slug: "about-us"

How To Remove a WordPress Page Using Its Slug (By Code)?

You can use the wp_delete_post function to delete a post (including pages) programmatically to remove a WordPress page using its slug.

Here’s an example of how to accomplish this with a plugin:

function remove_page_by_slug() {
  
    $page_slug = 'your-page-slug'; // Replace with the actual slug of the page you want to remove

    $page = get_page_by_path($page_slug);

    if ($page) {
        // Delete the page
        wp_delete_post($page->ID, true);

        // Optionally, you can display a success message
        echo 'Page with slug "' . $page_slug . '" has been deleted.';
    } else {
        // Optionally, you can display an error message
        echo 'Page with slug "' . $page_slug . '" not found.';
    }
}

// Hook the function to an action, such as 'admin_init' or 'init'
add_action('init', 'remove_page_by_slug');

Replace ‘your-page-slug‘ with the actual slug of the page you want to remove.

Read More: Complete Concept of Plugin Activation Hook in WordPress

Please be cautious when using this code, as it will permanently delete the specified page, and there’s no way to recover it once it’s deleted.

That’s it.

We hope this article helped you to learn about How To Remove Page Using Slug in WordPress Tutorial in a very detailed way.

Online Web Tutor invites you to try Skillshike! Learn CakePHP, Laravel, CodeIgniter, Node Js, MySQL, Authentication, RESTful Web Services, etc into a depth level. Master the Coding Skills to Become an Expert in PHP Web Development. So, Search your favourite course and enroll now.

If you liked this article, then please subscribe to our YouTube Channel for PHP & it’s framework, WordPress, Node Js video tutorials. You can also find us on Twitter and Facebook.

Sanjay KumarHello friends, I am Sanjay Kumar a Web Developer by profession. Additionally I'm also a Blogger, Youtuber by Passion. I founded Online Web Tutor and Skillshike platforms. By using these platforms I am sharing the valuable knowledge of Programming, Tips and Tricks, Programming Standards and more what I have with you all. Read more