Upgrade to Pro — share decks privately, control downloads, hide ads and more …

From Web Address to Web Page – the Humble URL i...

From Web Address to Web Page – the Humble URL in WordPress

Once upon a time we passed parameters to create web pages, now we use pretty URLs and (hopefully) think through our URLs carefully. URLs are one of the fundamental commands a user can give WordPress, and they can be poetic, powerful and precise all at once.

This talk will cover how URLs are turned into WP_Query parameters, what URL endpoints are and when to use them, how URLs can be attractive and amusing, and how to use URLs across different post type structures.

Here's the code I use throughout the presentation, as a plugin to download and slice and dice: https://github.com/simonwheatley/wcldn-web-addresses

Simon Wheatley

March 21, 2015
Tweet

More Decks by Simon Wheatley

Other Decks in Programming

Transcript

  1. SELECT wp_posts.* FROM wp_posts WHERE 1=1 AND ( ( YEAR(

    wp_posts.post_date ) = 2015 AND MONTH( wp_posts.post_date ) = 3 ) ) AND wp_posts.post_name = 'hello-world' AND wp_posts.post_type = 'post' ORDER BY wp_posts.post_date DESC
  2. $rewrite = array( 'slug' => ‘worked-for‘, ); $args = array(

    'labels' => $labels, 'has_archive' => 'our-work', 'public' => true, 'menu_icon' => 'dashicons-blahs', 'rewrite' => $rewrite, ); register_post_type( 'sw_team', $args );
  3. Get all the posts
 Where the post type is “sw_client”


    And the name of the posts are “dyson”

  4. SELECT wp_posts.* FROM wp_posts WHERE 1=1 AND wp_posts.post_name = '$1'

    AND wp_posts.post_type = 'sw_client' ORDER BY wp_posts.post_date DESC
  5. // Called on request filter function sw_fix_endpoint_reqs( $vars ) {

    if ( isset( $vars['sw_json'] ) ) { $vars['sw_json'] = true; } return $vars; }
  6. if ( get_query_var( ‘sw_json’ ) ) { 
 // Done

    some stuff, switch content, etc }
  7. // Called on request filter
 $names_to_compare = explode( '+', $vars['sw_compare']

    ); foreach ( $names_to_compare as $name ) { $args = array( 'post_type' => 'sw_client', 'name' => $name, 'fields' => 'ids', ); $query = new WP_Query( $args ); $post_ids = array_merge( $query->posts, $post_ids ); } $vars['post__in'] = $post_ids; $vars['post_type'] = 'sw_client';
  8. // Called on request filter
 $names_to_compare = explode( '+', $vars['sw_compare']

    ); foreach ( $names_to_compare as $name ) { $args = array( 'post_type' => 'sw_client', 'name' => $name, 'fields' => 'ids', ); $query = new WP_Query( $args ); $post_ids = array_merge( $query->posts, $post_ids ); } $vars['post__in'] = $post_ids; $vars['post_type'] = 'sw_client';
  9. // Called on request filter
 $names_to_compare = explode( '+', $vars['sw_compare']

    ); foreach ( $names_to_compare as $name ) { $args = array( 'post_type' => 'sw_client', 'name' => $name, 'fields' => 'ids', ); $query = new WP_Query( $args ); $post_ids = array_merge( $query->posts, $post_ids ); } $vars['post__in'] = $post_ids; $vars['post_type'] = 'sw_client';
  10. // Called on request filter
 $names_to_compare = explode( '+', $vars['sw_compare']

    ); foreach ( $names_to_compare as $name ) { $args = array( 'post_type' => 'sw_client', 'name' => $name, 'fields' => 'ids', ); $query = new WP_Query( $args ); $post_ids = array_merge( $query->posts, $post_ids ); } $vars['post__in'] = $post_ids; $vars['post_type'] = 'sw_client';
  11. // Called on request filter
 $names_to_compare = explode( '+', $vars['sw_compare']

    ); foreach ( $names_to_compare as $name ) { $args = array( 'post_type' => 'sw_client', 'name' => $name, 'fields' => 'ids', ); $query = new WP_Query( $args ); $post_ids = array_merge( $query->posts, $post_ids ); } $vars['post__in'] = $post_ids; $vars['post_type'] = 'sw_client';
  12. // Called on request filter
 $names_to_compare = explode( '+', $vars['sw_compare']

    ); foreach ( $names_to_compare as $name ) { $args = array( 'post_type' => 'sw_client', 'name' => $name, 'fields' => 'ids', ); $query = new WP_Query( $args ); $post_ids = array_merge( $query->posts, $post_ids ); } $vars['post__in'] = $post_ids; $vars['post_type'] = 'sw_client';