Add new page with preselected parent

I do not want my clients to select a parent page from the drop down list, when creating a child page, so I would like to create a link on the dashboard which links to “add new page” – but with a preselected parent page.

Is this possible? And if not, then is there a way to change the default parent from “(no parent)” to a parent of my choice?

Related posts

Leave a Reply

1 comment

  1. welcome to WPSE!

    This answer from Mike Schinkel was useful to find a solution* for Get QueryString values with jQuery. *(not used anymore, a simple $_GET does it)

    It’s also worth noting that, in that same answer, Mike also links to this other important Question: What is the best way to add custom javascript files to the site?

    That being said, the following code does the trick. It doesn’t use admin_enqueue_scripts -best practice-, and instead is printing it directly in the admin footer…

    Add this code to your theme’s functions.php and make your custom link to “Add New Page” like this: http://example.com/wp-admin/post-new.php?post_type=page&the_id=28, being the_id the ID of the desired parent page.

    add_action( 'admin_head-post-new.php', 'pre_select_parent_wpse_56952' );
    
    function pre_select_parent_wpse_56952() 
    {
        // Check if adding new page 
        if( !isset($_GET['post_type']) || 'page' != $_GET['post_type'] ) 
            return;
    
        // Check for pre-selected parent
        if( !isset($_GET['the_id']) || empty( $_GET['the_id'] ) ) 
            return;
    
        // There is a pre-selected value for the correct post_type, proceed with script
        $the_id = $_GET['the_id'];
        ?>
            <script type="text/javascript">
            jQuery(document).ready( function($) 
            {
                $('#parent_id').val(<?php echo $the_id; ?>);
            });
            </script>
        <?php
    }