'; if ( @ file_exists( WP_PLUGIN_DIR . '/contact-form-7/wp-contact-form-7.php' ) ) { $out .= esc_html__( 'The Contact Form 7 is installed, but you must activate Contact Form 7 below for the Hidden Fields Module to work.', 'cf7_modules' ); } else { $out .= esc_html__( 'The Contact Form 7 plugin must be installed for the Hidden Fields Module to work.', 'cf7_modules' ); $install_url = esc_url_raw( admin_url( 'plugin-install.php?tab=plugin-information&plugin=contact-form-7&from=plugins&TB_iframe=true&width=600&height=550' ) ); $out .= sprintf( ' %s', $install_url, esc_html__( 'Install Now.', 'cf7_modules' ) ); } $out .= '

'; echo $out; } function contact_form_7_hidden_fields_scripts() { wp_enqueue_script('thickbox'); } /** ** A base module for [hidden] and [hidden*] **/ /* Shortcode handler */ add_filter('wpcf7_form_elements', 'wpcf7_form_elements_strip_paragraphs_and_brs'); /** * Strip paragraph tags being wrapped around the field * @param $form * * @return mixed */ function wpcf7_form_elements_strip_paragraphs_and_brs($form) { return preg_replace_callback( '/

(/ism', 'wpcf7_form_elements_strip_paragraphs_and_brs_callback', $form ); } function wpcf7_form_elements_strip_paragraphs_and_brs_callback($matches = array()) { return "\n" . '' . "\n" . '

' . str_replace( '
', '', str_replace( '
', '', stripslashes_deep( $matches[1] ) ) ) . '
' . "\n" . '' . "\n"; } /** ** A base module for [hidden], [hidden*] **/ /* Shortcode handler */ function wpcf7_hidden_shortcode_handler( $tag ) { if ( class_exists( 'WPCF7_FormTag' ) ) { $tag = new WPCF7_FormTag( $tag ); } else { $tag = new WPCF7_Shortcode( $tag ); } if ( empty( $tag->name ) ) { return ''; } $validation_error = wpcf7_get_validation_error( $tag->name ); $class = wpcf7_form_controls_class( $tag->type, 'wpcf7-hidden' ); if ( $validation_error ) { $class .= ' wpcf7-not-valid'; } $class .= ' wpcf7-hidden'; if ( 'hidden*' === $tag->type ) { $class .= ' wpcf7-validates-as-required'; } $value = (string) reset( $tag->values ); $placeholder = ''; if ( $tag->has_option( 'placeholder' ) || $tag->has_option( 'watermark' ) ) { $placeholder = $value; $value = ''; } $default_value = $tag->get_default_option( $value ); $value = contact_form_7_hidden_fields_fill_post_data( $value, $tag ); // Post data hasn't filled yet. No arrays. if ( $default_value === $value ) { $value = contact_form_7_hidden_fields_fill_user_data( $value ); } // Arrays get imploded. $value = is_array( $value ) ? implode( apply_filters( 'wpcf7_hidden_field_implode_glue', ', ' ), $value ) : $value; // Make sure we're using a string. Objects get JSON-encoded. if ( ! is_string( $value ) ) { $value = json_encode( $value ); } $value = apply_filters( 'wpcf7_hidden_field_value', apply_filters( 'wpcf7_hidden_field_value_' . $tag->get_id_option(), $value ) ); $value = wpcf7_get_hangover( $tag->name, $value ); $atts = array( 'type' => 'hidden', 'class' => $tag->get_class_option( $class ), 'id' => $tag->get_id_option(), 'name' => $tag->name, 'tabindex' => $tag->get_option( 'tabindex', 'int', true ), 'placeholder' => $placeholder, 'value' => $value, ); $atts = wpcf7_format_atts( $atts ); $html = sprintf( '%2$s', $atts, $validation_error ); return $html; } /** * Fill data based on user information. * * @param string $value Existing value, if any * * @return mixed */ function contact_form_7_hidden_fields_fill_user_data( $value ) { $return = $value; // Process user stuff if ( preg_match( '/user/ism', strtolower( trim( $value ) ) ) && is_user_logged_in() ) { $current_user = wp_get_current_user(); switch ( $value ) { case 'user_name': $return = $current_user->user_login; break; case 'user_id': $return = $current_user->ID; break; case 'caps': $return = $current_user->caps; break; case 'allcaps': $return = $current_user->allcaps; break; case 'user_roles': $return = $current_user->roles; break; default: // Gets the values for `user_email`, others that have `user_` prefix. if ( $current_user->has_prop( $value ) ) { $return = $current_user->get( $value ); } else { // Define some other item in the WP_User object using the `user_[what you want to get]` format // This works for the `user_display_name` setting $user_key = preg_replace( '/user[_-](.+)/ism', '$1', $value ); if ( $current_user->has_prop( $user_key ) ) { $return = $current_user->get( $user_key ); } } break; } } return $return; } /** * Fill data based on user information. * * @param string $value Existing value, if any * @param WPCF7_Shortcode $tag Tag * * @return mixed */ function contact_form_7_hidden_fields_fill_post_data( $value = '', $tag ) { global $post; $return = $value; if ( is_object( $post ) ) { switch ( strtolower( trim( $value ) ) ) { case 'post_title': case 'post-title': $return = $post->post_title; break; case 'page_url': case 'post_url': $return = get_permalink( $post->ID ); if ( empty( $return ) && isset( $post->guid ) ) { $return = $post->guid; } $return = esc_url( $return ); break; case 'post_category': $categories = get_the_category( $post->ID ); $catnames = array(); // Get the category names foreach ( $categories as $cat ) { $catnames[] = $cat->cat_name; } $return = implode( ', ', $catnames ); break; case 'post_author_id': $return = $post->post_author; break; case 'post_author': $user = get_userdata( $post->post_author ); $return = $user->display_name; break; default: // You want post_modified? just use [hidden hidden-123 "post_modified"] if ( isset( $post->{ $value } ) ) { $return = $post->{ $value }; } break; } if ( preg_match( '/^custom_field\-(.*?)$/ism', $value ) ) { $custom_field = preg_replace( '/custom_field\-(.*?)/ism', '$1', $value ); $return = get_post_meta( $post->ID, $custom_field, false ) ? get_post_meta( $post->ID, $custom_field, false ) : ''; } } return $return; } /* Tag generator */ if ( is_admin() ) { add_action( 'admin_init', 'wpcf7_add_tag_generator_hidden', 30 ); } function wpcf7_add_tag_generator_hidden() { if ( class_exists( 'WPCF7_TagGenerator' ) ) { $tag_generator = WPCF7_TagGenerator::get_instance(); $tag_generator->add( 'hidden', _x( 'hidden', 'the name of the field button in CF7', 'cf7_modules' ), 'wpcf7_tg_pane_hidden' ); } } function wpcf7_tg_pane_hidden( $contact_form, $args = '' ) { $args = wp_parse_args( $args, array() ); $description = __( "Generate a form tag for a hidden field. For more details, see %s.", 'contact-form-7' ); $desc_link = wpcf7_link( 'https://wordpress.org/plugins/contact-form-7-modules/', __( 'the plugin page on WordPress.org', 'contact-form-7' ), array( 'target' => '_blank' ) ); ?>
 
  • post_title: The title of the post/page', 'cf7_modules' ); ?>
  • post_url: The URL of the post/page', 'cf7_modules' ); ?>
  • post_category: The categories the post is in, comma-separated', 'cf7_modules' ); ?>
  • post_date: The date the post/page was created', 'cf7_modules' ); ?>
  • post_author: The name of the author of the post/page', 'cf7_modules' ); ?>
  • user_name: User Login', 'cf7_modules' ); ?>
  • user_id: User ID', 'cf7_modules' ); ?>
  • user_email: User Email Address', 'cf7_modules' ); ?>
  • user_display_name: Display Name (Generally the first and last name of the user)', 'cf7_modules' ); ?>