1

I'm working on a CPT but I need to have more control over the layout of the post/edit page (post-new.php and post.php). I thought hacking through admin_init would be the best option, but I can't get the script to work at all. Help?

function init_shelf_page() {
    if (!current_user_can('edit_shelves') && $_SERVER['PHP_SELF'] == '/wp-admin/post.php') {
        if (isset($_GET['post'])) {
            $postID = intval($_GET['post']);
            $post = get_post($postID);
            if ($post->post_type == 'shelf') {
                $post_type_object = get_post_type_object($post->post_type);
                if (!current_user_can($post_type_object->cap->edit_posts)) {
                    wp_die(__('Cheatin’ uh?'));
                }
                include(dirname(__FILE__) . '/shelf-page.php');
                die;
            }
        }
    }
}
add_action('admin_init', 'init_shelf_page');

1 Answer 1

1

I suggest you just don't use the standard post editing UI. When you register your post type, there's an arg for showing the admin UI.

<?php
register_post_type(
    'some_type',
     array(
       // stuff here
       'show_ui' => false
     )
);

Then just create your own admin page and do whatever you need to do with the interface. Here's a skeleton example.

<?php
add_action( 'admin_init', 'wpse33382_add_page' );
function wpse33382_add_page()
{
    $page = add_menu_page(
        __( 'Some Title' ),
        __( 'Menu Title' ),
        'edit_others_posts',
        'some-slug',
        'wpse33382_page_cb',
    );

    add_action( 'admin_print_scripts-' . $page, 'wpse33382_scripts' );
    add_action( 'admin_print_styles-' . $page, 'wpse33382_styles' );
}

function wpse33382_page_cb()
{
    // code for the page itself here
    ?>
        <form method="post" action="">
            <input type="hidden" name="action" value="do_stuff" />
            <?php wp_nonce_field( 'wpse33382_nonce', '_nonce' ); ?>
        </form>

    <?php
    // catch POST submissions here, then call wp_insert_post
    if( isset( $_POST['action'] ) && 'do_stuff' == $_POST['action'] )
    {
        // verify nonces/referrers here, then save stuff
    }
}

function wpse33382_scripts()
{
    // wp_enqueue_script here
}

function wpse33382_styles()
{
    // wp_enqueue_style here
}

The other option would be adding whatever custom meta boxes you need to your standard editing screen.

3
  • I would like to keep the standard UI for managing/listing my CPT (e.g. edit.php?post_type=shelf) instead of creating another screen to maintain. In other words, I want to redirect only the post.php?action=edit&post=XX and post-new.php?post_type=shelf pages to my own. I know I could just edit the link in the manage screen to my own menu page, but then I'd need to register my menu page without showing it on the menu.
    – impleri
    Commented Nov 10, 2011 at 13:53
  • You're talking about creating your own page anyway? Your method, as you coded it above, is actually going to stop the entire WordPress admin from loading. Look into custom meta boxes, that sounds like more of what you need/want. Commented Nov 10, 2011 at 14:58
  • Well, what I coded above does nothing. The standard UI screen loads like normal. Perhaps I'm having an issue with the draggable/droppable, which doesn't seem to be working properly for me in the standard UI (that was my option before this one).
    – impleri
    Commented Nov 10, 2011 at 15:16

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.