Intermediate to Advanced Wordpress

From DevSummit
Jump to navigation Jump to search

Registering a custom post type in WordPress

  • A different way of categorizing your posts; there's generally pages and posts. Pages have more hierarchical organization scheme; posts are flat hierarchy.
  • Creating custom post types for different types of content; you can sub-categorize it, extend it with different fields, etc.
  • Also easier for clients: rather than just seeing pages and posts and then using categories to sort them, it gives you a way to keep content well organized.
  • A better way to manage content on a site.
  • Registering a custom post type: http://codex.wordpress.org/Function_Reference/register_post_type
    • Usage: <?php register_post_type( $post_type, $args ) ?>
  • You can hide post types both from the front end (site visitors) and from the WP backend.
  • You can also limit certain roles' capabilities so that they only have access to certain post types
  • http://codex.wordpress.org/Roles_and_Capabilities -- working with roles and capabilities is done via code by default
  • Members plugin is useful for managing roles: http://wordpress.org/plugins/members/ -- gives you a UI for roles and capabilities
  • Grant's WP class for registering custom post types: https://github.com/creativecoder/custom-post-type-helper -- shortcut for handling lots of labels and other things you need to set for custom post types
  • People often register custom post types in the functions.php file of your theme; a bad thing about that is if someone changes their theme, they lose all the post types. Grant liks to put it in a wp-content/mu-plugins. Any PHP files that you drop into that folder will automatically be run by WP core. This is for code you want to run all the time; the only way to turn them off is to delete the files. Then no matter what theme is, that code sticks around. This is core WP functionality. You can just drop each custom post type into its own PHP file in that folder, or one big PHP file, etc.
  • You also create (register) taxonomies within code; once you create the taxonomies in code, you'll see them in your WP UI and can add terms there.
  • Also: http://wordpress.org/plugins/simple-custom-types/

Relating content types

  • E.g. with property and neighborhood post types, you want properties to belong to neighborhoods. No way to relate posts to each other.
  • http://wordpress.org/plugins/posts-to-posts/ is a good plugin for that; excellent documentation in github.
  • You can also use this to connect users to posts (e.g. to connect multiple authors to one post.)

Advanced custom fields

Custom queries in WP