5

Here is an amazing solution to restrict the creation of new parent pages, and I would want to use it, but the newly created sub-pages are saved only as drafts and this was not solved (see last comments at mentioned link).

The problem is that this code always saves the page as Draft and I have no idea how to solve this:

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();
    }
}
5
  • 2
    Me neither. Tried some variations but no luck.
    – brasofilo
    Commented Jan 8, 2013 at 19:58
  • You are aware of the fact that your shown code disables the creation if you got more than 9 pages?
    – kaiser
    Commented Jan 10, 2013 at 21:17
  • 1
    Btw: This question is a blank duplicate. Why didn't you just add the bounty on the other question???
    – kaiser
    Commented Jan 10, 2013 at 21:18
  • @kaiser, bad move, I know. I raised a flag on this one, had no answer, did a bad calculus. And as I don't like chat rooms since 1988... :/
    – brasofilo
    Commented Jan 11, 2013 at 15:15
  • @brasofilo Realized one hour later that it was actually you adding the bounty ... which confused me even more. Talked to Rarst yesterday about it in chat, but I guess you/me have to poke someone else to move the rep.
    – kaiser
    Commented Jan 11, 2013 at 16:20

2 Answers 2

5
+100

you actually don't need any ajax or server side action, simple see if the user has selected a parent page:

add_action( 'admin_head-post-new.php', 'publish_admin_hook_wpse_78690' );
add_action( 'admin_head-post.php', 'publish_admin_hook_wpse_78690' );

function publish_admin_hook_wpse_78690()
{
    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 parent_id = jQuery('#parent_id').val();
                if (parseInt(parent_id) > 0){
                    jQuery('#ajax-loading').hide();
                    jQuery('#publish').removeClass('button-primary-disabled');
                    return true;
                }else{
                    alert("please selet your parent page");
                    jQuery('#ajax-loading').hide();
                    jQuery('#publish').removeClass('button-primary-disabled');
                    return false;
                }
                return false;
            });
        });
    </script>
    <?php
}
7
  • 2
    The matter is calculating if the maximum number of parents is complete or not... But just saw that this code alone (without Ajax) breaks the capability of clicking "Publish" and having the post_status go from Draft to Publish...
    – brasofilo
    Commented Jan 11, 2013 at 15:25
  • @Bainternet The result is the same: pages are saved as Draft.
    – Yuri
    Commented Jan 11, 2013 at 20:38
  • @brasofilo Its was cause by invoking the post instead of returning true when needed, i updated my answer.
    – Bainternet
    Commented Jan 12, 2013 at 16:40
  • When working with the original script the return true/false is not triggered in the Ajax response (seems that it's too late for the .click()), is it possible to overcome this?
    – brasofilo
    Commented Jan 13, 2013 at 12:39
  • Catch the form submit()?
    – Bainternet
    Commented Jan 13, 2013 at 22:04
0

I use Lock Pages plugin. Lock Pages prevents specified pages (or all pages) from having their slug, parent, status or password edited, or from being deleted, by non-administrator. http://wordpress.org/extend/plugins/lock-pages/

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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