Last active
August 29, 2015 14:02
-
-
Save unr/7c4ba0d10dae24791161 to your computer and use it in GitHub Desktop.
An expanded example of using hooks and classes in WordPress.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| /** | |
| * Our main class for our theme. | |
| * | |
| * This will act as our controller, and activate other hooks/functions for wordpress. | |
| */ | |
| class Theme { | |
| // Called by our bootstrap_theme function | |
| public static function init() { | |
| // Our theme will modify wp queries | |
| self::modify_wp_queries(); | |
| // Our theme will modify $post objects | |
| self::modify_wp_post_object(); | |
| } | |
| /** | |
| * Modify wordpress queries | |
| */ | |
| public static function modify_wp_queries() { | |
| require_once locate_template('/lib/theme/queries.php'); | |
| ThemeQueries::init(); | |
| } | |
| /** | |
| * Add Custom fields to posts | |
| */ | |
| public static function modify_wp_post_object() { | |
| require_once locate_template('/lib/theme/posts.php'); | |
| ThemePosts::init(); | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| /** | |
| * Our functions file. We're hooking into the wp after_setup_theme | |
| * hook to bootstrap our theme, instead of just firing everything | |
| * all willy-nilly. | |
| */ | |
| // Bootstrap our theme, this is called by the after_setup_theme hook | |
| function bootstrap_theme { | |
| // Include our class file, which we use to control our theme | |
| require_once locate_template('/lib/theme/class.php'); | |
| Theme::init(); | |
| } | |
| add_action( 'after_setup_theme', 'bootstrap_theme' ); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| /** | |
| * Posts.php | |
| * | |
| * This class is called by class.php | |
| * | |
| * It will hook into the_post() to make post modifications. | |
| */ | |
| class ThemePosts { | |
| public static function init() { | |
| // __CLASS__ allows us to reference this class, in the callback | |
| add_action('the_post', array(__CLASS__, 'add_custom_fields')); | |
| } | |
| /** | |
| * Add custom post meta to posts | |
| * | |
| * This function is called by the the_post() hook. | |
| */ | |
| public static function add_custom_fields( $post ) { | |
| $post->fields = get_post_meta($post->ID); | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| /** | |
| * Queries.php | |
| * | |
| * This class is called by class.php | |
| * | |
| * It will hook into pre_get_posts to make post modifications. | |
| */ | |
| class ThemeQueries { | |
| public static function init() { | |
| // __CLASS__ allows us to reference this class, in the callback | |
| add_action('pre_get_posts', array(__CLASS__, 'modify_home_query')); | |
| } | |
| /** | |
| * Modify Homepage Query to custom post type | |
| * | |
| * This function is called by the pre_get_posts hook. | |
| */ | |
| public static function modify_home_query( $wp_query ) { | |
| if ($wp_query->is_main_query() && is_home() ) { | |
| $wp_query->set('post_type', 'custom'); | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment