tombenner / wp-mvc

An MVC framework for WordPress
http://wpmvc.org
MIT License
623 stars 172 forks source link

Set the plugin view as the home page #242

Closed crisferar closed 5 years ago

crisferar commented 5 years ago

Hello, I am working with wordpress and wp-mvc and I would like to know if it is possible or how can you establish a plugin view as wordpress home page. Thank you

cyberscribe commented 5 years ago

Plugin pages are bound to the /controller/action/id URL structure. However, there is nothing to stop you including a view in e.g. a short code that you define and use on the homepage.

akamadr commented 5 years ago

I know it's bit late, but maybe someone else will look for it.

In public controller (ie. products_controller.php) I created method list()

public function list() {
    $this->view_rendered = true;

    // Params are shortcode atts
    $params = $this->params;

    $this->process_params_for_search();
    $collection = $this->model->paginate( $params );
    $this->set( 'objects', $collection['objects'] );

    if ( $params['pagination'] ) {
        // Set pagination
        $this->pagination = array(
            'total'             => $collection['total_pages'],
            'current'           => $collection['page'],
        );

        unset( $collection['objects'] );
        $this->pagination = array_merge( $collection, $this->pagination );
    }

    $response = $this->render_to_string( 'products/index' );

    return $response;
}

And then I create shortcode and use MvcDispatcher to return rendered shortcode content.

add_shortcode( 'products', products_shortcode' );
function products_shortcode( $atts ) {
    $atts = shortcode_atts( array(
        'controller'    => 'products',
        'action'        => 'list', // We're pointing to method created above
        'page'      => get_query_var( 'page', 1 ),
        'per_page'  => get_option( 'posts_per_page' ),
        'pagination'    => false,
    ), $atts, 'products' );

    return MvcDispatcher::dispatch( $atts );
}

Hope you find it helpfull 😊