Tina MVC Application Setup and Teardown Helpers
So far (if you have progressed through the tutorials) you have a good idea of the basics of Tina. At this stage you should have looked at:
- Writing basic page controllers
- Writing your own view templates
- Using the dispatcher method
- Using the form helper
However some things need some special handling in WordPress. For example, if you are using widgets the output to the browser has already started and therefore you cannot do things like send browser headers or add scripts or stylesheets (wp_enqueue_script() and wp_enqueue_style() ).
Tina MVC Bootstrap Functionality
This functionality was added in version 0.3.
This is designed to allow you to run arbitrary code during certain WordPress hooks. Currently there are three points at which you can insert your code:
- ‘the_posts’ action hook
- ‘init’ action hook
- Tina MVC plugin install/remove
1. ‘the_posts’ action hook
This bootstrap functionality must be enabled in tina_mvc_app_settings.php. Look for the variable $tina_mvc_enable_bootstrap_funcs.
If you do not need this functionality then do not enable it. Tina MVC will check for the existence of your code on every page request (not just on Tina MVC page requests). (This is necessary because you might be using Tina MVC for shortcodes or widgets and not using the Tina MVC front end page controller.)
This allows you to use functions like wp_enqueue_script() and wp_enqueue_style() and have the code available for your widgets.
Where To Put Your Code
Put your code in the app_bootstrap folder. Tina MVC will include() any PHP file (other than index.php) it finds in there. It will then attempt to call a function as follows:
- Filename: my_custom_code.php; function name: my_custom_code()
Uses
Enqueue css and javascript files so they are available from withing your shortcode and widgets.
2. ‘init’ action hook
Enable in tina_mvc_app_settings.php. Look for the variable $tina_mvc_enable_init_bootstrap_funcs.
Again Tina MVC will check for the existence of your code on every page request, so there is a performance penalty.
Where To Put Your Code
Put your code in the app_init_bootstrap folder. Tina MVC will include() and run your code in the same way as it does for code in the app_bootstrap.
Uses
Running code that needs to be run early. For example code using the register_post_type() function.
3. Tina MVC plugin install/remove
Run whenever Tina MVC is activated/deactivated. This feature is always enabled.
Running code when Tina MVC is activated
Create a file called tina_mvc_app_install.php in the app_install_remove folder. Put your code in a function called tina_mvc_app_install().
Running code when Tina MVC is deactivated
Create a file called tina_mvc_app_remove.php in the app_install_remove folder. Put your code in a function called tina_mvc_app_remove().
Uses
Setting up database tables for your application or setting up custom roles and permissions. For example:
<?php /** * File name: tina_mvc_app_install.php */ function tina_mvc_app_install() { /** * Custom roles */ global $wp_roles; $my_roles = array( 'My First Custom Role'=>'my_custom_role_1' , 'My Second Custom Role'=>'my_custom_role_2' ); foreach( $my_roles AS $display_name => $role ) { if( ! $wp_roles->get_role( $role ) ) { $wp_roles->add_role( $role, $display_name, FALSE ); } } /** * * NB: read http://codex.wordpress.org/Creating_Tables_with_Plugins before messing with dbDelta!!! * */ global $wpdb; $tbl_name = $wpdb->prefix.'my_custom_table'; $sql_create = "CREATE TABLE $tbl_name ( id int(11) NOT NULL AUTO_INCREMENT, text_field_1 varchar(32) NOT NULL, text_field_2 varchar(64) NOT NULL, UNIQUE KEY id (id), KEY text_field_1 (cat_order) ) ENGINE=MyISAM DEFAULT CHARSET=utf8;"; require_once(ABSPATH . 'wp-admin/includes/upgrade.php'); dbDelta($sql_create); } ?>
Enjoy,
Fran.