Home / Archive / WPForms: add several new smart tags for newly submitted CPT: ID, title and URL.
Duplicate Snippet

Embed Snippet on Your Site

WPForms: add several new smart tags for newly submitted CPT: ID, title and URL.

Mainly useful when the Post Submissions addon is used.
Several new smart tags are registered to grab the Custom Post Type (CPT) ID, URL and Title.

Code Preview
php
<?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 );

Comments

Add a Comment