Lấy _page_title, _page_url, _post_id, _cf7_debug on Contact Form 7 (ok)

/**
 * !CF7 special mail tags: [_page_title], [_page_url], [_post_id], [_cf7_debug]
 */
add_filter( 'wpcf7_special_mail_tags', 'my_cf7_page_info_tags', 10, 3 );
function my_cf7_page_info_tags( $output, $name, $html ) {
    // Chỉ xử lý những tag mình cần
    $wanted = array( '_page_title', '_page_url', '_post_id', '_cf7_debug' );
    if ( ! in_array( $name, $wanted, true ) ) {
        return $output;
    }
    $post_id = 0;
    $detected_url = '';
    // 1) Try global $post (normal case when form is rendered inside Loop)
    global $post;
    if ( isset( $post->ID ) && $post->ID ) {
        $post_id = (int) $post->ID;
    }
    // 2) If not found, try CF7 submission meta (recommended fallback for AJAX / REST contexts)
    if ( ! $post_id && class_exists( 'WPCF7_Submission' ) ) {
        $submission = WPCF7_Submission::get_instance();
        if ( $submission ) {
            $meta_url = $submission->get_meta( 'url' );
            if ( ! empty( $meta_url ) ) {
                $detected_url = esc_url_raw( $meta_url );
                $post_id = url_to_postid( $detected_url );
                // nếu url_to_postid không tìm được, thử tìm page theo path
                if ( ! $post_id ) {
                    $path = trim( parse_url( $detected_url, PHP_URL_PATH ), '/' );
                    if ( $path ) {
                        $maybe = get_page_by_path( $path );
                        if ( $maybe ) {
                            $post_id = $maybe->ID;
                        }
                    }
                }
            }
        }
    }
    // 3) Fallback: HTTP_REFERER
    if ( ! $post_id && ! empty( $_SERVER['HTTP_REFERER'] ) ) {
        $ref = esc_url_raw( wp_unslash( $_SERVER['HTTP_REFERER'] ) );
        $detected_url = $ref;
        $post_id = url_to_postid( $ref );
        if ( ! $post_id ) {
            $path = trim( parse_url( $ref, PHP_URL_PATH ), '/' );
            if ( $path ) {
                $maybe = get_page_by_path( $path );
                if ( $maybe ) {
                    $post_id = $maybe->ID;
                }
            }
        }
    }
    // 4) Final fallbacks: site info
    if ( $name === '_post_id' ) {
        return $post_id ? (string) $post_id : '';
    }
    if ( $name === '_page_title' ) {
        if ( $post_id ) {
            return get_the_title( $post_id );
        }
        // fallback: site name
        return get_bloginfo( 'name' );
    }
    if ( $name === '_page_url' ) {
        if ( $post_id ) {
            return esc_url( get_permalink( $post_id ) );
        }
        // fallback: detected_url (from submission/referrer) or home_url
        if ( ! empty( $detected_url ) ) {
            return esc_url( $detected_url );
        }
        return esc_url( home_url() );
    }
    // Debug tag: trả về thông tin detect để test (bỏ vào mail khi test)
    if ( $name === '_cf7_debug' ) {
        $debug = array(
            'global_post_id'   => isset( $post->ID ) ? $post->ID : null,
            'detected_url'     => $detected_url,
            'resolved_post_id' => $post_id,
        );
        return wp_json_encode( $debug );
    }
    return $output;
}
/**
 * CF7 special mail tags: [_page_title], [_page_url], [_post_id], [_cf7_debug]!
 */

Last updated

Was this helpful?