Skip to content

Instantly share code, notes, and snippets.

@offroadkev
Created October 28, 2012 04:03
Show Gist options
  • Save offroadkev/3967448 to your computer and use it in GitHub Desktop.
Save offroadkev/3967448 to your computer and use it in GitHub Desktop.
Customizing AppThemes Vantage Theme Permalink Structure For SEO Friendliness
remove_filter('template_redirect', 'redirect_canonical');
/***************************************************************************
// Adding a new rule
****************************************************************************/
add_filter( 'rewrite_rules_array','kv_listing_rewrite_rules' );
function kv_listing_rewrite_rules( $rules ) {
$newrules = array();
$newrules['listing/(.+?)/(.+?)/(.+?)/page/([0-9]+)?/?$'] = 'index.php?post_type=listing&listing_category=$matches[1]&state=$matches[2]&city=$matches[3]&paged=$matches[4]';
$newrules['listing/(.+?)/(.+?)/page/([0-9]+)?/?$'] = 'index.php?post_type=listing&listing_category=$matches[1]&state=$matches[2]&paged=$matches[3]';
$newrules['listing/(.+?)/page/([0-9]+)?/?$'] = 'index.php?post_type=listing&listing_category=$matches[1]&paged=$matches[2]';
$newrules['listing/(.+?)/(.+?)/(.+?)/(.+?)$'] = 'index.php?post_type=listing&listing_category=$matches[1]&state=$matches[2]&city=$matches[3]&name=$matches[4]';
$newrules['listing/(.+?)/(.+?)/(.+?)$'] = 'index.php?post_type=listing&listing_category=$matches[1]&state=$matches[2]&city=$matches[3]';
$newrules['listing/(.+?)/(.+?)$'] = 'index.php?post_type=listing&listing_category=$matches[1]&state=$matches[2]';
$newrules['listing/(.+?)$'] = 'index.php?post_type=listing&listing_category=$matches[1]';
return $newrules + $rules;
}
/***************************************************************************
// Adding the state city var so that WP recognizes it
****************************************************************************/
add_filter( 'query_vars','kv_query_vars' );
function kv_query_vars( $vars ) {
array_push($vars, 'state');
array_push($vars, 'city');
return $vars;
}
/***************************************************************************
// Adding rewrite tags
****************************************************************************/
add_action('generate_rewrite_rules', 'kv_add_rewrite_tags');
function kv_add_rewrite_tags(){
global $wp_rewrite;
$wp_rewrite->add_rewrite_tag( '%state%', '([^/]+)', 'state=');
$wp_rewrite->add_rewrite_tag( '%city%', '([^/]+)', 'city=');
}
/***************************************************************************
// Altering the permalinks
****************************************************************************/
add_filter('post_type_link', 'kv_post_link', 10 , 3);
function kv_post_link($permalink, $post, $leavename){
if($post->post_type == 'listing'){
if($post->post_status == 'publish'){
$url_base = get_bloginfo('url');
$listingcat = get_the_terms($post->ID, 'listing_category');
if(!empty($listingcat)){
$listingcat = array_pop($listingcat);
$state = get_post_meta($post->ID, 'state-slug', true);
$city = get_post_meta($post->ID, 'city-slug', true);
if(($state != false) && ($city != false)){
$preview_link = $url_base . "/listing/" . $listingcat->slug . "/" . $state . "/" . $city . '/' . $post->post_name;
$permalink = $preview_link;
}else{
return $permalink;
}
}else{
return $permalink;
}
}
}
return $permalink;
}
/***************************************************************************
Redirect files based on matched query
****************************************************************************/
add_filter('template_include', 'kv_template_redirect_files');
function kv_template_redirect_files($templates = ""){
global $wp, $wp_query, $wpdb;
if($wp->matched_rule == 'listing/(.+?)/(.+?)/(.+?)$' /*city*/){
if(!is_array($templates) && !empty($templates)) {
$templates = locate_template(array("meta-city.php", $templates),false);
}
elseif(empty($templates)) {
$templates = locate_template("meta-city.php",false);
}
else {
$new_template = locate_template(array("meta-city.php"));
if(!empty($new_template)) array_unshift($templates,$new_template);
}
}else if($wp->matched_rule == 'listing/(.+?)/(.+?)$' /*state*/){
if(!is_array($templates) && !empty($templates)) {
$templates = locate_template(array("meta-state.php", $templates),false);
}
elseif(empty($templates)) {
$templates = locate_template("meta-state.php",false);
}
else {
$new_template = locate_template(array("meta-state.php"));
if(!empty($new_template)) array_unshift($templates,$new_template);
}
}else if($wp->matched_rule == 'listing/(.+?)/(.+?)/(.+?)/(.+?)$' /*state&city&name*/){
if(!is_array($templates) && !empty($templates)) {
$templates = locate_template(array("meta-listing.php", $templates),false);
}
elseif(empty($templates)) {
$templates = locate_template("meta-listing.php",false);
}
else {
$new_template = locate_template(array("meta-listing.php"));
if(!empty($new_template)) array_unshift($templates,$new_template);
}
}
return $templates;
}
<?php
add_action( 'wp_loaded', 'va_handle_listing_form' );
add_action( 'va_listing_validate_fields', 'va_validate_listing_title' );
add_action( 'va_listing_validate_fields', 'va_validate_listing_category' );
add_filter( 'va_handle_update_listing', 'va_validate_update_listing' );
add_action( 'appthemes_notices', 'va_listing_error_notice' );
function va_handle_listing_form() {
global $va_options;
if ( !isset( $_POST['action'] ) || ( 'new-listing' != $_POST['action'] && 'edit-listing' != $_POST['action'] ) )
return;
if ( !current_user_can( 'edit_listings' ) )
return;
check_admin_referer( 'va_create_listing' );
$listing = va_handle_update_listing();
if ( ! $listing ) {
// there are errors, redirect the user to the edit or create listing form
if ( 'edit-listing' == $_POST['action'] ) {
wp_redirect( $_REQUEST['_wp_http_referer'] );
exit;
}
// return to current page
return;
}
if( _va_needs_purchase( $listing ) ){
wp_redirect( va_get_listing_purchase_url( $listing->ID ) );
}else{
if( _va_needs_publish( $listing ) ){
va_maybe_publish_listing( $listing->ID );
}
wp_redirect( get_permalink( $listing->ID ) );
}
exit;
}
function va_handle_update_listing() {
$listing_cat = va_get_listing_cat_id();
$args = wp_array_slice_assoc( $_POST, array( 'ID', 'post_title', 'post_content', 'tax_input' ) );
$errors = apply_filters( 'va_listing_validate_fields', va_get_listing_error_obj() );
if( $errors->get_error_codes() ){
return false;
}
$args['post_type'] = VA_LISTING_PTYPE;
if ( !(bool) get_post( $_POST['ID'] ) ) {
$listing_id = wp_insert_post( $args );
} else {
$listing_id = wp_update_post( $args );
}
wp_set_object_terms( $listing_id, (int) $listing_cat, VA_LISTING_CATEGORY );
/*echo("<pre style='background:lavenderblush; color:purple; overflow:auto;'>");
var_dump($_POST);
echo("</pre>"); die();*/
/*************************************************
//Turning your custom fields into post-meta data
**************************************************/
if(!empty($_POST['app_whatisthelargestcityyouservice'])){
update_post_meta( $listing_id, 'city-slug', sanitize_title( $_POST['app_whatisthelargestcityyouservice'] ) );
}
if(!empty($_POST['app_stateyourbusinessislocatedin'])){
update_post_meta( $listing_id, 'state-slug', sanitize_title( $_POST['app_stateyourbusinessislocatedin'] ) );
}
/****************************************************
//Back to the AppThemes Vantage Original Coding
*****************************************************/
foreach ( va_get_listing_contact_fields() as $field ) {
update_post_meta( $listing_id, $field, strip_tags( _va_get_initial_field_value( $field ) ) );
}
va_update_form_builder( $listing_cat, $listing_id );
appthemes_set_coordinates( $listing_id, $_POST['lat'], $_POST['lng'] );
va_handle_files( $listing_id, $listing_cat );
return apply_filters('va_handle_update_listing', get_post( $listing_id) );
}
// validates the listing data and returns the post if there are no errors. In case of errors, returns false
function va_validate_update_listing( $listing ) {
$errors = va_get_listing_error_obj();
if ( $errors->get_error_codes( )) {
set_transient('va-errors', $errors );
$listing = false;
}
return $listing;
}
function _va_needs_purchase( $listing ){
global $va_options;
return _va_needs_publish( $listing ) && $va_options->listing_charge;
}
function _va_needs_publish( $listing ){
return in_array( $listing->post_status, array( 'draft', 'expired' ));
}
function _va_is_claimable( $listing_id = '' ) {
$listing_id = !empty( $listing_id ) ? $listing_id : get_the_ID();
$claimable = get_post_meta( $listing_id, 'listing_claimable', true );
if ( empty( $claimable ) ) return false;
return true;
}
function va_validate_listing_title( $errors ){
$args = wp_array_slice_assoc( $_POST, array( 'ID', 'post_title', 'post_content', 'tax_input' ) );
if ( empty( $args['post_title'] ) )
$errors->add( 'no-title', 'No title was submitted.' );
return $errors;
}
function va_validate_listing_category( $errors ){
$listing_cat = va_get_listing_cat_id();
if ( !$listing_cat )
$errors->add( 'wrong-cat', 'No category was submitted.' );
return $errors;
}
function va_update_form_builder( $listing_cat, $listing_id ) {
$fields = va_get_fields_for_cat( $listing_cat );
$to_update = scbForms::validate_post_data( $fields );
scbForms::update_meta( $fields, $to_update, $listing_id );
}
function va_get_listing_cat_id() {
static $cat_id;
if ( is_null( $cat_id ) ) {
if ( isset( $_REQUEST[VA_LISTING_CATEGORY] ) && $_REQUEST[VA_LISTING_CATEGORY] != -1 ) {
$listing_cat = get_term( $_REQUEST[VA_LISTING_CATEGORY], VA_LISTING_CATEGORY );
$cat_id = is_wp_error( $listing_cat ) ? false : $listing_cat->term_id;
} else {
$cat_id = false;
}
}
return $cat_id;
}
function the_listing_tags_to_edit( $listing_id ) {
$tags = get_the_terms( $listing_id, VA_LISTING_TAG );
if ( empty( $tags ) )
return;
echo esc_attr( implode( ', ', wp_list_pluck( $tags, 'name' ) ) );
}
function va_get_default_listing_to_edit() {
require ABSPATH . '/wp-admin/includes/post.php';
$listing = get_default_post_to_edit( VA_LISTING_PTYPE );
$listing->category = va_get_listing_cat_id();
foreach ( array( 'post_title', 'post_content' ) as $field ) {
$listing->$field = _va_get_initial_field_value( $field );
}
foreach ( va_get_listing_contact_fields() as $field ) {
$listing->$field = _va_get_initial_field_value( $field );
}
return $listing;
}
function _va_get_initial_field_value( $field ) {
return isset( $_POST[$field] ) ? stripslashes( $_POST[$field] ) : '';
}
function va_get_existing_listing_to_edit() {
$listing = get_queried_object();
$listing->category = get_the_listing_category( $listing->ID )->term_id;
foreach ( va_get_listing_contact_fields() as $field ) {
$listing->$field = get_post_meta( $listing->ID, $field, true );
}
return $listing;
}
function va_get_listing_contact_fields() {
return array( 'phone', 'address', 'website', 'twitter', 'facebook' );
}
function va_get_listing_error_obj(){
static $errors;
if ( !$errors ){
$errors = new WP_Error();
}
return $errors;
}
function va_listing_error_notice() {
$errors = va_get_listing_error_obj();
if ( ! $errors )
return;
// look for transient errors and merge them if they exist
$transient_errors = get_transient('va-errors');
if ( $transient_errors && $transient_errors->get_error_codes() ) {
$errors->errors = array_merge( $errors->errors, $transient_errors->errors );
delete_transient('va-errors');
}
$map = array(
'no-title' => __( 'The listing must have a title.', APP_TD ),
'wrong-cat' => __( 'The selected category does not exist.', APP_TD ),
);
foreach( $errors->get_error_messages() as $message )
appthemes_display_notice( 'error', $message );
}
<?php
// Template Name: City
?>
<div id="main" class="list">
<div class="section-head">
<h1><?php _e( 'Businesses', APP_TD ); ?></h1>
</div>
<?php
if ( $featured = va_get_featured_listings() ) :
while ( $featured->have_posts() ) : $featured->the_post();
?>
<article id="post-<?php the_ID(); ?>" <?php post_class( 'featured' ); ?>>
<div class="featured-head">
<h3><?php _e( 'Featured', APP_TD ); ?></h3>
</div>
<?php get_template_part( 'content-listing' ); ?>
</article>
<?php
endwhile;
endif;
if ( $featured || is_page() ) :
$args = $wp_query->query;
// The template is loaded as a page, not as an archive
if ( is_page() )
$args['post_type'] = VA_LISTING_PTYPE;
// Don't want to show featured listings a second time
if ( $featured )
$args['post__not_in'] = wp_list_pluck( $featured->posts, 'ID' );
$args['posts_per_page'] = $va_options->listings_per_page;
query_posts( $args );
endif;
$args = array(
'numberposts' => -1,
'listing_category' => $wp_query->query_vars['listing_category'],
'post_type' => 'listing',
'post_status' => 'publish',
'meta_query' => array(
'relation' => 'AND',
array(
'key' => 'state-slug',
'value' => $wp_query->query_vars['state']
),
array(
'key' => 'city-slug',
'value' => $wp_query->query_vars['city']
)
)
);
query_posts( $args );
if ( have_posts() ) : ?>
<?php if ( is_search() ) : ?>
<article class="listing">
<h2><?php printf( __( 'Listings found for "%s" near "%s"', APP_TD ), va_get_search_query_var( 'ls' ), va_get_search_query_var( 'location' ) ); ?></h2>
</article>
<?php endif; ?>
<?php while ( have_posts() ) : the_post(); ?>
<article id="post-<?php the_ID(); ?>" <?php post_class(); ?>>
<?php get_template_part( 'content-listing' ); ?>
</article>
<?php endwhile; ?>
<?php elseif ( !$featured ) : ?>
<?php if ( is_search() ) : ?>
<article class="listing">
<h2><?php printf( __( 'Sorry, No Listings Found For "%s" Near "%s"', APP_TD ), va_get_search_query_var( 'ls' ), va_get_search_query_var( 'location' ) ); ?></h2>
</article>
<?php elseif ( is_archive() ) : ?>
<article class="listing">
<h2><?php printf( __( 'Sorry there are no listings for %s "%s"', APP_TD ), ( is_tax( VA_LISTING_CATEGORY ) ? __( 'category', APP_TD ) : __( 'tag', APP_TD ) ), single_term_title( '', false ) ); ?></h2>
</article>
<?php endif; ?>
<?php endif; ?>
<?php if ( $wp_query->max_num_pages > 1 ) : ?>
<nav class="pagination">
<?php appthemes_pagenavi(); ?>
</nav>
<?php endif; ?>
</div>
<div id="sidebar">
<?php get_sidebar( app_template_base() ); ?>
</div>
<?php
// Template Name: State
?>
<div id="main" class="list">
<div class="section-head">
<h1><?php _e( 'Businesses', APP_TD ); ?></h1>
</div>
<?php
if ( $featured = va_get_featured_listings() ) :
while ( $featured->have_posts() ) : $featured->the_post();
?>
<article id="post-<?php the_ID(); ?>" <?php post_class( 'featured' ); ?>>
<div class="featured-head">
<h3><?php _e( 'Featured', APP_TD ); ?></h3>
</div>
<?php get_template_part( 'content-listing' ); ?>
</article>
<?php
endwhile;
endif;
if ( $featured || is_page() ) :
$args = $wp_query->query;
// The template is loaded as a page, not as an archive
if ( is_page() )
$args['post_type'] = VA_LISTING_PTYPE;
// Don't want to show featured listings a second time
if ( $featured )
$args['post__not_in'] = wp_list_pluck( $featured->posts, 'ID' );
$args['posts_per_page'] = $va_options->listings_per_page;
query_posts( $args );
endif;
$args = array(
'numberposts' => -1,
'listing_category' => $wp_query->query_vars['listing_category'],
'post_type' => 'listing',
'post_status' => 'publish',
'meta_query' => array(
array(
'key' => 'state-slug',
'value' => $wp_query->query_vars['state']
)
)
);
query_posts( $args );
if ( have_posts() ) : ?>
<?php if ( is_search() ) : ?>
<article class="listing">
<h2><?php printf( __( 'Listings found for "%s" near "%s"', APP_TD ), va_get_search_query_var( 'ls' ), va_get_search_query_var( 'location' ) ); ?></h2>
</article>
<?php endif; ?>
<?php while ( have_posts() ) : the_post(); ?>
<article id="post-<?php the_ID(); ?>" <?php post_class(); ?>>
<?php
/*$colors = array('lightblue', 'lightgreen', 'peachpuff');
$ran = array_rand($colors);
echo "<pre style='background:" . $colors[$ran] . "; color:darkblue;'>";
var_export(get_post_meta($post->ID));
echo "</pre>";*/
?>
<?php get_template_part( 'content-listing' ); ?>
</article>
<?php endwhile; ?>
<?php elseif ( !$featured ) : ?>
<?php if ( is_search() ) : ?>
<article class="listing">
<h2><?php printf( __( 'Sorry, No Listings Found For "%s" Near "%s"', APP_TD ), va_get_search_query_var( 'ls' ), va_get_search_query_var( 'location' ) ); ?></h2>
</article>
<?php elseif ( is_archive() ) : ?>
<article class="listing">
<h2><?php printf( __( 'Sorry there are no listings for %s "%s"', APP_TD ), ( is_tax( VA_LISTING_CATEGORY ) ? __( 'category', APP_TD ) : __( 'tag', APP_TD ) ), single_term_title( '', false ) ); ?></h2>
</article>
<?php endif; ?>
<?php endif; ?>
<?php if ( $wp_query->max_num_pages > 1 ) : ?>
<nav class="pagination">
<?php appthemes_pagenavi(); ?>
</nav>
<?php endif; ?>
</div>
<div id="sidebar">
<?php get_sidebar( app_template_base() ); ?>
</div>
@neggga
Copy link

neggga commented Apr 24, 2013

Hi,
could you please help me how can I use this solution in a child-theme for vantage?
Thanks
neggga

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment