Source code for https://formidable-masterminds.com/conditionally-show-form-or-edit-in-place-view-in-parent-view/
This file contains 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 | |
/* use the frm_display_entry_content filter hook to conditionally display the relationship form for | |
* adding a new relationship record or relationship view for editing the relationship in place in the | |
* media contacts view | |
*/ | |
add_filter('frm_display_entry_content', 'media_contact_relationships', 20, 7); | |
function media_contact_relationships($new_content, $entry, $shortcodes, $display, $show, $odd, $atts) { | |
if ( $display->ID == 14318 ) { | |
$results = masterminds_check_relationships( $entry->id ); | |
if ( $results && $results !== 'Login') { | |
$display_form_or_view = '[display-frm-data id=14593 filter=limited entry_id="' . $results . '"]'; | |
} else { | |
if ( $results == 'Login' ) { | |
$display_form_or_view = 'You must be logged in to view your relationship status with this media contact.'; | |
} else { | |
$display_form_or_view = '[formidable id=31 contactid="' . $entry->id . '"]'; | |
} | |
} | |
/* string substitution to display either the form for adding a new record or view for displaying/editing existing relationship record | |
* or login message if user is not logged in | |
*/ | |
$new_content = str_replace('[maybe_add_relationship]', $display_form_or_view, $new_content); | |
} | |
return $new_content; | |
} | |
/* function that checks if relationship record exists for specific media contact/user combination */ | |
function masterminds_check_relationships( $entry_id ) { | |
/* set up the custom query */ | |
global $wpdb; | |
$frm_item_metas_tbl = $wpdb->prefix . 'frm_item_metas'; | |
/* parameter passed from media_contact_relationships(() */ | |
$contactid = $entry_id; | |
/* get current user ID */ | |
$user = wp_get_current_user(); | |
/* if user is logged in, continue with display, otherwise display message to login */ | |
if ( $user->exists() ) { | |
$sql = 'SELECT t1.item_id FROM `' . $frm_item_metas_tbl .'` t1 LEFT JOIN `' . $frm_item_metas_tbl .'` t2 ON t1.item_id = t2.item_id WHERE t1.field_id = "253" AND t1.meta_value = "' . $contactid . '" AND t2.field_id = "256" AND t2.meta_value = "' . $user->ID . '"'; | |
/* results will return either an item_id for an existing relationship record or null if no record found */ | |
$results = $wpdb->get_var( $sql ); | |
if ( null !== $results ) { | |
return $results; | |
} else { | |
return false; | |
} | |
} else { | |
return 'Login'; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment