Skip to content

Instantly share code, notes, and snippets.

@philipdowner
Created May 6, 2015 14:48
Show Gist options
  • Save philipdowner/628f1626aad28a531057 to your computer and use it in GitHub Desktop.
Save philipdowner/628f1626aad28a531057 to your computer and use it in GitHub Desktop.
Allow WordPress user to update category subscriptions
<?php
/**
* Allow users to save categories they wish to subscribe to
*/
//First make sure the user is logged in
if( !is_user_logged_in() || !current_user_can('read_posts') ) { //You can change the capability from 'read_posts' if needed
echo '<div class="loginForm">';
echo '<p>Please login to manage your subscription preferences.</p>';
wp_login_form();
echo '</div>';//.loginForm
}
//The user is logged in
else {
$userID = get_current_user_id(); //Get the user's database ID
//Get a list of the terms in the category
$terms = get_terms('category', array(
'hide_empty' => false,
'hierarchical' => false,
));
//Get the user's current subscription preferences
$currentCategories = maybe_unserialize(get_user_meta($userID, 'subscription_category', true));
//If no current categories make the var an array so we can use it later
if( $currentCategories === '' ) $currentCategories = array();
//Show the subscription form
if( !isset($_POST) || !array_key_exists('subscription_save', $_POST) ) {
echo '<form action="'.get_permalink($post->ID).'" method="post" id="subscriptionSave">';
echo '<label for="categoryCheckboxes">Choose the categories you wish to subscribe to:</label>';
echo '<div id="categoryCheckboxes">';
//Loop thru each term and provide a checkbox for each
foreach( $terms as $term ) {
echo '<input type="checkbox" name="subscriptionCategories['.$term->term_id.']" value="'.$term->term_id.'" '.checked($term->term_id, in_array($term->term_id, $currentCategories), false).' />';
echo '<label for="subscriptionCategories['.$term->term_id.']">'.$term->name.'</label>';
}
echo '</div>';//#categoryCheckboxes
//Show a submit button
echo '<input type="submit" name="subscription_save" value="Save My Preferences" />';
echo '</form>';
}
//Process the subscription form
else {
//Make sure at least 1 checkbox was selected, if not, delete the user meta
if( empty($_POST['subscriptionCategories']) ) {
delete_user_meta($userID, 'subscription_category');
}
//Get only the term IDs
$termIDs = get_terms('category', array(
'hide_empty' => false,
'hierarchical' => false,
'fields' => 'ids'
));
//Check the submission against the list of term IDs to ensure no funny business
foreach( $_POST['subscriptionCategories'] as $key => $value ) {
if( !in_array($value, $termIDs) ) unset($_POST['subscriptionCategories'][$key]);
}
//First we check whether the user already has a meta field saved
if( empty($currentCategories) ) {
//No user meta exists, use add_user_meta, so we can ensure the field is unique
if( add_user_meta($userID, 'subscription_category', maybe_serialize($_POST['subscriptionCategories']), true) !== false ) {
//Show a success message
echo '<div class="message success">';
echo '<p>Thank you for updating your subscription preferences.</p>';
echo '</div>';
}
//Show a failure message
else {
echo '<div class="message failure">';
echo '<p>Something went wrong and we could not save your subscription preferences.</p>';
echo '</div>';
}
}
//The user already has meta saved
else {
//Update the user meta, because it was created as a unique field, we do not need to specify a previous value.
if( update_user_meta($userID, 'subscription_category', maybe_serialize($_POST['subscriptionCategories'])) !== false ) {
//Show a success message
echo '<div class="message success">';
echo '<p>Thank you for updating your subscription preferences.</p>';
echo '</div>';
}
//Show a failure message
else {
echo '<div class="message failure">';
echo '<p>Something went wrong and we could not save your subscription preferences.</p>';
echo '</div>';
}
}//if( empty($currentCategories) )
}//if( !isset($_POST) || !array_key_exists('subscription_save', $_POST) )
}//if( !is_user_logged_in() || !current_user_can('read_posts') )
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment