This hook runs after all theme files have been included, but before anything else is loaded into the theme.
Use the appthemes_init
action hook when you want to execute something before the page begins to be rendered.
Usage
This hook provides no parameters. You use this hook by having your function echo output to the browser, or by having it perform background tasks. Your functions shouldn’t return, and shouldn’t take any parameters.
add_action( 'appthemes_init', 'your_function' ); |
Example: Banned Users
If you do not want certain users to access your site, you can create a hook to check for their IP address, and then use the wp_die()
command to stop the page from loading.
function check_banned_user(){
$banned_user_ip = '192.168.1.1';
// If the user's IP matches, end the request and send a message
if( $_SERVER['REMOTE_ADDR'] == $banner_user_ip )
wp_die( __( 'You have been banned.', 'appthemes' ) );
}
add_action_hook("appthemes_init", "check_banned_user"); |
Example: Registering Other Hooks
You can also create a hook to optionally register other hooks. For instance, if you only want to register a hook inside the admin section, you could use the is_admin()
function to decide whether or not to register the hook.
function register_my_admin_hook(){
if( is_admin() ){
register_hook( 'wp_head' , 'my_function' );
}
add_action_hook( 'appthemes_init', 'register_my_admin_hook' ); |
Changelog
Source File
appthemes_init()
is located in includes/theme-functions.php
.
The appthemes_add_submenu_page action hook is triggered within the WordPress admin theme option pages. This hook provides no parameters but must be used in conjunction with the appthemes_add_submenu_page_content function in order to work properly.
appthemes_add_submenu_page(); |
Usage
This example creates a brand new option menu item called, “My Menu Name” within the AppThemes option menu. You will need to paste this code within your functions.php theme file.
function my_custom_appthemes_admin_page() {
add_submenu_page('admin-options.php', 'My Page Title', 'My Menu Name' , 'manage_options', 'my-page-url', 'my_custom_appthemes_admin_page_content');
}
add_action('appthemes_add_submenu_page', 'my_custom_appthemes_admin_page', 10); |
As you can see, this admin hook uses the WordPress function add_submenu_page with the following syntax:
add_submenu_page( $parent_slug, $page_title, $menu_title, $capability, $menu_slug, $function); |
The parameters can all be changed except for the following:
Example
This example puts all the pieces together by using both the appthemes_add_submenu_page and appthemes_add_submenu_page_content functions to create a new admin theme sub-menu, sub-menu page, and saves the options to the WordPress database.
You would need to place this code in your functions.php file.
<?php
// hook into the theme menu and create a sub-menu item
function my_custom_appthemes_admin_page() {
add_submenu_page('admin-options.php', 'My Page Title', 'My Menu Name' , 'manage_options', 'my-page-url', 'my_custom_appthemes_admin_page_content');
}
add_action('appthemes_add_submenu_page', 'my_custom_appthemes_admin_page', 10);
// create the actual page and new field(s)
function my_custom_appthemes_admin_page_content() {
global $app_abbr;
$my_options = array(
array( 'type' => 'tab',
'tabname' => __('Tab Title', 'appthemes')),
array( 'name' => __('Section Title', 'appthemes'),
'type' => 'title',
'desc' => '',
'id' => ''),
array( 'name' => __('Option Name','appthemes'),
'desc' => '',
'tip' => __('Enter your tooltip text here.','appthemes'),
'id' => $app_abbr.'_custom_field_name',
'css' => 'min-width:100px;',
'std' => 'yes',
'vis' => '',
'req' => '',
'js' => '',
'min' => '',
'type' => 'select',
'options' => array( 'yes' => __('Yes', 'appthemes'),
'no' => __('No', 'appthemes'))),
array( 'type' => 'tabend'),
);
// update and save the options in the WordPress database on form submit
appthemes_update_options($my_options);
?>
<div class="wrap">
<div class="icon32" id="icon-tools"><br/></div>
<h2><?php _e('Testing','cp') ?></h2>
<form method="post" id="mainform" action="">
<p class="submit btop"><input name="save" type="submit" value="<?php _e('Save changes','appthemes') ?>" /></p>
<?php appthemes_admin_fields($options_testing); ?>
<p class="submit bbot"><input name="save" type="submit" value="<?php _e('Save changes','appthemes') ?>" /></p>
<input name="submitted" type="hidden" value="yes" />
</form>
</div>
<?php
}
add_action('appthemes_add_submenu_page_content', 'my_custom_appthemes_admin_page_content', 10);
?> |
So what does all that code actually do? It creates a new theme admin sub-menu called “My Menu Name”. When you click on it, you’ll see a new page with one drop-down option called “Option Name”.
If you save the page, you’ll instantly save that admin option in the WordPress options table. Then you can write a function or if statement to execute based on the value.
A simpler test would be to just echo the value by using the WordPress ‘get_option’ function like this:
<?php echo get_option( $app_abbr.'_custom_field_name' ); ?> |
Paste that anywhere in your theme index.php template and you should see either a ‘yes’ or ‘no’ printed on your screen. If not, make sure you’ve actually saved a value and that $app_abbr
is globally set (i.e. global $app_abbr;
).
Changelog
- since appthemes-functions.php version 1.2
Source File
appthemes_add_submenu_page()
is located in includes/admin/admin-options.php
.