1. Home
  2. Docs
  3. Application Framework
  4. Annotations Reference
  5. Post Page

Post Page

@MWP\WordPress\PostPage

Using this annotation will assign your controller as the handler for rendering the content of a post.

@Params

post_id=1 (optional) – The post id
post_getter="getPost" (optional) / default: ‘getPost’ – A method on your controller to get the post id

The post page annotation can only be used on a class. The class must follow the MWP\Framework\Pattern\Controller pattern. The post which your controller will render the output of the page for is determined by specifying a specific post_id, or returning the post_id or WP_Post object from the instance method specified in the post_getter param. By default, the annotation will call the getPost() method on your controller to get the post that which uses your controller.

@Example Code

The default method that will be used to generate the post content will be the do_index() method. If the url contains a &do=action parameter, the corresponding method do_action() on the class will be called to process and output the page. This allows you to have one controller that can have various different content screens for the same post page.

Also, if your controller contains an init() method, that method will be called whenever the post page is being viewed. You can use it to load scripts, styles, etc.

/**
 * A post page controller
 *
 * @MWP\WordPress\PostPage
 */
class CustomPageController extends MWP\Framework\Pattern\Controller 
{
    public function init()
    {
        // load scripts, styles, etc.
    }

    // stuff to do when page is first accessed
    public function do_index()
    {
        echo "Content has arrived.";
    }

    /* Get the post which hosts this controller */
    public function getPost()
    {
        // Get the post id from a configurable source (such as a plugin settings page).
        $post_id = MyPlugin::instance()->getSetting( 'custom_page_post_id' );
        return $post_id;
    }
}
Was this article helpful to you? Yes No

How can we help?