我有一个动作钩,它非常适合于\\u内容。我想在任何时候请求一些custom\\u字段时应用相同的钩子。
自定义字段是1)myfieldone 2)myfieldart 3)myfieldestionSo让我们只取其中一个-“myfieldone”有没有办法-在任何时候请求“myfieldone”自定义字段时,我都可以过滤该自定义字段的值(内容)?
我想这很简单
add_filter(\'myfieldone\', \'my_add_a_class_function\', 10,8);
//or
add_action(\'myfieldone\', \'my_add_a_class_function\');
然而,这两者都没有任何效果。
我还尝试将其全局应用于所有get\\u meta、get\\u meta\\u key
add_filter(\'get_meta\', \'my_add_a_class_function\', 10,8);
add_filter(\'get_meta_key\', \'my_add_a_class_function\', 10,8);
//no such luck. What am I missing?
所以我基本上想
$which_meta_key = \'myfieldone\';
add_action($which_meta_key, \'my_add_a_class_function\');
下面是我如何处理\\u内容的。
add_action(\'the_content\', \'my_add_a_class_function\');
function my_add_a_class_function($content){
$sample_html = $content;
// grab all the matches for img tags and exit if there aren\'t any
if(!preg_match_all(\'/<img.*\\/>/i\', $sample_html, $matches))
exit("Found no img tags that need fixing\\n");
// check out all the image tags we found (stored in index 0)
//print_r($matches);
// iterate through the results and run replaces where needed
foreach($matches[0] as $string){
// keep this for later so that we can replace the original with the fixed one
$original_string = $string;
// preg_replace only replaces stuff when it matches a pattern so it\'s safe to
// just run even if it wont do anything.
$classes = \'TEST\'; // separated by spaces, e.g. \'img image-link\'
// check if there are already classes assigned to the anchor
if ( preg_match(\'/<img.*? class=".*?">/\', $string) ) {
$string = preg_replace(\'/(<img.*? class=".*?)(".*?>)/\', \'$1 \' . $classes . \'$2\', $string);
} else {
$string = preg_replace(\'/(<img.*?)>/\', \'$1 class="\' . $classes . \'" >\', $string);
}
// now replace the original occurence in the html with the fix
$sample_html = str_replace($original_string, $string, $sample_html);
}
return $sample_html;
}
UPDATE:下面的自定义元键调用似乎工作正常。然而,我收到了“未找到需要修复的img标记”{if/EXIT}消息
... // pass the custom_meta content to my filter.
$sample_content = $content
print_r($sample_content);
//YIELDS
<p><img class="alignleft" width="175" height="175" src="/wp-content/uploads/2013/04/brother-in-law.gif" alt="">
Peter, My brother-in-law informed me that he has a new job at IBM.</p>
但是,在应用过滤器之前,我会检查是否有要过滤的图像。如果不存在,则存在。显然,没有找到任何图像-尽管通过print\\r-图像标签明显存在。
if(!preg_match_all(\'/<img.*\\/>/i\', $sample_html, $matches))
exit("Found no img tags that need fixing\\n");
// YIELDS:
Found no img tags that need fixing
//however it does echo out my custom field html code, the image and paragraph text display - - because thats part of the get_meta_data - where I initially request the custom field.
SO - QUESTION - 这是一个简单的正则表达式问题吗?我的
preg_match_all (\'/<img.*\\/>/i\'...
OR - 我是否需要对内容应用“to\\u string”过滤器,以便它将图像作为图像而不仅仅是文本查找