Skip to navigation | Skip to content



Easy child page creation for WordPress

Here’s a quickie that can help with WordPress sites that have a lot of pages. If some of the pages have longish titles, the “Parent Page” drop-down on the edit page screen can get unwieldy. The scroller for the drop-down can run off the right side of the screen.

OK, it’s fine (often easier) to use the keyboard to navigate drop-downs. But not everyone knows how to, and even so, with a large amount of pages, selecting the right parent can become a pain.

I think I adapted the following code from a plugin or someone else’s post. Thanks whoever you are, but frankly I’m so busy at the moment I’ve forgotten where it came from! Anyway, if you drop the following into your custom theme’s functions.php, you’ll get a new “Create child” link under every page listed in WP admin, when you hover over the title:

add_filter( 'page_row_actions', 'slt_child_page_action', 10, 2 );
function slt_child_page_action( $actions, $page ) {
	$actions["create-child"] = '<a href="' . add_query_arg( array( 'post_type' => 'page', 'parent_id' => $page->ID ), admin_url( 'post-new.php') ) . '" title="Create a new page with this page as its parent">Create child</a>';
	return $actions;
}
add_action( 'edit_page_form', 'slt_set_child_page' );
function slt_set_child_page() {
	global $post;
	if ( $post->post_type == "page" && $post->post_parent == 0 && isset( $_GET["parent_id"] ) && is_numeric( $_GET["parent_id"] ) )
		echo '<script type="text/javascript">jQuery( document ).ready( function($) { $("#parent_id").val("' . $_GET["parent_id"] . '"); } );</script>';
}

The first bit adds the page action link. The second bit adds some jQuery to the right edit screen to set the specified parent, if it’s been passed. I couldn’t find another way to set the drop-down… Well, it works!

UPDATE 23/10/10: Changed the URL for the creation of a new page from /wp-admin/page-new.php to /wp-admin/post-new.php?post_type=page.

UPDATE 21/9/11: Changed the URL for the creation of a new page again, to be properly dynamic with admin_url and add_query_arg. Thanks One Trick Pony!

4 comments

  1. VALERIE (7th September 2010)

    Hello Steve,

    I try your esy child page creation for WP but i got a “404 error” (url not found). Do you have any idea to solve this ?

    Thanks in advance and sorry for my english (i am french),

    Valérie

  2. Steve Taylor (7th September 2010)

    Valérie, I can’t do anything without any details, sorry…

  3. Prams (23rd October 2010)

    I have always been intrigued by the idea of creating child pages for wordpress but never really knew how to. Im so glad to have stumbled across this post. This has a very clear description along with the code segment.

  4. Steve Taylor (23rd October 2010)

    Apologies, Valeries, my bad – I should have updated this because a recent WP upgraded ditched the page-new.php file in favour of post-new.php?post_type=page. The code above should work now.

Comments on this post are closed.

Recent posts

View complete archives »