2012-11-01 43 views
6

聯繫表7有一些簡碼,比如[_date]以獲取今天的日期。但是我想從現在起一週後顯示日期。Wordpress聯繫表格7個自定義簡碼

所以我需要創建一個自定義短代碼聯繫表單7,需要說[next_week],並在收到的電子郵件顯示正確的日期。

在哪裏以及如何創建自定義簡碼以聯繫表單7?

回答

13

以下內容添加到您的functions.php

wpcf7_add_shortcode('custom_date', 'wpcf7_custom_date_shortcode_handler', true); 

function wpcf7_custom_date_shortcode_handler($tag) { 
    if (!is_array($tag)) return ''; 

    $name = $tag['name']; 
    if (empty($name)) return ''; 

    $next_week = date('Y-m-d', time() + (60*60*24*7)); 
    $html = '<input type="hidden" name="' . $name . '" value="' . $next_week . '" />'; 
    return $html; 
} 

現在,在「表格「字段中的CF7 GUI類型[custom_date next_week]

現在您可以在混亂中使用[next_week]年齡身體。

+1

我使用了一個更簡單的版本來滿足我的需求:'wpcf7_add_shortcode('input_name',function($ tag){return''});' – vladkras

0

我以前沒有做過,但我認爲短代碼是由wordpress本身管理的(即使插件爲CF7)。

一個例子來創建簡單的短代碼是:

//[foobar] 
function foobar_func($atts){ 
return "foo and bar"; 
} 
add_shortcode('foobar', 'foobar_func'); 

在放置的functions.php。

欲瞭解更多信息:http://codex.wordpress.org/Shortcode_API

或者你可以使用插件像這樣做的工作:http://wordpress.org/extend/plugins/shortbus/

+1

CF7打印[foobar的],文章和網頁打印 「foo和bar」。所以它不起作用。 – halliewuud

+0

這個工作正常,當你想在表單中使用短代碼,但它不能在發送的電子郵件中工作。我的答案是正確的解決方案 – halliewuud

0

這對於響應方來說有點遲,但是當我想將自定義簡碼添加到我的表單和郵件正文中時,我一直看到這篇文章。我希望能夠插入簡碼,而不用在CF7中註冊它們,並且通常只在郵件正文中(CF7似乎無法做到這一點)。

下面是我終於做到了:

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