Functions
WP MVC provides a number of functions that can be used either inside or outside of a plugin's app/
directory. Many are designed to allow for WP MVC-related functionality to be referenced from elsewhere in a WordPress app (e.g. in a theme or in plugins that aren't WP MVC-based).
is_mvc_page
This simply returns a boolean signifying whether or not the current page is being rendered by a WP MVC-based plugin.
<?php
echo is_mvc_page() ? 'WP MVC page!' : 'Not a WP MVC page!';
?>
mvc_model
This allows you to easily use a WP MVC model from elsewhere in WP (e.g. in a theme). Pass the name of a model to it, and it will return an instantiation of the model that has all of the standard model methods and attributes.
<?php
$venue_model = mvc_model('Venue');
$venues = $venue_model->find(array('limit' => 5));
?>
mvc_public_url
This returns a URL the corresponds to the routing options that are passed to it. For example:
<?php
// http://mysite.com/venues/
echo mvc_public_url(array('controller' => 'venues'));
// http://mysite.com/venues/1
echo mvc_public_url(array('controller' => 'venues', 'id' => 1));
// http://mysite.com/venues/my_action/1
echo mvc_public_url(array('controller' => 'venues', 'action' => 'my_action', 'id' => 1));
?>
Ideally, for 'show'
actions, you would pass an object, as this allows Pretty URLs to be used if to_url()
is defined in the model:
<?php
// http://mysite.com/documentation/5/creating-database-tables/
$documentation_node_model = new DocumentationNode();
$documentation_node = $documentation_node_model->find_by_id(5);
echo mvc_public_url(array('object' => $documentation_node));
?>
Note: If you are looking to make a link to a MVC page from within an MVC page, you may want to use the HTML helper's link()
method:
<?php
$this->html->link('All Venues', array('controller' => 'venues'))
?>
mvc_admin_url
This behaves like mvc_public_url
, except that it provides the URL to admin MVC pages instead of public ones. For example:
<?php
// http://mysite.com/wp-admin/admin.php?page=mvc_venues
echo mvc_admin_url(array('controller' => 'venues'));
// http://mysite.com/wp-admin/admin.php?page=mvc_venues-add
echo mvc_admin_url(array('controller' => 'venues', 'action' => 'add'));
// http://mysite.com/wp-admin/admin.php?page=mvc_venues-edit&id=1
echo mvc_admin_url(array('controller' => 'venues', 'action' => 'edit', 'id' => 1));
?>
mvc_css_url
This returns the URL of a CSS style sheet that's in the specified plugin's app/public/
directory. If a file extension isn't given, a .css
is appended, but this automatic behavior can be bypassed by setting the add_extension
option to false
, as shown below.
<?php
// This will return a URL that looks like the following:
// http://mysite.com/wp-content/plugins/events-calendar-example/app/public/css/my_style.css
echo mvc_css_url('events-calendar-example', 'my_style');
// This will prevent the ".css" from being appended:
// http://mysite.com/wp-content/plugins/events-calendar-example/app/public/css/my_style
echo mvc_css_url('events-calendar-example', 'my_style', array('add_extension' => false));
?>
mvc_js_url
This returns the URL of a JavaScript file that's in the specified plugin's app/public/
directory. If a file extension isn't given, a .js
is appended, but this automatic behavior can be bypassed by setting the add_extension
option to false
, as shown below.
<?php
// This will return a URL that looks like the following:
// http://mysite.com/wp-content/plugins/events-calendar-example/app/public/js/my_script.js
echo mvc_js_url('events-calendar-example', 'my_script');
// This will prevent the ".js" from being appended:
// http://mysite.com/wp-content/plugins/events-calendar-example/app/public/js/my_script
echo mvc_js_url('events-calendar-example', 'my_script', array('add_extension' => false));
?>
mvc_render_to_string
This lets you capture the output of a view as a string. The first argument is the view path, and the second is an array of variables that the view will use (this is optional).
<?php
// Somewhere in the theme:
?>
<?php
$venue_model = mvc_model('Venue');
$venues = $venue_model->find(array('limit' => 5));
$venues_list_html = mvc_render_to_string('venues/list', array('objects' => $venues));
?>
<ul class="venues-list"><?php echo $venues_list_html; ?></ul>
<?php
// In app/views/venues/list.php:
?>
<?php foreach($objects as $object): ?>
<li><?php echo $this->html->venue_link($object); ?></li>
<?php endforeach; ?>
mvc_get_plugins
This returns an array listing the currently active WP MVC-based plugins.
<?php
// This will return an array that looks like the following:
// Array ( [0] => events-calendar-example [1] => hierarchical-documentation )
print_r(mvc_get_plugins());
?>