Site cover image

Site icon image 制作メモ

Description is here. The icon, the title, the description can be modified in Notion.

[WordPress] MW WP FORM 投稿と連携させて投稿内容をメールで送る

クエリ文字列 post_id で取得した記事タイトル {post_title} をメールに記載したい。

以前はできていた気がしたけどたぶん気のせいなのだろう・・・。

ちゃんとフィルターフックを用意してくれているのでそれを使う。

mwform_custom_mail_tag

https://plugins.2inc.org/mw-wp-form/filter-hook/mwform_custom_mail_tag/

function my_custom_mail_tag( $value, $key, $insert_contact_data_id ) {
    $post_id = filter_input( INPUT_GET, 'post_id', FILTER_SANITIZE_NUMBER_INT );
    if ( $key === 'post_title' ) {
        return get_the_title( $post_id );
    }
    return $value;
}
add_filter( 'mwform_custom_mail_tag', 'my_custom_mail_tag', 10, 3 );

カスタムフィールドの場合はこんな感じか。

function my_custom_mail_tag( $value, $key, $insert_contact_data_id ) {
    $post_id = filter_input( INPUT_GET, 'post_id', FILTER_SANITIZE_NUMBER_INT );
    if ( $key === 'cf_key' ) {
        return get_post_meta( $post_id, 'cf_key', true );
    }
    return $value;
}
add_filter( 'mwform_custom_mail_tag', 'my_custom_mail_tag', 10, 3 );