WordPress: Eigenes Plugin schreiben – “Post Functions”

wp-logoFür die Pluginentwicklung in WordPress sollte man zwei Sprachen beherrschen: PHP und Englisch. Alle wichtigen Hinweise können auf codex.wordpress.org/Writing_a_Plugin (en) gefunden werden.

Motivation

Ich verwende in meinem Blog relativ viele Links auf externe aber auch auf eigene Seiten. Das Verlinken von eigenen Seiten birgt ein Problem, welches sich erst bei einem eventuellen Umzug auf eine andere Domain offenbart: Alle Links müssen absolut sein, also die komplette Domain beinhalten. Bei einem Umzug auf eine andere Domain wären diese Links alle “kaputt”.

Mein experimentelles Plugin “Post Functions” führt 2 neue Funktionen ein, die in Artikeln und Seiten verwendet werden können:

Funktion “link”

Beispiele
(Der “.” in “[." dient dazu, dass das Plugin nicht aktiv wird...)

Externer Link
[.link:http://codex.wordpress.org/Writing_a_Plugin]
Resultat: codex.wordpress.org/Writing_a_Plugin

Interner Link
[.link:ueber-mich|Über mich]
Resultat: Über mich

Der Text nach dem “|” dient als Text für den Link. Wenn kein “|” vorhanden ist, wird die Url als Text verwendet, wobei das http:// wird entfernt. Zusätzlich kann das Plugin so konfiguriert werden, dass lange Urls durch “…” verkürzt werden:

Langer externer Link
[.link:http://www.chiark.greenend.org.uk/~sgtatham/putty/download.html]
Resultat: chiark.greenend.org.uk/…/download.html

Funktion “bloginfo”

Beispiel
[.bloginfo:url]
Resultat: http://familie-ottenhaus.de/simon/blog

“bloginfo” gibt Zugriff auf die WordPress-Funktion bloginfo. “[.bloginfo:url]” kann zum Beispiel in Bild-Urls verwendet werden.

(Source Code im ganzen Artikel)

Zusammenfassung

“Post Functions” ist ein experimentelles Plugin – aber auf jeden Fall harmlos. Feedback und Erweiterungsvorschläge (weitere Funktionen) sind in jedem Fall erwünscht.

Source Code

Der nachfolgende Source Code ist eventuell neuer als der Artikel, da der Code automatisch geladen wird.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
Contents of 'wp-content/plugins/post-functions/post-functions.php'
<?php
/**
 * @author Simon Ottenhaus
 * @version 0.1
 */
/*
Plugin Name: Post Functions
Plugin URI: 
Description: ...
Author: Simon Ottenhaus
Version: 0.1
Author URI: http://familie-ottenhaus.de/simon/blog/
*/
 
include_once "post-functions-admin.php";
 
class PostFunctions{
 
	function substitute($orig, $func_name, $func_args, $inner)
	{
		$enabledFunctions = get_option('post_functions_enabled');
		if(!in_array($func_name, explode(",", $enabledFunctions))){
			return $orig;
		} else {
			$ret = call_user_func (array(&$this, 'pf_' . $func_name), $func_args, $inner);
			return $ret;
		}
	}
 
	function pf_printfile($args, $inner) {
		$p = explode("|", $args);
		$filename = $p[0];
		if(substr($filename,0,1)=="/") $filename = substr($filename,1);
		$filepath = ABSPATH . $filename;
		unset($p[0]);
		$settings = $this->explodeKeyValue( $p , '|', '=', true );
 
 
		if ( file_exists( $filepath ) ) {
			$ret = "";
			if ( isset ( $settings[ 'displaypath' ] ) ) $ret = "Contents of '$filename'\n";
			$ret .= file_get_contents($filepath);
			return $ret;
		} else {
			return "'$filename' not found.";
		}
 
	}
 
	function pf_fold($args, $inner) {
		global $post_functions_fold_id;
		if(!isset($post_functions_fold_id))$post_functions_fold_id=0;
		$post_functions_fold_id++;
		$id = "fold_$post_functions_fold_id";
		return "<a class=\"js\" onclick=\"$('#$id').slideToggle('slow'); \">" . $args . "</a><span style=\"display:none;\" id=\"$id\">$inner</span>";
	}
 
	function pf_jslink($args, $inner) {
		return '<a class="js" onclick="' . htmlspecialchars($inner) . '">' . $args . '</a>';
	}
	function pf_bloginfo($args, $inner){
		return get_bloginfo($args);
	}
	function pf_link($args, $inner){
		$homeUrl = get_bloginfo("url");
		$parts = explode("|", $args);
		$url = $parts[0];
		$url_ellipsis_length = get_option('post_functions_url_ellipsis_length');
 
		if(sizeof($parts) > 1) {
			$text = $parts[1];
		} else{
			$text = $url;
			$this->str_remove($text, "://");
			if ( substr($text, 0, 4) == "www.") $text = substr($text, 4);
			if(substr($text, -1) == '/') $text = substr($text, 0, -1);
			if($url_ellipsis_length > 0 && strlen($text) > $url_ellipsis_length) {
				$text = $this->str_ellipsis_url($text);
			}
		}
 
		if(!preg_match("#^\w+://#", $url)) $url = "$homeUrl/$url";
 
		if(strlen($url) > 0){
			$s = $url;
			if($this->str_remove($s, "://")) {
				$this->str_remove($s, "/");
			}
			if(strlen($s) > 0 && strpos($s, '.')===false && substr($s, -1) != '/') $url .= '/';
		}
 
		return "<a href=\"$url\">$text</a>";
	}
 
	function str_remove(&$string, $find) {
		$pos = strpos($string, $find);
		if($pos === false) {
			return false;
		} else {
			$string = substr($string, $pos + 3);
			return true;
		}
	}
 
	function str_ellipsis_url($url) {
		$parts = explode('/', $url);
		$size = sizeof($parts);
		if($size > 2){
			return $parts[0] . '/.../' . $parts[$size - 1];
		}
		return $url;
	}
	function explodeKeyValue($str, $item_sep, $assign_sep, $trim=true){
		$ret = array();
		$arr = is_array( $str ) ? $str : explode ( $item_sep, $str );
		foreach ( $arr as $item ) {
			if ( $trim ) $item = trim ( $item );
			$p = explode ( $assign_sep, $item );
			$ret[ $p[0] ] = isset( $p[1] ) ? $p[1] : 1;
		}
		return $ret;
	}
 
 
	function before_filter($content)
	{
		$parts = preg_split( "#(\\[\\/?\\w+(?::[^\\]]+)?\\])#", $content, -1, PREG_SPLIT_DELIM_CAPTURE );
		$parts[] = "";
		$parts[] = "";
		$tokens = array();
		$out = array();
 
		$out[] = $parts[0];
 
		for($i=1;$i<sizeof($parts)-3;$i+=2){
			$m = explode(":", substr($parts[$i],1,-1), 2);
			$next = substr($parts[$i+2],1,-1);
			$func_name = $m[0];
			$func_args = sizeof($m)>1 ? $m[1] : "";
			$inner = "";
			$orig = $parts[$i];
			if ( "/".$func_name == $next ) {
				$inner = $parts[$i+1];
				$i+=2;
				$orig .= $parts[$i+1] . $parts[$i+2];
			}
			$out[] = $this->substitute($orig, $func_name, $func_args, $inner);
			$out[] = $parts[$i+1];
		}
		$content = implode("", $out);
 
		return $content;
	}
 
	function wp_head_style() {
		echo "\n\t".'<!-- Generated by Post Functions Plugin -->'
			. "\n\t".'<style type="text/css" media="screen">'
			. "\n\t\t".'a.js { cursor:pointer; }'
			. "\n\t".'</style>'
			. "\n\t".'<!-- /Post Functions Plugin -->'
			. "\n\t";		
	}
 
}
$myPostFunctions = new PostFunctions();
 
// We want to run before other filters; hence, a priority of 0 was chosen.
// The lower the number, the higher the priority.  10 is the default and
// several formatting filters run at or around 6.
add_filter('the_content', array(&$myPostFunctions, 'before_filter'), 0);
add_filter('the_excerpt', array(&$myPostFunctions, 'before_filter'), 0);
add_action('wp_head', array(&$myPostFunctions, 'wp_head_style'));
 
 
?>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
Contents of 'wp-content/plugins/post-functions/post-functions-admin.php'
<?
 
function post_functions_admin_menu() {
	add_option("post_functions_url_ellipsis_length", "40");
	add_option("post_functions_enabled", "bloginfo,link,jslink"); 
	add_options_page('Post Functions Options', 'Post Functions', 8, __FILE__, 'post_functions_admin_options');
}
 
function post_functions_admin_options() {
?>
<div class="wrap">
<h2>Post Functions</h2>
 
<form method="post" action="options.php">
<?php wp_nonce_field('update-options'); ?>
 
<table class="form-table">
 
<tr valign="top">
<th scope="row">Url Ellipsis Length</th>
<td><input type="text" class="small-text" name="post_functions_url_ellipsis_length" value="<?php echo get_option('post_functions_url_ellipsis_length'); ?>" />
<span class="setting-description">Insert ellipsis-dots ("...") into urls longer than x characters. Use "0" to disable this function.</span></td>
</tr>
 
<tr valign="top">
<th scope="row">Enabled Functions</th>
<td><input type="text" class="regular-text code" name="post_functions_enabled" value="<?php echo get_option('post_functions_enabled'); ?>" />
<span class="setting-description">Enabled Functions</span></td>
</tr>
 
</table>
 
<input type="hidden" name="action" value="update" />
<input type="hidden" name="page_options" value="post_functions_url_ellipsis_length,post_functions_enabled" />
 
<p class="submit">
<input type="submit" class="button-primary" value="<?php _e('Save Changes') ?>" />
</p>
 
</form>
</div>
 
<?
}
 
add_action('admin_menu', 'post_functions_admin_menu');
 
?>

Keine Kommentare

Noch keine Kommentare.

RSS Feed für Kommentare zu diesem Artikel. TrackBack URI

Hinterlasse einen Kommentar

WordPress Themes