web-dev-qa-db-ja.com

カスタムフィールドにフィルタを適用するためのグローバル関数

The_contentに対して完璧に動作するアクションフックがあります。一握りのcustom_fieldが要求されたときはいつでも同じフックを適用したいと思います。

カスタムフィールドは1)myfieldone 2)myfieldart 3)myfieldestionですので、「myfieldone」のいずれかを選択してみてください。カスタムフィールド?

私はそれが同じくらい簡単だろうと思います

add_filter('myfieldone', 'my_add_a_class_function', 10,8);
//or 
add_action('myfieldone', 'my_add_a_class_function');

しかし、どちらも効果がありません。

私はまた、すべてのget_meta、get_meta_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');

これがthe_contentを使ったやり方です。

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;    
}

更新:下記のカスタムメタキーの呼び出しはうまくいくようです。しかし - 修正が必要な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>

ただし、フィルタを適用する前に - フィルタ処理する画像があることを確認します。そうでなければ存在します。どうやら、imageは見つかりません - 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_string"フィルタを適用する必要がありますか

1
Acts7Seven

私はget_{$meta_type}_metadataフィルターについても経験がないので、私が提供できるのは可能な回避策へのさらなるアプローチです:

Iffあなたはget_post_metaを使って第三者を満足させる必要はありませんが、あなた自身のコードの中でフィルタを使いたいだけの場合は、カスタムラッパーを書くことができます。 get_post_metaは、順番にフィルタを受け付けます。

function wpse94639_get_post_meta( $post_id, $key = '', $single = false ) {
    $metadata = get_post_meta( $post_id, $key, $single );
    return apply_filters( 'wpse94639', $metadata );
}

これでadd_filter( 'wpse94639', 'my_add_a_class_function' );を使うことができます。

これは実行可能な解決策にすぎません。iff他のテーマ/プラグインがオリジナルのget_post_metaを使用している場合は、結果をフィルタリングする必要はありません。

1
Johannes Pille

同じフィルターをメタキー値にどのように適用しますか?

あなたがしようとしていること-add_filter('myfieldone', 'my_add_a_class_function', 10,8);--は動作しません。フックを作ることはできません。ソースのどこかに apply_filters または do_action を使用して実装する必要があります。

最も簡単な方法は、関数を呼び出すことです。

$meta_value = get_post_meta($post_id,'key');
$meta_value = my_add_a_class_function($meta_value);

私が知っている限り、その関数をアタッチできるthe_contentのようなテンプレートタグ/フックはありません。

フィルターがあります on get_metadata 、これは get_post_meta および get_post_custom (間接的に)で使用されます。 get_{$meta_type}_metadata、しかし正直に言って、私にとって意味のある方法でそのフィルターを機能させることはできません。

If(is_page_template( 'enews_template.php'){のようなもの} // //このテンプレートで生成されたすべてのカスタムフィールドをフィルタリングする

おそらくない。カスタムフィールドはテンプレートで「生成」されず、特にテンプレートには関連付けられず、投稿/ページ/ CPTに関連付けられます。あなたがそれをうまく機能させることができれば、それは複雑で複雑になります-誰かがget_{$meta_type}_metadataフィルターを動作させることができない限り、私は悲しいことに失敗しました。

0
s_ha_dum