Last active
November 1, 2024 14:27
-
-
Save jeremyfelt/a4650a70e33eb54d6bcd to your computer and use it in GitHub Desktop.
Pullman WordPress Meetup - Powerful Code Snippets
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 | |
| // Powerful Code Snippets (will there be 10?) | |
| add_action( 'init', 'puwwp_register_post_type' ); | |
| /** | |
| * Registering a custom post type provides many out of | |
| * the box defaults new types of content. | |
| */ | |
| function puwwp_register_post_type() { | |
| $labels = array(); // array of labels | |
| $args = array( | |
| 'labels' => $labels, | |
| 'description' => 'WordPress Meetups', | |
| 'public' => true, | |
| 'hierarchical' => false, | |
| 'menu_icon' => 'dashicons-heart', | |
| 'supports' => array ( | |
| 'title', | |
| 'editor', | |
| 'revisions', | |
| 'thumbnail', | |
| ), | |
| 'has_archive' => true, | |
| 'rewrite' => array( | |
| 'slug' => 'meetups', | |
| 'with_front' => false | |
| ), | |
| ); | |
| // https://example.org/meetups/february-meetup/ | |
| // https://example.org/meetups/ | |
| // The post type slug can be up to 20 characters. | |
| register_post_type( 'puwwp-meetup', $args ); | |
| } | |
| add_action( 'add_meta_boxes', 'puwwp_add_meetup_meta_boxes', 10, 1 ); | |
| /** | |
| * Add meta boxes to the current view. | |
| */ | |
| function puwwp_add_meetup_meta_boxes() { | |
| add_meta_box( 'puwwp-meetup-meta', 'Meetup Data', 'puwwp_display_meetup_meta', 'puwwp-meetup' ); | |
| } | |
| /** | |
| * Display the meta box that was added above. | |
| */ | |
| function puwwp_display_meetup_meta( $post ) { | |
| // A very powerful feature in WordPress is nonce management. | |
| wp_nonce_field( 'save-extra-meetup-data', '_puwwp_extra_nonce' ); | |
| // Post meta stores extra data about an item in a table. | |
| $extra_data = get_post_meta( $post->ID, 'puwwp_extra_data', true ); | |
| ?> | |
| <label for="puwwp-extra-data">Extra Data:</label> | |
| <p class="description">This is some extra data about the meetup.</p> | |
| <textarea id="puwwp-extra-data" name="puwwp_extra_data"><?php echo esc_textarea( $extra_data ); ?></textarea> | |
| <?php | |
| } | |
| add_action( 'save_post', 'puwwp_save_meetup_meta', 10, 2 ); | |
| /** | |
| * Whenever a post is saved, make sure the meta data is handled | |
| * as well. | |
| */ | |
| function puwwp_save_meetup_meta( $post_id, $post ) { | |
| if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) { | |
| return; | |
| } | |
| if ( ! isset( $_POST['_puwwp_extra_nonce'] ) | |
| || false === wp_verify_nonce( $_POST['_puwwp_extra_nonce'], 'save-extra-meetup-data' ) ) { | |
| return; | |
| } | |
| if ( 'auto-draft' === $post->post_status ) { | |
| return; | |
| } | |
| // Update or delete post meta and take advantage of wp_kses_post | |
| // when storing it. | |
| if ( isset( $_POST['puwwp_extra_data'] ) && '' !== trim( $_POST['puwwp_extra_data'] ) ) { | |
| update_post_meta( $post_id, 'puwwp_extra_data', wp_kses_post( $_POST['wsuwp_extra_data'] ) ); | |
| } else { | |
| delete_post_meta( $post_id, 'puwwp_extra_data' ); | |
| } | |
| } | |
| add_filter( 'manage_puwwp-meetup_columns', 'puwwp_manage_meetup_columns' ); | |
| /** | |
| * Add an extra column to the list table showing meetups that | |
| * contains the extra data being stored. | |
| */ | |
| function puwwp_manage_meetup_columns( $posts_columns ) { | |
| $posts_columns['extra_data'] = 'Extra Data'; | |
| return $posts_columns; | |
| } | |
| add_action( 'manage_puwwp-meetup_custom_column', 'puwwp_manage_meetup_custom_columns', 10, 2 ); | |
| /** | |
| * When the extra column is displayed, retrieve the post meta that is | |
| * stored for the specific post. | |
| */ | |
| function puwwp_manage_meetup_custom_columns( $column_name, $post_id ) { | |
| if ( 'extra_data' === $column_name ) { | |
| $extra_data = get_post_meta( $post_id, 'puwwp_extra_data', true ); | |
| echo esc_html( $extra_data ); | |
| } | |
| } | |
| add_action( 'init', 'puwwp_register_taxonomy' ); | |
| /** | |
| * Registering a custom taxonomy also provides many things out of the box | |
| * for a new way of categorizing content. | |
| */ | |
| function puwwp_register_taxonomy() { | |
| $args = array( | |
| 'labels' => array( 'name' => 'Meetup Type' ), | |
| 'hierarchical' => true, | |
| 'show_ui' => true, | |
| 'show_admin_column' => true, | |
| 'query_var' => true, | |
| 'rewrite' => array( 'slug' => 'meetup-type' ), | |
| ); | |
| // https://example.org/meetup-type/presentation/ | |
| // The taxonomy slug can be up to 20 characters. | |
| register_taxonomy( 'puwwp-meetup-type', $args ); | |
| } | |
| add_action( 'wp_head', 'puwp_output_header_data' ); | |
| /** | |
| * The wp_head action can be used for outputting any sort of | |
| * data that should exist in <head></head> in your HTML. | |
| */ | |
| function puwp_output_header_data() { | |
| ?> | |
| <meta property="og:title" content="WordPress Meetups" /> | |
| <meta property="og:description" content="This is a listing of our WordPress Meetups" /> | |
| <?php | |
| } | |
| add_action( 'pre_get_posts', 'puwwp_modify_query', 10, 1 ); | |
| /** | |
| * pre_get_posts is a powerful action as it allows you to hook in | |
| * immediately before a query is performed to alter the query. | |
| */ | |
| function puwwp_modify_query( $query ) { | |
| // Only show 2 posts per page when viewing a meetup type archive. | |
| if ( $query->is_main_query() && $query->is_tax( 'puwwp-meetup-type' ) ) { | |
| $query->set( 'posts_per_page', 2 ); | |
| } | |
| // Sort an archive of meetups by the extra data. | |
| if ( $query->is_main_query() && $query->is_post_type_archive( 'puwwp-meetup' ) ) { | |
| $query->set( 'meta_key', 'puwwp_extra_data' ); | |
| $query->set( 'orderby', 'meta_value' ); | |
| $query->set( 'order', 'ASC' ); | |
| } | |
| } | |
| add_filter( 'body_class', 'puwwp_add_body_classes', 10, 1 ); | |
| /** | |
| * Several filters like body_class exist as a way to tweak small | |
| * parts of code for big results. | |
| */ | |
| function puwwp_add_body_classes( $classes ) { | |
| // Add an extra class to <body> if the meetup has extra data. | |
| if ( is_singular( 'puwwp-meetup' ) ) { | |
| if ( get_post_meta( get_the_ID(), 'puwwp_extra_data', true ) ) { | |
| $classes[] = 'has-extra-data'; | |
| } | |
| } | |
| return $classes; | |
| } | |
| add_action( 'after_setup_theme', 'puwwp_additional_image_sizes', 10 ); | |
| /** | |
| * Harnessing post thumbnails (featured images) and different image | |
| * sizes is much easier than it may seem. | |
| */ | |
| function puwwp_additional_image_sizes() { | |
| add_theme_support( 'post-thumbnails' ); | |
| set_post_thumbnail_size( 198, 198, true ); | |
| add_image_size( 'puwwp-small', 400, 99999 ); | |
| add_image_size( 'puwwp-medium', 800, 99999 ); | |
| add_image_size( 'puwwp-large', 1200, 99999 ); | |
| add_image_size( 'puwwp-xlarge', 1600, 99999 ); | |
| } | |
| // What do you want to see? |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment