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'm looking for a way to only allow users to create new subpages once there are nine pages that are parents/root pages. Once there are nine parent pages when trying to create a new parent the users will get an error message.

Edit:

Moved from the comments to @brasofilo s answer below:

when I press submit it processes as it should but when the page refreshes I get the message: "Page updated" rather than "Page published. View page". So it's only saved as a draft, not actually published.

So the goal is to have the post status behave like it would for a default post type, when the new limit isn't triggering.

Ideas?

Thanks

share|improve this question
add comment

1 Answer 1

up vote 3 down vote accepted

Code based in Bainternet's answer to this question: Make Categories and Tags required in admin

See code comments.

add_action( 'admin_head-post-new.php', 'wpse_59770_publish_admin_hook' );
add_action( 'admin_head-post.php', 'wpse_59770_publish_admin_hook' );
add_action( 'wp_ajax_wpse_59770_pre_submit_validation', 'wpse_59770_ajax_pre_submit_validation' );

function wpse_59770_publish_admin_hook()
{
    global $current_screen;
    if( 'page' != $current_screen->post_type )
        return;

    ?>
    <script language="javascript" type="text/javascript">
        jQuery(document).ready(function() {
            jQuery('#publish').click(function() 
            {
                var form_data = jQuery('#parent_id').val();
                form_data = ( '' != form_data ) ? '1' : '0';
                var data = {
                    action: 'wpse_59770_pre_submit_validation',
                    security: '<?php echo wp_create_nonce( 'pre_publish_validation' ); ?>',
                    form_data: form_data
                };
                jQuery.post(ajaxurl, data, function(response) 
                {
                    // OK, save page
                    if (response=='true') {
                        jQuery('#ajax-loading').hide();
                        jQuery('#publish').removeClass('button-primary-disabled');
                        jQuery('#post').submit();
                    }
                    // Not OK, display alert message
                    else
                    {
                        alert(response);
                        jQuery('#ajax-loading').hide();
                        jQuery('#publish').removeClass('button-primary-disabled');
                        return false;
                    }
                });
                return false;
            });
        });
    </script>
    <?php
}


function wpse_59770_ajax_pre_submit_validation()
{
    //simple Security check
    check_ajax_referer( 'pre_publish_validation', 'security' );

    // Parent is set, no further action
    if( '1' == $_POST['form_data'] )
    {
        echo 'true'; 
        die();
    }

    $args = array( 'post_type' => 'page', 'post_parent'=> 0, 'numberposts' => -1 );
    $parents_total = get_posts( $args );

    // Total parents is less than 9, no further action
    if( count($parents_total) < 9 )
    {
        echo 'true'; 
        die();
    }
    // No more parents allowed
    else
    {
        $error = "No more Parent Pages allowed";   
        echo $error; 
        die();
    }
}
share|improve this answer
    
This script won't allow publishing of any page, sub page nor parent page. Ideas? –  INT Sep 30 '12 at 22:30
    
Not sure what gone wrong with this code, I always test before posting... With some debugging tool maybe you can make it work... –  brasofilo Sep 30 '12 at 23:46
    
$args = array( 'post_type' => 'page', 'post_parent'=> 0 ); should be $args = array( 'post_type' => 'page', 'post_parent'=> 0, 'numberposts' => -1 ); That was the problem :) –  INT Oct 30 '12 at 14:21
    
Thanks @INT, code corrected. Next time, don't be shy and edit the Answer yourself ;) –  brasofilo Oct 31 '12 at 1:45
1  
K, finally saw it... I guess that jQuery('#post').submit(); is missing/messing something :/ Adding the action wp_ajax_nopriv_wpse_59770_pre_submit_validation (some target function) doesn't help either. But I cannot tackle the issue further now, will have to be in another moment... Try to search for similar Q&A or reduce the code to the minimum working block to try to locate the bug. –  brasofilo Nov 5 '12 at 22:11
show 6 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.