I am trying to add an option to create a child page from the wordpress all pages screen. I found this link, but cannot get it working properly. It is exactly what I need, but I need help fixing it

http://sltaylor.co.uk/blog/easy-child-page-creation-wordpress/

function slt_childPageAction( $actions, $page ) {  
    $actions["create-child"] = '<a href="/wp-admin/post-new.php?post_type=page&amp;parent_id=' . $page->ID . '" title="Create a new page with this page as its parent">Create child</a>';  
    return $actions;  
}  
add_filter( 'page_row_actions', 'slt_childPageAction', 10, 2 );  
function slt_setChildPage() {  
    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>';  
}  
add_action( 'edit_page_form', 'slt_setChildPage' );  
link|improve this question

40% accept rate
feedback

1 Answer

up vote 0 down vote accepted

If you mean the 404 error:

function slt_childPageAction( $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_filter( 'page_row_actions', 'slt_childPageAction', 10, 2 );
function slt_setChildPage() {
    global $post;
    if ( $post->post_type === 'page' && $post->post_parent === 0 && isset( $_GET["parent_id"] ) )
        echo '<script type="text/javascript">jQuery( document ).ready( function($) { $("#parent_id").val("' . (int)$_GET["parent_id"] . '"); } );</script>';
}
add_action( 'edit_page_form', 'slt_setChildPage' );

(the url path to /wp-admin/ was hard-coded)

nice find btw :)

link|improve this answer
Great! works like a charm. Do you know how I could have this show up only on top level parent pages? – Shae Sep 21 '11 at 17:18
Add a conditional check in the 1st function: if($page->post_parent == 0) – One Trick Pony Sep 21 '11 at 18:21
Perfect! Thanks for the help! – Shae Sep 21 '11 at 18:33
One thing I noticed, the add child link shows up on all types. ex., all trash items, and some plugins using custom post types. How can you show this only on pages? – Shae Sep 22 '11 at 19:16
feedback

Your Answer

 
or
required, but never shown

Not the answer you're looking for? Browse other questions tagged or ask your own question.