wp plugin -10
Commonly Used Action Hooks
(1)plugins_loaded。一个WP插件应该在此hook做setup。
It is fired after most of the WordPress files are loaded but before the pluggable functions and WordPress starts executing anything;plugins_loaded is executed when all the user’s activated plugins have been loaded by WordPress. It is also the earliest hook plugin developers can use in the loading process.
< ?php
add_action( ‘plugins_loaded’, ‘boj_footer_message_plugin_setup’ );
function boj_footer_message_plugin_setup() {
/* Add the footer message action. */
add_action( ‘wp_footer’, ‘boj_example_footer_message’, 100 );
}
function boj_example_footer_message() {
echo ‘This site is built using < a href=”http://wordpress.org”
title=”WordPress publishing platform” > WordPress < /a > .’;
}
? >
(2)init。The init hook is fired after most of WordPress is set up。
(3)admin_menu。The admin_menu hook is called only when an administration page loads。
< ?php
add_action( ‘admin_menu’, ‘boj_admin_settings_page’ );
function boj_admin_settings_page() {
add_options_page(
‘BOJ Settings’,
‘BOJ Settings’,
‘manage_options’,
‘boj_admin_settings’,
‘boj_admin_settings_page’
);
}
? >
(4)template_redirect。it’s the point where WordPress knows which page a user is viewing. It is executed just before the theme template is chosen for the particular page view.
< ?php
add_action( ‘template_redirect’, ‘boj_singular_post_css’ );
function boj_singular_post_css() {
if ( is_singular( ‘post’ ) ) {
wp_enqueue_style(
‘boj-singular-post’,
‘boj-example.css’,
false,
0.1,
‘screen’
);
}
}
? >
(5)wp_head。On the front end of the site, WordPress themes call the wp_head() function, which fi res the wp_head hook. Plugins use this hook to add HTML between the opening < head > tag and its closing < /head > .
< ?php
add_action( ‘wp_head’, ‘boj_front_page_meta_description’ );
function boj_front_page_meta_description() {
/* Get the site description. */
$description = esc_attr( get_bloginfo( ‘description’ ) );
/* If a description is set, display the meta element. */
if ( !empty( $description ) )
echo ‘ < meta name=”description” content=”’ . $description . ‘” / > ’;
}
? >
js不应该使用此hook,而应该使用wp_enqueue_script。除非是js不在另一单独分离的文件中。
浙公网安备 33010602011771号