Skip to content

Instantly share code, notes, and snippets.

@slaFFik
Last active February 14, 2020 19:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save slaFFik/1d0600ca990f72485f84877ff86a6fd0 to your computer and use it in GitHub Desktop.
Save slaFFik/1d0600ca990f72485f84877ff86a6fd0 to your computer and use it in GitHub Desktop.
WPForms: add several new smart tags for newly submitted CPT: ID, title and URL. Useful only for Post Submission addon.
<?php
function wpf_dev_register_smarttag( $tags ) {
// Key is the tag, value is the tag name.
$tags['submitted_cpt_id'] = 'Submitted Post Type ID';
$tags['submitted_cpt_url'] = 'Submitted Post Type URL';
$tags['submitted_cpt_title'] = 'Submitted Post Type Title';
return $tags;
}
add_filter( 'wpforms_smart_tags', 'wpf_dev_register_smarttag' );
function wpf_dev_process_smarttag( $content, $tag ) {
if ( empty( $_POST['wpforms']['entry_id'] ) ) {
return $content;
}
/** @var \WPForms_Entry_Handler $entry */
static $entry;
if ( empty( $entry ) ) {
$entry = wpforms()->entry->get( (int) $_POST['wpforms']['entry_id'], [ 'cap' => false ] );
}
if ( empty( $entry->post_id ) ) {
return $content;
}
switch ( $tag ) {
case 'submitted_cpt_id':
$content = str_replace( '{submitted_cpt_id}', (int) $entry->post_id, $content );
break;
case 'submitted_cpt_url':
$content = str_replace( '{submitted_cpt_url}', esc_url( get_permalink( (int) $entry->post_id ) ), $content );
break;
case 'submitted_cpt_title':
$title = get_post_field( 'post_title', $entry->post_id );
$content = str_replace( '{submitted_cpt_title}', esc_html( $title ), $content );
break;
}
return $content;
}
add_filter( 'wpforms_smart_tag_process', 'wpf_dev_process_smarttag', 10, 2 );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment