Skip to content

Instantly share code, notes, and snippets.

@rbk
Last active February 27, 2020 18:01
Show Gist options
  • Select an option

  • Save rbk/43908b52d44ac3f46a3e to your computer and use it in GitHub Desktop.

Select an option

Save rbk/43908b52d44ac3f46a3e to your computer and use it in GitHub Desktop.
Turn input placeholders into floating labels for WP Contact Form 7 fields
<?php
defined( 'ABSPATH' ) or die( 'No script kiddies please!' );
/*
Plugin name: WPCF7 FLoating Labels
Author: Richard Keller
URI: http://richardkeller.net
Description: Adds floating lables to all text inputs
*/
function append_style_and_js( ){
?>
<script type="text/javascript">
jQuery(document).ready(function(){
$(document).on('focus', 'input[type=text], input[type=email], textarea', function(e){
var ph = $(this).attr('placeholder');
if( ph ){
$(this).attr('ph', $(this).attr('placeholder') );
$(this).attr('placeholder', '');
$(this).animate({'padding-top':'20px', 'position' : 'relative'}, 100);
$(this).parent().append('<div class="floating-label">' + ph + '</div>');
$(this).parent().find('.floating-label').fadeIn();
}
});
});
</script>
<style type="text/css">
.wpcf7-form-control-wrap {
display: block;
}
.wpcf7-form-control-wrap .floating-label {
display: none;
position: absolute;
top: 3px;
z-index: 9;
left: 12px;
font-size: 12px;
}
</style>
<?php
}
add_action('wp_footer', 'append_style_and_js');
@mattawise

Copy link
Copy Markdown

Hi there, whereabouts does this code go in the plugin? Thanks, Matt

@sagive

sagive commented Apr 2, 2017

Copy link
Copy Markdown

This doesnt work in chrome due to a bug...

@ThianBrodie

ThianBrodie commented Sep 20, 2017

Copy link
Copy Markdown

Hi, I am also a little unsure where to place the code, I have broke it up and put in functions.php

`
add_action('wp_footer', 'append_style_and_js');
function append_style_and_js( ){

?>
<script type="text/javascript">
jQuery(document).ready(function(){

	$(document).on('focus', 'input[type=text], input[type=email], input[type=tel], input[type=number], textarea', function(e){
		var ph = $(this).attr('placeholder');
		if( ph ){
			$(this).attr('ph', $(this).attr('placeholder') );
			$(this).attr('placeholder', '');
			$(this).animate({'padding-top':'20px', 'position' : 'relative'}, 100);
			$(this).parent().append('<div class="floating-label">' + ph + '</div>');
			$(this).parent().find('.floating-label').fadeIn();
		}
	});
});
</script>


<?php

}
`
And the css in a custom.css, works ok but many other conflicts with other plugins, so any advice on where to place the index.php file be appreciated

@vinyllicker

vinyllicker commented May 14, 2018

Copy link
Copy Markdown

Hi everyone, was trying to get this applied myself and found that it could be coded slightly wrong for WP, $ isn't usually a function, it needs jQuery written out so try the below. This is a plugin as it is uploaded so you can download and change the contents of the file or I apply it as a php shortcode directly on the pages I need. I use a shortcode creator plugin for this, let me know if you need assistance. I have also added in telephone fields to extend functionality :

<?php

defined( 'ABSPATH' ) or die( 'No script kiddies please!' );
/*

Plugin name: WPCF7 FLoating Labels
Author: Richard Keller
URI: http://richardkeller.net
Description: Adds floating lables to all text inputs

*/
function append_style_and_js( ){

?>
<script type="text/javascript">
jQuery(document).ready(function(){

	jQuery(document).on('focus', 'input[type=text], input[type=email], input[type=tel], textarea', function(e){
		var ph = jQuery(this).attr('placeholder');
		if( ph ){
			jQuery(this).attr('ph', jQuery(this).attr('placeholder') );
			jQuery(this).attr('placeholder', '');
			jQuery(this).animate({'padding-top':'20px', 'position' : 'relative'}, 100);
			jQuery(this).parent().append('<div class="floating-label">' + ph + '</div>');
			jQuery(this).parent().find('.floating-label').fadeIn();
		}
	});
});
</script>
<style type="text/css">

	.wpcf7-form-control-wrap {
	display: block;
	}
	.wpcf7-form-control-wrap .floating-label {
		display: none;
		position: absolute;
		top: 3px;
		z-index: 9;
		left: 12px;
		font-size: 12px;
	}

</style>

<?php

}

add_action('wp_footer', 'append_style_and_js');

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment