¿Forzar la elección de categoría antes de crear una nueva publicación?

10

¿Cómo puedo forzar al usuario a elegir primero una categoría antes de continuar con el editor al crear una nueva publicación? Quiero configurar un contenido predeterminado , pero esto se basa en la categoría, así que necesito saber eso antes de mostrar el editor (a menos que haga algunas cosas de Ajax de lujo, pero en este caso no quiero hacer eso).

    
pregunta Jan Fabry 10.04.2011 - 13:17

1 respuesta

9

Resolví esto al enlazar con post-new.php y verificando un parámetro de solicitud category_id . Si no existe, muestro un formulario con un menú desplegable de categoría que se envía a esta página y luego llamo a exit() para que no se muestre el formulario de envío regular. Si existe, configuro un gancho para wp_insert_post que agregará la categoría. Esto funciona porque ya se creó una nueva publicación en la base de datos a través de la función get_default_post_to_edit() , y podemos agregar categorías, etiquetas u otro (meta) contenido. El formulario se procesa después de esto con el nuevo contenido "fresco".

add_filter( 'load-post-new.php', 'wpse14403_load_post_new' );
function wpse14403_load_post_new()
{
    $post_type = 'post';
    if ( isset( $_REQUEST['post_type'] ) ) {
        $post_type = $_REQUEST['post_type'];
    }

    // Only do this for posts
    if ( 'post' != $post_type ) {
        return;
    }

    if ( array_key_exists( 'category_id', $_REQUEST ) ) {
        add_action( 'wp_insert_post', 'wpse14403_wp_insert_post' );
        return;
    }

    // Show intermediate screen
    extract( $GLOBALS );
    $post_type_object = get_post_type_object( $post_type );
    $title = $post_type_object->labels->add_new_item;

    include( ABSPATH . 'wp-admin/admin-header.php' );

    $dropdown = wp_dropdown_categories( array(
        'name' => 'category_id[]',
        'hide_empty' => false,
        'echo' => false,
    ) );

    $category_label = __( 'Category:' );
    $continue_label = __( 'Continue' );
    echo <<<HTML
<div class="wrap">
    <h2>{$title}</h2>

    <form method="get">
        <table class="form-table">
            <tbody>
                <tr valign="top">
                    <th scope="row">{$category_label}</th>
                    <td>{$dropdown}</td>
                </tr>
                <tr>
                    <td></td>
                    <th><input name="continue" type="submit" class="button-primary" value="{$continue_label}" /></th>
            </tbody>
        </table>
        <input type="hidden" name="post_type" value="{$post_type}" />
    </form>
</div>
HTML;
    include( ABSPATH . 'wp-admin/admin-footer.php' );
    exit();
}

// This function will only be called when creating an empty post,
// via 'get_default_post_to_edit()', called in post-new.php
function wpse14403_wp_insert_post( $post_id )
{
    wp_set_post_categories( $post_id, $_REQUEST['category_id'] );
}
    
respondido por el Jan Fabry 10.04.2011 - 13:37

Lea otras preguntas en las etiquetas