I have a blog with a very complex page structure (like, 4 levels of hierarchy).

When creating new pages, it is very cumbersome to pick the parent page from the drop-down list, as it is constantly growing.

For that reason, I would like to add a button to the frontend admin bar that points to post-new.php, but with a “parent page ID” parameter. Effectively, the button would allow you to navigate to the desired parent page on the front page, and then click a “add new page here” link there that takes you to the back-end with the parent page preselected.

However, it seems to be impossible to add parameters to post-new.php and a related feature request has been closed in the WordPress trac.

Does anybody know a solution that does not require hacking the core? One idea I had was hooking up to a hook that gets triggered before a new item is created, pulling the parent page from a GET parameter there, and setting it in the item-to-be. If that is a viable way, I would be grateful for a simple pointer which hook to use.

1 Answer
1

You are right on the mark with GET, that would probably be easiest to make use of.

Try this:

Add_Child_Page::on_load();

class Add_Child_Page {

    static function on_load() {

        add_action( 'init', array( __CLASS__, 'init' ) );
        add_action( 'admin_init', array( __CLASS__, 'admin_init' ) );
    }

    static function init() {

        add_action( 'admin_bar_menu', array( __CLASS__, 'admin_bar_menu' ), 90 );
    }

    static function admin_bar_menu( $wp_admin_bar ) {

        if( is_page() ) {

            $wp_admin_bar->add_node( array(
                'id'    => 'add_child_page',
                'title' => 'Add Child Page',
                'href'  => add_query_arg( array( 'post_type'   => 'page', 'page_parent' => get_the_ID() ), admin_url( 'post-new.php' ) ),
            ) );
        }
    }

    static function admin_init() {

        add_filter( 'page_attributes_dropdown_pages_args', array( __CLASS__, 'page_attributes_dropdown_pages_args' ) );
    }

    static function page_attributes_dropdown_pages_args( $dropdown_args ) {

        if ( ! empty($_REQUEST['page_parent']) )
            $dropdown_args['selected'] = (int) $_REQUEST['page_parent'];

        return $dropdown_args;
    }
}

Leave a Reply

Your email address will not be published. Required fields are marked *