Created
November 14, 2024 06:46
-
-
Save ipokkel/6f14062b42fb00884e6624825ec36842 to your computer and use it in GitHub Desktop.
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 | |
/** | |
* Add a user field file to the member links section. | |
* | |
* You can add this recipe to your site by creating a custom plugin | |
* or using the Code Snippets plugin available for free in the WordPress repository. | |
* Read this companion article for step-by-step directions on either method. | |
* https://www.paidmembershipspro.com/create-a-plugin-for-pmpro-customizations/ | |
*/ | |
function my_pmpro_member_links_bottom_link_file_user_field() { | |
$user_field_name = 'user_field_name_here'; // Your user field name for file. | |
// $link_text = 'Your Link Text Here'; // Optional: Your link text. Uncomment to use. | |
$user_id = get_current_user_id(); // Get the user ID. | |
// Do we have an active member and PMPro 3.0 or newer? | |
if ( empty( $user_id ) || ! pmpro_hasMembershipLevel() || ! function_exists( 'pmpro_get_user_field' ) ) { | |
return; | |
} | |
$user_field = pmpro_get_user_field( $user_field_name ); // Get the user field object. | |
// Do we have a user field and is it a file field? | |
if ( empty( $user_field ) || ( ! empty( $user_field ) ) && $user_field->type !== 'file' ) { | |
return; | |
} | |
// Use the user field label as the link text if not set. | |
if ( empty( $link_text ) ) { | |
$link_text = $user_field->label; | |
} | |
$user_field_value = get_user_meta( $user_id, $user_field_name, true ); // Get the user field value. | |
// Do we have a user field value and is it an array with a fullurl key? | |
if ( ! empty( $user_field_value ) && is_array( $user_field_value ) && ! empty( $user_field_value['fullurl'] ) ) { | |
?> | |
<li><a href="<?php echo esc_url( $user_field_value['fullurl'] ); ?>" target="_blank"><?php echo esc_html( $link_text ); ?></a></li> | |
<?php | |
} | |
} | |
add_action( 'pmpro_member_links_bottom', 'my_pmpro_member_links_bottom_link_file_user_field' ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment