Skip to content

Instantly share code, notes, and snippets.

@radist2s
Last active December 24, 2015 06:09
Show Gist options
  • Save radist2s/6755642 to your computer and use it in GitHub Desktop.
Save radist2s/6755642 to your computer and use it in GitHub Desktop.
WordPress integration for VK or other social via url prefix
<?php
new WP_VK_Templater();
/**
* Class WP_VK_Templater
*
* Add 'vk/' prefix to al links, and load for such links templates
* with '-vk' suffix: index-vk.php, page-vk.php, taxonomy-{name}-vk.php, etc.
*/
class WP_VK_Templater
{
const PREFIX = 'vk';
function __construct()
{
add_filter('rewrite_rules_array', array(&$this, 'rewrite_rules_array_filter'), 100);
add_filter('query_vars', array(&$this, 'query_vars_filter'));
add_action('wp_loaded', array(&$this, 'wp_loaded_action'));
add_action('template_redirect', array(&$this, 'template_redirect_action'));
}
function rewrite_rules_array_filter($rules)
{
$prefix = static::PREFIX;
$vk_rules = array();
$vk_rules["^$prefix/?\$"] = "index.php?is_$prefix=1";
foreach ($rules AS $rule => $query)
{
$vk_rules["$prefix/$rule"] = rtrim($query, '&=') . "&is_$prefix=1";
}
$rules = array_merge($vk_rules, $rules);
return $rules;
}
function wp_loaded_action()
{
$prefix = static::PREFIX;
$rules = get_option('rewrite_rules');
if (!isset($rules["^$prefix/?\$"]))
{
global $wp_rewrite;
$wp_rewrite->flush_rules();
}
}
function query_vars_filter($vars)
{
$prefix = static::PREFIX;
array_push($vars, "is_$prefix");
return $vars;
}
function permalink_filter($url)
{
$prefix = static::PREFIX;
$site_url = trailingslashit(get_option('home'));
$url = preg_replace("~$site_url~iu", $site_url . "$prefix/", $url);
return $url;
}
function template_redirect_action()
{
$prefix = static::PREFIX;
if(!get_query_var("is_$prefix"))
{
return;
}
add_filter('template_include', function ($template_include)
{
$prefix = static::PREFIX;
//Try load template with VK suffix
//index-vk.php, page-vk.php, taxonomy-{name}-vk.php, etc.
$vk_template = trailingslashit(dirname($template_include)) . basename($template_include, '.php') . "-$prefix.php";
return is_file($vk_template) ? $vk_template : $template_include;
}, 100, 1);
add_filter('post_link', array(&$this, 'permalink_filter'), 1, 2);
add_filter('post_type_link', array(&$this, 'permalink_filter'), 1, 2);
add_filter('page_link', array(&$this, 'permalink_filter'), 1, 2);
add_filter('tag_link', array(&$this, 'permalink_filter'), 1, 2);
add_filter('term_link', array(&$this, 'permalink_filter'), 1, 2);
//VK home_url prefix
add_filter('home_url', function ($url, $path)
{
$prefix = static::PREFIX;
$site_url = trailingslashit(get_option('home'));
// apply this for home url - not for posts or page permalinks since this filter is called there too
if (did_action('template_redirect') AND trailingslashit($url) == $site_url)
{
$url = $site_url . "$prefix/";
}
if ($path == '/')
{
$url = $site_url . "$prefix/";
}
return $url;
}, 20, 2);
}
}
/**
* Return site base url
*
* @param string $path
* @return string
*/
function site_base_url($path = '')
{
static $base_url;
if ($base_url === null)
{
$base_url = untrailingslashit(get_option('home'));
}
return $path ? $base_url . '/' . ltrim($path, '/') : $base_url;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment