Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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?

share|improve this question
add comment

1 Answer 1

up vote 2 down vote accepted

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
}
share|improve this answer
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
    
Cool, thanks again! :) –  Cody Jul 3 '12 at 7:21
show 5 more comments

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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