• Thank you Takayuki for developing such an excellent and easy to use plugin.

    I needed to call some of my own shortcodes to populate defaults beyond the built-in ones provided by CF7.

    I have developed a workable solution that I currently use(described below), but unfortunately it modifies the plugin code.

    Is there a way to add the functionality without hacking the code?

    Steps to add user defined shortcodes in forms to populate defaults:

    1. Modify function get_default_option in shortcodes.php by adding this fragment at the end of the if, elseif construct:

    elseif ( 'shortcode' == substr( $opt, 0, 9 )) {
    	$s = do_shortcode('[' . substr($opt,9) . ']');
    
    	if ( $args['multiple'] ) {
    		$values[] = $s;
    	} else {
    		return "$s";
    	}
    }

    2. Add your functions & shortcodes in your theme functions.php
    For example, this (useless) function retrieves a hard-coded phone number if the the user is logged in.

    function get_user_phone()
    {
        $tel = '';
        if (is_user_logged_in()) {
            $tel = '0207 3839 8892';
        }
        return $tel;
    }
    
    add_shortcode('user_phone', 'get_user_phone');

    3. Call your shortcode in CF7 form to populate default this way:

    <label> Your Phone (required)
        [text* your-phone default:shortcode:user_phone placeholder "Your Phone"] </label>

    Thank you.

    https://ww.wp.xz.cn/plugins/contact-form-7/

The topic ‘User defined shortcodes for default option’ is closed to new replies.