Điều này hơi muộn đối với bên phản hồi, nhưng tôi tiếp tục nhìn thấy bài đăng này khi tôi muốn thêm mã ngắn tùy chỉnh vào biểu mẫu và nội dung thư của tôi. Tôi muốn có thể chèn mã ngắn mà không cần đăng ký chúng đặc biệt trong CF7 và thường chỉ trong nội dung thư (một cái gì đó CF7 dường như không thể làm).
Sau đây là cách cuối cùng tôi đã làm điều đó:
// Allow custom shortcodes in CF7 HTML form
add_filter('wpcf7_form_elements', 'dacrosby_do_shortcodes_wpcf7_form');
function dacrosby_do_shortcodes_wpcf7_form($form) {
$form = do_shortcode($form);
return $form;
}
// Allow custom shortcodes in CF7 mailed message body
add_filter('wpcf7_mail_components', 'dacrosby_do_shortcodes_wpcf7_mail_body', 10, 2);
function dacrosby_do_shortcodes_wpcf7_mail_body($components, $number) {
$components['body'] = do_shortcode($components['body']);
return $components;
};
// Add shortcode normally as per WordPress API
add_shortcode('my_code', 'my_code_callback');
function my_code_callback($atts){
extract(shortcode_atts(array(
'foo' => 'bar'
), $atts));
// do things
return $foo;
}
Tôi sử dụng phiên bản đơn giản hơn cho các nhu cầu của mình: 'wpcf7_add_shortcode ('input_name', hàm ($ tag) {return ' '}); ' – vladkras