Created
June 1, 2016 03:29
-
-
Save ben-heath/1cd7db4dec5136e539f67b53bc2709be to your computer and use it in GitHub Desktop.
Gravity Form Merge tag for getting embedded post's category (of custom post type)
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 | |
// merge tag for Job Application Category | |
add_action( 'gform_admin_pre_render', 'sls_add_merge_tags' ); | |
function sls_add_merge_tags( $form ) { | |
?> | |
<script type="text/javascript"> | |
gform.addFilter('gform_merge_tags', 'add_merge_tags'); | |
function add_merge_tags(mergeTags, elementId, hideAllFields, excludeFieldTypes, isPrepop, option){ | |
mergeTags["custom"].tags.push({ tag: '{embededPostCat}', label: 'Embeded Post Category' }); | |
return mergeTags; | |
} | |
</script> | |
<?php | |
//return the form object from the php hook | |
return $form; | |
} | |
add_filter( 'gform_replace_merge_tags', 'sls_replace_prod_sku_tag', 10, 7 ); | |
function sls_replace_prod_sku_tag( $text, $form, $entry, $url_encode, $esc_html, $nl2br, $format ) { | |
$custom_merge_tag = '{embededPostCat}'; | |
if ( strpos( $text, $custom_merge_tag ) === false ) { | |
return $text; | |
} | |
global $post; | |
$term_list = wp_get_post_terms($post->ID, 'job-category', array("fields" => "all")); | |
foreach($term_list as $term_single) { | |
$postCat = $term_single->slug; //do something here | |
} | |
$text = str_replace( $custom_merge_tag, $postCat, $text ); | |
return $text; | |
} | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment