此插件允许您创建Custom Fields 对于任何类型的内容,都可以这样命名mic:name 然后,当你使用{mic:name} 或[mic:name], 只有在wpautop 这样它就不会被弄脏了(优先级11)
micev:name 工作原理相同(使用{micev:name} 或[micev:name]) 但在插入内容之前,输出是eval()-ed。小心使用。。。因为它允许实际的PHP代码在帖子中运行。
/*
Plugin Name: [Post] Metas In Content
Description: Enables <code>[mic:MIC_Name]</code> and <code>[micev:MIC_Name]</code> inside content.
Version: 0.0.7
Author: EarnestoDev
Author URI: http://www.earnestodev.com/
*/
// ---------------------------------------------------------- //
class Meta_In_Content{
public function __construct(){
register_uninstall_hook(__FILE__, array($this, \'uninstall\'));
// Run after the WordPress wpautop and such killers of code
add_filter(\'the_content\', array($this, \'the_content\'), 11);
}
public function uninstall(){
global $wpdb;
$wpdb->query("DELETE FROM {$wpdb->postmeta} WHERE (`meta_key` LIKE \'mic:%\') OR (`meta_key` LIKE \'micev:%\');");
}
public function the_content($content){
global $post, $wpdb; $postID = $post->ID;
// mic:NAME metas are not eval()-ed before inserted in the content
$ph_metas = $wpdb->get_col("SELECT DISTINCT(`meta_key`) FROM {$wpdb->postmeta} WHERE `post_id`={$postID} AND `meta_key` LIKE \'mic:%\';");
foreach($ph_metas as $ph_meta){
$value = get_post_meta($postID, $ph_meta, true);
if(!is_string($value)) continue;
$content = str_replace(array("[{$ph_meta}]", "{{$ph_meta}}") , array($value, $value), $content);
}
// micev:NAME metas are eval()-ed before inserted in the content
$ph_metas = $wpdb->get_col("SELECT DISTINCT(`meta_key`) FROM {$wpdb->postmeta} WHERE `post_id`={$postID} AND `meta_key` LIKE \'micev:%\';");
foreach($ph_metas as $ph_meta){
$value = get_post_meta($postID, $ph_meta, true);
if(!is_string($value)) continue;
ob_start();
eval(sprintf(\' ?>%s<?php \', $value));
$value = ob_get_clean();
$content = str_replace(array("[{$ph_meta}]", "{{$ph_meta}}") , array($value, $value), $content);
}
return $content;
}
};
// ---------------------------------------------------------- //
// Instantiate the Object once
$Meta_In_Content = new Meta_In_Content();
// ---------------------------------------------------------- //
玩得开心!
只需将其保存在插件中/wp-content/mu-plugins. 要禁用eval()功能,只需从// micev:NAME 返回之前的右侧metas。