Skip to content

Instantly share code, notes, and snippets.

@butlerblog
Last active November 24, 2021 18:39
Show Gist options
  • Save butlerblog/71999f4cb8ec461f56fc2263e2b131bb to your computer and use it in GitHub Desktop.
Save butlerblog/71999f4cb8ec461f56fc2263e2b131bb to your computer and use it in GitHub Desktop.
WP-Members #API functions
<?php
add_action( 'wpmem_after_admin_init', 'my_add_custom_wpmembers_email' );
function my_add_custom_wpmembers_email() {
// Required arguments for wpmem_add_custom_email()
$tag = 'my_unique_msg_tag';
$heading = 'Heading to display input in Emails tab';
$subject_input = 'my_unique_subj_tag';
$message_input = 'my_unique_body_tag';
// Apply the arguments to the function.
wpmem_add_custom_email( $tag, $heading, $subject_input, $message_input );
}
/**
* When using a saved custom email, it is saved in wp_options using the $tag
* value. Subject and message body are arrayvalues keyed with 'subj' and 'body'.
* wp_mail() can be used to send the message.
* https://developer.wordpress.org/reference/functions/wp_mail/
*/
$email = get_option( 'my_unique_msg_tag' );
$email_subject = $email['subj'];
$email_message = $email['body'];
wp_mail( 'email@example.com', $email_subject, $email_message );
<?php
wpmem_array_insert( $array, $new, $key, 'after' );
<?php
$array = array(
'apples' => 'red',
'bananas' => 'yellow',
'blueberries' => 'blue',
);
$new = array(
'oranges' => 'orange',
);
/*
* This will insert "oranges=>orange" into the array
* AFTER bananas=>yellow
*/
wpmem_array_insert( $array, $new, 'bananas' );
/*
* This will insert "oranges=>orange" into the array
* BEFORE bananas=>yellow
*/
wpmem_array_insert( $array, $new, 'bananas', 'before' );
<?php
// Create a simple text field:
$args = array(
'name' => 'my_text_field',
'type' => 'text',
'value' => '', // default values can go here
'compare' => '', // comparison var could go here
);
$my_field = wpmem_form_field( $args );
// Create a text field with a placeholder and a custom class:
$args = array(
'name' => 'my_placeholder_text_field',
'type' => 'text',
'value' => '',
'compare' => '',
'class' => 'my-custom-css-class',
'placeholder' => 'Enter your stuff',
);
$my_placeholder_field = wpmem_form_field( $args );
<?php
/*
* Use wpmem_form_field() to create a new username field
* with placeholder text and use wpmem_register_form_rows
* to replace the username field with the placeholder field.
*
* @see: https://rocketgeek.com/plugins/wp-members/docs/filter-hooks/wpmem_register_form_rows/
* @see: https://rocketgeek.com/plugins/wp-members/docs/api-functions/wpmem_form_field/
*/
function my_placeholder_username( $rows ) {
// Arguments to create a form field with wpmem_form_field()
$username_args = array(
'name' => 'user_login',
'type' => 'text',
'value' => '',
'compare' => '',
'placeholder' => 'Choose a Username',
);
// Create form field with wpmem_form_field()
$username_field = wpmem_form_field( $username_args );
// Replace existing field with new field (and remove label)
$rows['username']['label'] = '';
$rows['username']['field'] = $username_field;
// Return new $rows array
return $rows;
}
<?php
// An example of creating a form field label:
$args = array(
'meta_key' => 'my_field_meta_key',
'label' => 'My Field',
'type' => 'text',
);
$my_label = wpmem_form_label( $args );
<?php
$name = 'var_name'; // required
$default = 'default_value'; // optional, default: null
$method = 'post'; // optional (post|get|request), default: post
$var = wpmem_get( $name, $default, $method );
<?php
// Instead of this:
$var = ( isset( $_POST['form_var'] ) ) ? $_POST['form_var'] : '';
// wpmem_get() handles it like this:
$var = wpmem_get( 'form_var' );
// Instead of this:
$var = ( isset( $_GET['form_var'] ) ) ? $_GET['form_var'] : 'default';
// wpmem_get() handles it like this:
$var = wpmem_get( 'form_var', 'default', 'get' );
<?php
// Example retrieves the default login heading text ('restricted_msg')
$message = wpmem_gettext( 'login_heading' );
<?php
$defaults = array(
// Login form.
'login_heading' => __( 'Existing Users Log In', 'wp-members' ),
'login_username' => __( 'Username' ),
'login_password' => __( 'Password' ),
'login_button' => __( 'Log In' ),
'remember_me' => __( 'Remember Me' ),
'forgot_link_before' => __( 'Forgot password?', 'wp-members' ) . '&nbsp;',
'forgot_link' => __( 'Click here to reset', 'wp-members' ),
'register_link_before' => __( 'New User?', 'wp-members' ) . '&nbsp;',
'register_link' => __( 'Click here to register', 'wp-members' ),
'username_link_before' => __( 'Forgot username?', 'wp-members' ) . '&nbsp;',
'username_link' => __( 'Click here', 'wp-members' ),
// Password change form.
'pwdchg_heading' => __( 'Change Password', 'wp-members' ),
'pwdchg_password1' => __( 'New password' ),
'pwdchg_password2' => __( 'Confirm new password' ),
'pwdchg_button' => __( 'Update Password', 'wp-members' ),
// Password reset form.
'pwdreset_heading' => __( 'Reset Forgotten Password', 'wp-members' ),
'pwdreset_username' => __( 'Username' ),
'pwdreset_email' => __( 'Email' ),
'pwdreset_button' => __( 'Reset Password' ),
// Retrieve username form.
'username_heading' => __( 'Retrieve username', 'wp-members' ),
'username_email' => __( 'Email Address', 'wp-members' ),
'username_button' => __( 'Retrieve username', 'wp-members' ),
// Register form.
'register_heading' => __( 'New User Registration', 'wp-members' ),
'register_username' => __( 'Choose a Username', 'wp-members' ),
'register_rscaptcha' => __( 'Input the code:', 'wp-members' ),
'register_tos' => __( 'Please indicate that you agree to the %s TOS %s', 'wp-members' ),
'register_clear' => __( 'Reset Form', 'wp-members' ),
'register_submit' => __( 'Register' ),
'register_req_mark' => '<span class="req">*</span>',
'register_required' => '<span class="req">*</span>' . __( 'Required field', 'wp-members' ),
// User profile update form.
'profile_heading' => __( 'Edit Your Information', 'wp-members' ),
'profile_username' => __( 'Username' ),
'profile_submit' => __( 'Update Profile', 'wp-members' ),
'profile_upload' => __( 'Update this file', 'wp-members' ),
// Error messages and dialogs.
'login_failed_heading' => __( 'Login Failed!', 'wp-members' ),
'login_failed' => __( 'You entered an invalid username or password.', 'wp-members' ),
'login_failed_link' => __( 'Click here to continue.', 'wp-members' ),
'pwdchangempty' => __( 'Password fields cannot be empty', 'wp-members' ),
'usernamefailed' => __( 'Sorry, that email address was not found.', 'wp-members' ),
'usernamesuccess' => __( 'An email was sent to %s with your username.', 'wp-members' ),
'reg_empty_field' => __( 'Sorry, %s is a required field.', 'wp-members' ),
'reg_valid_email' => __( 'You must enter a valid email address.', 'wp-members' ),
'reg_non_alphanumeric' => __( 'The username cannot include non-alphanumeric characters.', 'wp-members' ),
'reg_empty_username' => __( 'Sorry, username is a required field', 'wp-members' ),
'reg_password_match' => __( 'Passwords did not match.', 'wp-members' ),
'reg_email_match' => __( 'Emails did not match.', 'wp-members' ),
'reg_empty_captcha' => __( 'You must complete the CAPTCHA form.', 'wp-members' ),
'reg_invalid_captcha' => __( 'CAPTCHA was not valid.', 'wp-members' ),
// Links.
'profile_edit' => __( 'Edit My Information', 'wp-members' ),
'profile_password' => __( 'Change Password', 'wp-members' ),
'register_status' => __( 'You are logged in as %s', 'wp-members' ),
'register_logout' => __( 'Click to log out.', 'wp-members' ),
'register_continue' => __( 'Begin using the site.', 'wp-members' ),
'login_welcome' => __( 'You are logged in as %s', 'wp-members' ),
'login_logout' => __( 'Click to log out', 'wp-members' ),
'status_welcome' => __( 'You are logged in as %s', 'wp-members' ),
'status_logout' => __( 'click to log out', 'wp-members' ),
// Widget.
'sb_status' => __( 'You are logged in as %s', 'wp-members' ),
'sb_logout' => __( 'click here to log out', 'wp-members' ),
'sb_login_failed' => __( 'Login Failed!<br />You entered an invalid username or password.', 'wp-members' ),
'sb_not_logged_in' => __( 'You are not logged in.', 'wp-members' ),
'sb_login_username' => __( 'Username' ),
'sb_login_password' => __( 'Password' ),
'sb_login_button' => __( 'log in', 'wp-members' ),
'sb_login_forgot' => __( 'Forgot?', 'wp-members' ),
'sb_login_register' => __( 'Register' ),
// Default Dialogs.
'restricted_msg' => __( "This content is restricted to site members. If you are an existing user, please log in. New users may register below.", 'wp-members' ),
'user' => __( "Sorry, that username is taken, please try another.", 'wp-members' ),
'email' => __( "Sorry, that email address already has an account.<br />Please try another.", 'wp-members' ),
'success' => __( "Congratulations! Your registration was successful.<br /><br />You may now log in using the password that was emailed to you.", 'wp-members' ),
'editsuccess' => __( "Your information was updated!", 'wp-members' ),
'pwdchangerr' => __( "Passwords did not match.<br /><br />Please try again.", 'wp-members' ),
'pwdchangesuccess' => __( "Password successfully changed!", 'wp-members' ),
'pwdreseterr' => __( "Either the username or email address do not exist in our records.", 'wp-members' ),
'pwdresetsuccess' => __( "Password successfully reset!<br /><br />An email containing a new password has been sent to the email address on file for your account.", 'wp-members' ),
); // End of $defaults array.
<?php
wpmem_is_reg_page();
<?php
if ( wpmem_is_reg_page() ) {
// This is the registration page.
} else {
// This is not the registration page.
}
<?php
// Displays the user's login status when logged in
wpmem_login_status();
<?php
// Display a link to the login page.
echo '<a href="' . wpmem_login_url() . '">Log In Here</a>';
// The above will output "Log In Here" (with a link to the login page URL).
<?php
// Compare current page to see if we are on the login page.
$current_page = trailingslashit( wpmem_current_url() );
$login_page = trailingslashit( wpmem_login_url() );
if ( $current_page == $login_page ) {
echo 'you are on the login page';
}
// Better version of above, accounts for presence of a
// query string, such as ?redirect_to=
$current_page = trailingslashit( wpmem_current_url( true, false ) );
$login_page = trailingslashit( wpmem_login_url() );
if ( $current_page == $login_page ) {
echo 'you are on the login page';
}
<?php
// Default use:
echo wpmem_loginout();
// Example using all possible arguments.
$args = array(
'login_redirect_to' => 'http://mysite.com/after-login/',
'login_text' => 'log in here',
'logout_text' => 'click to logout',
);
wpmem_loginout( $args, true );
<?php
// Display a link to the user profile page.
echo '<a href="' . wpmem_profile_url() . '">Update My Settings</a>';
// The above will output "Update My Settings" (with a link to the user profile page URL).
<?php
// Compare current page to see if we are on the user profile page.
$current_page = trailingslashit( wpmem_current_url() );
$profile_page = trailingslashit( wpmem_profile_url() );
if ( $current_page == $profile_page ) {
echo 'you are on the user profile page';
}
<?php
// Redirects all pages except login, register, and profile url to the login page.
$pages = wpmem_user_pages();
if ( ! is_user_logged_in() && ! in_array( wpmem_current_url( true, false ), $pages ) ) {
wpmem_redirect_to_login();
}
<?php
// Display a link to the register page.
echo '<a href="' . wpmem_register_url() . '">Register Here</a>';
// The above will output "Register In Here" (with a link to the register page URL).
<?php
// Compare current page to see if we are on the register page.
$current_page = trailingslashit( wpmem_current_url() );
$register_page = trailingslashit( wpmem_register_url() );
if ( $current_page == $register_page ) {
echo 'you are on the registration page';
}
<?php
// Validate if user has the role of subscriber.
$is_subscriber = wpmem_user_has_role( 'subscriber' );
// If the current user has 'subscriber' role, $is_subscriber will be true.
<?php
// Get an array of login, register, and profile pages.
$pages = wpmem_user_pages();
/**
* $pages will contain an array as follows:
*
* array(
* http://mysite.com/my-login-page/,
* http://mysite.com/my-register-page/,
* http://mysite.com/my-profile-page/,
* )
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment