1

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?

2

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
}
  • 1
    Wow! You sir, are a modern day hero! I can not thank you enough for this! – Cody Jun 30 '12 at 15:09
  • could this be used for preselecting page templates as well? – Cody Jul 1 '12 at 11:43
  • Yes, quite easily, you only have to analyze the HTML code, check the ID and values used, and add the correspondent two lines of Javascript (template_id)... Give it a try but ping me in case of troubles (i'm on the iphone right now;) – brasofilo Jul 1 '12 at 12:02
  • Similar code for templates in my answer to this question: wordpress.stackexchange.com/questions/53486/… – brasofilo Jul 1 '12 at 12:18
  • hi I am trying to do the same for a new project - but with preseleted for posts, do you think this could be done? :) – Cody Jan 8 '13 at 11:24

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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