ご注意!

この記事には PHP コードが含まれており、開発者を対象としています。このコードは便宜上提供していますが、コードのカスタマイズやサードパーティの開発についてはサポートを提供していません。

追加のガイダンスについては、WPBeginner の カスタムコードの追加方法に関するチュートリアル を参照してください。

閉じる

説明

WPFormsフォームビルダー内でスマートタグを処理するために使用されるwpforms_smart_tag_processフィルター。

パラメーター

$content
(文字列) (必須)スマートタグの内容。
$tag
(文字列) (必須)スマートタグのタグ名。

ソース

wpforms/includes/class-smart-tags.php

詳細情報

このフィルターは、WPFormsフォームビルダー内で使用されるスマートタグを作成、定義、登録するために使用されます。

このフィルターを使用する場合は、処理する前にスマートタグを登録するwpforms_smart_tagsも併せて使用する必要があります。

/**
 * Process the Smart Tag.
 *
 * @link    https://wpforms.com/developers/wpforms_smart_tag_process/
 *
 * @param   string   $content  Content of the Smart Tag.
 * @param   string   $tag      Tag name of the Smart Tag.
 * @return  string
 */

function wpf_dev_process_smarttag( $content, $tag ) {
 
    $userID = get_current_user_id();
 
    // Only run if it is our desired tag.
    if ( 'wp_nickname' === $tag ) {

        $wp_nickname = get_the_author_meta( 'nickname', $userID );

        // Replace the tag with the nickname pulled from the user's WordPress profile.
        $content = str_replace( '{wp_nickname}', $wp_nickname, $content );

    }

        if ( 'wp_username' === $tag ) {

        $wp_username = get_the_author_meta( 'user_login', $userID );

        // Replace the tag with the username pulled from the user's WordPress profile.
        $content = str_replace( '{wp_username}', $wp_username, $content );

    }
 
    return $content;
}

add_filter( 'wpforms_smart_tag_process', 'wpf_dev_process_smarttag', 10, 2 );

参考記事