Skip to content

Commit 4c15892

Browse files
committed
Create custom-fields-permalink.php
1 parent 3a2b7a8 commit 4c15892

File tree

1 file changed

+91
-0
lines changed

1 file changed

+91
-0
lines changed

custom-fields-permalink.php

Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
<?php
2+
/*
3+
Plugin Name: Custom Fields Permalink 2
4+
Plugin URI: http://athlan.pl/custom-fields-permalink
5+
Description: This plugin enable to make a permalink from custom field's value.
6+
Author: Piotr Pelczar
7+
Version: 2.0
8+
Author URI: http://athlan.pl/
9+
*/
10+
11+
class CustomFieldsPermalink {
12+
13+
const PARAM_CUSTOMFIELD_KEY = 'custom_field_key';
14+
const PARAM_CUSTOMFIELD_VALUE = 'custom_field_value';
15+
16+
public static function linkPost($permalink, $post, $leavename) {
17+
return self::linkRewriteFields($permalink, $post);
18+
}
19+
20+
public static function linkPostType($permalink, $post, $leavename, $sample) {
21+
return self::linkRewriteFields($permalink, $post);
22+
}
23+
24+
protected static function linkRewriteFields($permalink, $post) {
25+
return preg_replace('#(%field_(.*?)%)#e', 'CustomFieldsPermalink::linkRewriteFieldsExtract($post, "\\2")', $permalink);
26+
}
27+
28+
public static function linkRewriteFieldsExtract($post, $fieldName) {
29+
$postMeta = get_post_meta($post->ID);
30+
31+
if(!isset($postMeta[$fieldName]))
32+
return '';
33+
34+
return implode('', $postMeta[$fieldName]);
35+
}
36+
37+
public static function registerExtraQueryVars($value) {
38+
array_push($value, self::PARAM_CUSTOMFIELD_KEY, self::PARAM_CUSTOMFIELD_VALUE);
39+
return $value;
40+
}
41+
42+
public static function processRequest($value) {
43+
// additional parameters added to Wordpress
44+
// Main Loop query
45+
$value['meta_key'] = $value[self::PARAM_CUSTOMFIELD_KEY];
46+
$value['meta_value'] = $value[self::PARAM_CUSTOMFIELD_VALUE];
47+
48+
// remove temporary injected parameters
49+
unset($value[self::PARAM_CUSTOMFIELD_KEY], $value[self::PARAM_CUSTOMFIELD_VALUE]);
50+
51+
return $value;
52+
}
53+
54+
public static function rewriteRulesArrayFilter($rules) {
55+
$keys = array_keys($rules);
56+
$tmp = $rules;
57+
$rules = array();
58+
59+
for($i = 0, $j = sizeof($keys); $i < $j; ++$i) {
60+
$key = $keys[$i];
61+
62+
if (preg_match('/%field_([^%]*?)%/', $key)) {
63+
$keyNew = preg_replace(
64+
'/%field_([^%]*?)%/',
65+
'([^/]+)',
66+
// you can simply add next group to the url, because WordPress
67+
// detect them automatically and add next $matches indiceis
68+
$key
69+
);
70+
$rules[$keyNew] = preg_replace(
71+
'/%field_([^%]*?)%/',
72+
sprintf('%s=$1&%s=', self::PARAM_CUSTOMFIELD_KEY, self::PARAM_CUSTOMFIELD_VALUE),
73+
// here on the end will be pasted $matches[$i] from $keyNew, so we can
74+
// grab it it the future in self::PARAM_CUSTOMFIELD_VALUE parameter
75+
$tmp[$key]
76+
);
77+
}
78+
else {
79+
$rules[$key] = $tmp[$key];
80+
}
81+
}
82+
83+
return $rules;
84+
}
85+
}
86+
87+
add_filter('pre_post_link', array('CustomFieldsPermalink', 'linkPost'), 100, 3);
88+
add_filter('post_type_link', array('CustomFieldsPermalink', 'linkPostType'), 100, 4);
89+
add_filter('rewrite_rules_array', array('CustomFieldsPermalink', 'rewriteRulesArrayFilter'));
90+
add_filter('query_vars', array('CustomFieldsPermalink', 'registerExtraQueryVars'), 10, 1);
91+
add_filter('request', array('CustomFieldsPermalink', 'processRequest'), 10, 1);

0 commit comments

Comments
 (0)