首页 文章

Wordpress联系表格7个自定义短代码

提问于
浏览
7

联系表格7有一些短代码,比如[_date]来获取今天的日期 . 但我想在一周后显示日期 .

因此,我需要为联系表单7创建一个自定义短代码,其中显示[next_week],并在收到的电子邮件中显示正确的日期 .

我在何处以及如何创建自定义短代码以联系表单7?

3 回答

  • 1

    将以下内容添加到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类型的"Form"字段 [custom_date next_week]

    现在,您可以在邮件正文中使用 [next_week] .

  • -1

    这对响应方来说有点晚了,但是当我想在我的表单和邮件正文中添加自定义短代码时,我一直看到这篇文章 . 我希望能够插入短代码而无需在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;
    }
    
  • 15

    我之前没有做过,但我认为短代码是由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/

相关问题