Retain Child Page Slug

Introduction

Changing the parent page of a child page in WordPress alters its URL to include the parent’s slug. In some scenarios, you might want to keep the child page’s original slug. In this blog post, we’ll delve into a custom WordPress plugin that helps you retain the original slug of a child page, regardless of its parent.

Note: The code provided in this blog post can also be added to your child theme’s functions.php file if you prefer not to create a plugin.

Why This Matters

  1. SEO: Stable URLs perform better in search rankings.
  2. User Experience: Users appreciate consistent URLs.
  3. Links: Bookmarks and external links won’t break after reorganizing the site.

Prerequisites

  • Basic PHP understanding
  • Familiarity with WordPress Plugin Development
  • Access to your WordPress dashboard

Plugin Creation Guide

Step 1: Set Up the Plugin Folder and File

  1. Create a folder named retain-child-slug in wp-content/plugins.
  2. Create a PHP file within the folder, e.g., retain-child-slug.php.

Step 2: Initialize the Plugin

Open the retain-child-slug.php file and start with the following metadata:

<?php
/**
 * Plugin Name: Retain Child Page Slug
 * Description: Retains the original slug of a child page, even when it's assigned a parent page.
 * Version: 1.0
 * Author: WebFix Lab
 */

Step 3: Hook Into page_link to Modify the Permalink

We’ll use the page_link filter to change the URL for child pages. Add this code:

add_filter('page_link', 'retain_child_slug_permalink', 10, 2);

function retain_child_slug_permalink($post_link, $post) {
    if ($post->post_parent) {
        $parent_post = get_post($post->post_parent);
        $pattern = '/'. preg_quote($parent_post->post_name, '/') . '\//';
        $post_link = preg_replace($pattern, '', $post_link, 1);
    }
    return $post_link;
}

Explanation

  • add_filter: Hooks into the page_link filter.
  • retain_child_slug_permalink: Custom function to modify the permalink.
  • $post->post_parent: Checks if the page has a parent.
  • preg_replace: Removes the parent slug from the URL.

Step 4: Update WordPress Rewrite Rules

Next, we add a custom rewrite rule to make sure WordPress recognizes these new URLs:

add_action('init', 'retain_child_slug_rewrite_rules');

function retain_child_slug_rewrite_rules() {
    global $wp_rewrite;
    $wp_rewrite->use_verbose_page_rules = true;
    add_rewrite_rule(
        '([^/]+)/?$',
        'index.php?pagename=$matches[1]',
        'top'
    );
}

Explanation

  • add_action(‘init’, …): Hooks into WordPress’s initialization action.
  • global $wp_rewrite: Accesses the global rewrite rules object.
  • add_rewrite_rule: Adds a new rewrite rule that recognizes the child page using its original slug.

Step 5: Activate the Plugin

Save the retain_child_slug.php file, go to your WordPress dashboard, and navigate to Plugins > Installed Plugins. Locate your new plugin and click “Activate.”

Conclusion

By following these steps, you’ll create a plugin that allows you to retain the original URL slugs for child pages, regardless of their parent pages. This offers various advantages, such as SEO benefits and a better user experience. If you have any questions, feel free to contact us here. We typically reply all our emails within 24 hours.