Last active
December 2, 2016 11:12
-
-
Save rmpel/c2fd36fed95af0af896e9c39ded22981 to your computer and use it in GitHub Desktop.
WPCF7 helper: make a checkbox label partially linking, WordPress shortcode
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| add_shortcode('label_link', 'label_link'); | |
| function label_link($atts, $content='') { | |
| $field = $text = $target = $href = false; | |
| extract( shortcode_atts( compact('field', 'text', 'target', 'href' ), $atts ) ); | |
| if (!$field || !$text || !$href) { | |
| return false; | |
| } | |
| label_link_store($field, $text, $href, $target); | |
| } | |
| function label_link_store($field=null, $text=null, $href=null, $target=null) { | |
| static $store; | |
| if (!$store) | |
| $store = array(); | |
| if ($field && $text && $href) { | |
| $store[$field] = compact('field', 'text', 'target', 'href' ); | |
| } | |
| return $store; | |
| } | |
| add_action('wp_footer', 'label_link_script', 1000); | |
| function label_link_script() { | |
| $store = label_link_store(); | |
| if (!$store) { | |
| return; | |
| } | |
| ?><script> | |
| jQuery(document).ready(function(){ | |
| var store = <?php print json_encode($store); ?> || {}; | |
| for (var field in store) { | |
| var $field = jQuery("[name^='" + field + "']"); | |
| var $label = jQuery("[for^='" + field + "']"); | |
| if ($label.length == 0) { | |
| $label = $field.siblings(".wpcf7-list-item-label"); | |
| } | |
| if ($label.length > 0) { | |
| var link = jQuery('<a/>'); | |
| link.text(store[field].text); | |
| link.bind('click', function(e){e.stopPropagation();}); | |
| link.attr('href', store[field].href); | |
| if (store[field].target) link.attr('target', store[field].target); | |
| $newLabel = jQuery('<span>' + $label.text().replace( store[field].text, link.get(0).outerHTML ) + '</span>'); | |
| $label.empty().append( $newLabel ); | |
| } | |
| } | |
| }); | |
| </script><?php | |
| } |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Example use:
[checkbox checkbox-405 use_label_element "I accept the terms and conditions."][label_link field="checkbox-405" text="terms and conditions" href="http://some-page" target="_blank"]