Skip to content

Instantly share code, notes, and snippets.

@dbernar1
Last active August 29, 2015 14:04

Revisions

  1. dbernar1 revised this gist Jul 31, 2014. 1 changed file with 5 additions and 10 deletions.
    15 changes: 5 additions & 10 deletions plugin.php
    Original file line number Diff line number Diff line change
    @@ -9,23 +9,19 @@ function get_permalink__dbernar1( $post = null ) {

    add_action( 'init', 'create_post_type_rewrite_rules__dbernar1' );

    function create_post_type_rewrite_rules__dbernar1()
    function create_post_type_rewrite_rules__dbernar1() {
    add_rewrite_tag( '%' . PRODUCT_QUERY_VAR . '%', '([^&]+)' );
    add_rewrite_rule( '^' . PRODUCT_URL_PREFIX .' /([^/]*)/?', 'index.php?' . PRODUCT_QUERY_VAR . '=$matches[1]', 'top' );

    }

    add_action( 'wp', 'show_product_in_current_site__dbernar1' );

    function show_product_in_current_site__dbernar1() {
    $viewing_product = get_query_var( PRODUCT_QUERY_VAR );

    if ( $viewing_product ) {
    allow_theme_to_overwrite_template__dbernar1( $named = 'single-product.php' );
    }
    $viewing_product = get_query_var( PRODUCT_QUERY_VAR );

    if ( $viewing_product ) {
    allow_theme_to_overwrite_template__dbernar1( $named = 'single-product.php' );
    }

    }

    function allow_theme_to_overwrite_template__dbernar1( $template_filename ) {
    @@ -35,5 +31,4 @@ function allow_theme_to_overwrite_template__dbernar1( $template_filename ) {
    load_template( dirname( __FILE__ ) . '/templates/' . $template_filename );
    }
    die;
    }

    }
  2. dbernar1 revised this gist Jul 31, 2014. 1 changed file with 7 additions and 4 deletions.
    11 changes: 7 additions & 4 deletions plugin.php
    Original file line number Diff line number Diff line change
    @@ -1,21 +1,24 @@
    <?php
    define( 'PRODUCT_QUERY_VAR', 'product__dbernar1' );
    define( 'PRODUCT_URL_PREFIX', 'products' );

    function get_permalink__dbernar1( $post = null ) {
    if ( is_null( $post ) ) global $post;
    return site_url( 'products/' . $post->post_name );
    return site_url( PRODUCT_URL_PREFIX . '/' . $post->post_name );
    }

    add_action( 'init', 'create_post_type_rewrite_rules__dbernar1' );

    function create_post_type_rewrite_rules__dbernar1()
    add_rewrite_tag( '%product__dbernar1%', '([^&]+)' );
    add_rewrite_rule( '^products/([^/]*)/?', 'index.php?product__dbernar1=$matches[1]', 'top' );
    add_rewrite_tag( '%' . PRODUCT_QUERY_VAR . '%', '([^&]+)' );
    add_rewrite_rule( '^' . PRODUCT_URL_PREFIX .' /([^/]*)/?', 'index.php?' . PRODUCT_QUERY_VAR . '=$matches[1]', 'top' );

    }

    add_action( 'wp', 'show_product_in_current_site__dbernar1' );

    function show_product_in_current_site__dbernar1() {
    $viewing_product = get_query_var( 'product__dbernar1' );
    $viewing_product = get_query_var( PRODUCT_QUERY_VAR );

    if ( $viewing_product ) {
    allow_theme_to_overwrite_template__dbernar1( $named = 'single-product.php' );
  3. dbernar1 revised this gist Jul 31, 2014. 1 changed file with 4 additions and 4 deletions.
    8 changes: 4 additions & 4 deletions plugin.php
    Original file line number Diff line number Diff line change
    @@ -12,20 +12,20 @@ function create_post_type_rewrite_rules__dbernar1()

    }

    add_action( 'wp', 'show_product_in_current_site' );
    add_action( 'wp', 'show_product_in_current_site__dbernar1' );

    function show_product_in_current_site() {
    function show_product_in_current_site__dbernar1() {
    $viewing_product = get_query_var( 'product__dbernar1' );

    if ( $viewing_product ) {
    allow_theme_to_overwrite_template( $named = 'single-product.php' );
    allow_theme_to_overwrite_template__dbernar1( $named = 'single-product.php' );
    }

    }

    }

    function allow_theme_to_overwrite_template( $template_filename ) {
    function allow_theme_to_overwrite_template__dbernar1( $template_filename ) {
    if ( $overridden_template = locate_template( $template_filename ) ) {
    load_template( $overridden_template );
    } else {
  4. dbernar1 revised this gist Jul 31, 2014. 2 changed files with 36 additions and 13 deletions.
    13 changes: 0 additions & 13 deletions functions.php
    Original file line number Diff line number Diff line change
    @@ -1,13 +0,0 @@
    <?php
    function get_permalink__dbernar1( $post = null ) {
    if ( is_null( $post ) ) global $post;
    return site_url( 'products/' . $post->post_name );
    }

    add_action( 'init', '_fbc_create_agcanada_post_types_rewrite_rules' );

    function _reate_aost_types_rewrite_rules(__dbernar1) {
    add_rewrite_tag( '%product%', '([^&]+)' );
    add_rewrite_rule( '^products/([^/]*)/?', 'index.php?' . $post_type_data[ 'query_var' ] . '=$matches[1]', 'top' );

    }
    36 changes: 36 additions & 0 deletions plugin.php
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,36 @@
    <?php
    function get_permalink__dbernar1( $post = null ) {
    if ( is_null( $post ) ) global $post;
    return site_url( 'products/' . $post->post_name );
    }

    add_action( 'init', 'create_post_type_rewrite_rules__dbernar1' );

    function create_post_type_rewrite_rules__dbernar1()
    add_rewrite_tag( '%product__dbernar1%', '([^&]+)' );
    add_rewrite_rule( '^products/([^/]*)/?', 'index.php?product__dbernar1=$matches[1]', 'top' );

    }

    add_action( 'wp', 'show_product_in_current_site' );

    function show_product_in_current_site() {
    $viewing_product = get_query_var( 'product__dbernar1' );

    if ( $viewing_product ) {
    allow_theme_to_overwrite_template( $named = 'single-product.php' );
    }

    }

    }

    function allow_theme_to_overwrite_template( $template_filename ) {
    if ( $overridden_template = locate_template( $template_filename ) ) {
    load_template( $overridden_template );
    } else {
    load_template( dirname( __FILE__ ) . '/templates/' . $template_filename );
    }
    die;
    }

  5. dbernar1 revised this gist Jul 31, 2014. 1 changed file with 2 additions and 15 deletions.
    17 changes: 2 additions & 15 deletions functions.php
    Original file line number Diff line number Diff line change
    @@ -7,20 +7,7 @@ function get_permalink__dbernar1( $post = null ) {
    add_action( 'init', '_fbc_create_agcanada_post_types_rewrite_rules' );

    function _reate_aost_types_rewrite_rules(__dbernar1) {
    global $_FBC_AGCANADA_POST_TYPES;


    // add_rewrite_tag( '%video%', '([^&]+)' );


    foreach( $_FBC_AGCANADA_POST_TYPES as $slug => $post_type_data ) {
    add_rewrite_tag( '%' . $post_type_data[ 'query_var' ] . '%', '([^&]+)' );

    if ( $post_type_data[ 'has_archive' ] ) {
    add_rewrite_rule( '^' . $post_type_data[ 'url_slug' ] . '/?$', 'index.php?' . $post_type_data[ 'query_var' ] . '=archive', 'top' );
    add_rewrite_rule( '^' . $post_type_data[ 'url_slug' ] . '/page/(\d++)/?$', 'index.php?' . $post_type_data[ 'query_var' ] . '=archive&paged=$matches[1]', 'top' );
    }
    add_rewrite_rule( '^' . $post_type_data[ 'url_slug' ] . '/([^/]*)/?', 'index.php?' . $post_type_data[ 'query_var' ] . '=$matches[1]', 'top' );
    }
    add_rewrite_tag( '%product%', '([^&]+)' );
    add_rewrite_rule( '^products/([^/]*)/?', 'index.php?' . $post_type_data[ 'query_var' ] . '=$matches[1]', 'top' );

    }
  6. dbernar1 revised this gist Jul 31, 2014. 1 changed file with 21 additions and 0 deletions.
    21 changes: 21 additions & 0 deletions functions.php
    Original file line number Diff line number Diff line change
    @@ -2,4 +2,25 @@
    function get_permalink__dbernar1( $post = null ) {
    if ( is_null( $post ) ) global $post;
    return site_url( 'products/' . $post->post_name );
    }

    add_action( 'init', '_fbc_create_agcanada_post_types_rewrite_rules' );

    function _reate_aost_types_rewrite_rules(__dbernar1) {
    global $_FBC_AGCANADA_POST_TYPES;


    // add_rewrite_tag( '%video%', '([^&]+)' );


    foreach( $_FBC_AGCANADA_POST_TYPES as $slug => $post_type_data ) {
    add_rewrite_tag( '%' . $post_type_data[ 'query_var' ] . '%', '([^&]+)' );

    if ( $post_type_data[ 'has_archive' ] ) {
    add_rewrite_rule( '^' . $post_type_data[ 'url_slug' ] . '/?$', 'index.php?' . $post_type_data[ 'query_var' ] . '=archive', 'top' );
    add_rewrite_rule( '^' . $post_type_data[ 'url_slug' ] . '/page/(\d++)/?$', 'index.php?' . $post_type_data[ 'query_var' ] . '=archive&paged=$matches[1]', 'top' );
    }
    add_rewrite_rule( '^' . $post_type_data[ 'url_slug' ] . '/([^/]*)/?', 'index.php?' . $post_type_data[ 'query_var' ] . '=$matches[1]', 'top' );
    }

    }
  7. dbernar1 created this gist Jul 31, 2014.
    5 changes: 5 additions & 0 deletions functions.php
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,5 @@
    <?php
    function get_permalink__dbernar1( $post = null ) {
    if ( is_null( $post ) ) global $post;
    return site_url( 'products/' . $post->post_name );
    }