Redirects to the Home URL after Logout from the Admin (or elsewhere)
/** *Used to redirect if logout */ function logout_page() { $login_page = home_url(); wp_redirect( $login_page . "" ); exit; } add_action('wp_logout','logout_page');
Redirects to the Home URL after Logout from the Admin (or elsewhere)
/** *Used to redirect if logout */ function logout_page() { $login_page = home_url(); wp_redirect( $login_page . "" ); exit; } add_action('wp_logout','logout_page');
This snippet allows “Members only” sites.
It redirects always to a custom PHP template (noaccess) if the user is not logged in, and obviously it will NOT redirect anywhere if the User access the custom template (noaccess).
Other wise a infinite loop would be generated.
/** *Used to redirect if not logged in */ function admin_redirect() { if ( !is_page('noaccess') && ! is_user_logged_in() ) { wp_redirect( home_url('/noaccess/') ); exit; } } add_action('get_header', 'admin_redirect');
Removes the WP Admin Bar for a given Role (contributor in this case)
function remove_admin_bar_user() { if( current_user_can('contributor')) { show_admin_bar(false); } } add_action('after_setup_theme', 'remove_admin_bar_user');
Create a View with parametric search and a plotted map listing posts of a CPT.
All of those posts will have different authors.
All authors will either own a certain capability (managed by the groups plugin that stores these capabilities in the plugin’s own tables) or not.
Retrieve the capability of the author by using this function:
which returns 1 if the author has the capability and 0 if the author does not have the capability.
This wpv_filter_query_post_process
now lists the posts of authors who own the capability – means where the above function would return 1.
add_filter( 'wpv_filter_query_post_process', 'prefix_modify_empty_query', 10, 3 ); function prefix_modify_empty_query( $query, $view_settings, $view_id ) { if ($view_id == 41) { // get some posts (author-profile cpt) $args = array( 'posts_per_page' => -1, 'post_type' => 'author-profile', 'post_status' => 'publish', ); $posts_array = new WP_Query($args); //If there are some posts if (!empty($posts_array->posts)) { //build a array for valid (to return) posts $valid_posts = array(); //we need to check if the posts author has the correct cap foreach ($posts_array->posts as $post_array) { //this custom code checks wehter the author (user) is / has capability 'pp_speaker' require_once( ABSPATH . 'wp-includes/pluggable.php' ); $groups_user = new Groups_User(get_the_author_meta('ID', $post_array->post_author)); $can_make_donuts = $groups_user->can('pp_speaker'); if ($can_make_donuts) $valid_posts[] = $post_array; } } //If there are posts with this author / cap if (!empty($valid_posts)) { //assign the valid posts array to the query $query->posts = $valid_posts; } } return $query; }
Let a user edit his existing WordPress Profile directly from the FrontEnd through a CPT Edit/Create CRED form.
Note that this function assumes a setup with the correct CPT’s, custom fields, user CF’s.
The Process of this example CRED Form will create a CPT “user Profile” entry , which can be used for front-end displaying user profiles (with a content template as example).
During this, it will get the CPT field values and update the USER fields with it.
This example is also useful to see how checkboxes (multi-options) are different then “normal” CF’s.
function user_save_data_action($post_id, $form_data) { if ($form_data['id']==ID) { //get curretn user's ID $user_id = get_current_user_id(); //get Skype post_meta field value he submits in post //note: any other SINGLE option Field applies to below "rule") $skype = get_post_meta( $post_id, 'wpcf-skype', true ); //update the user_meta field with this value update_user_meta($user_id, 'wpcf-skype', $skype); //repeats as above //again, ANY single value Field can be used (email, URL, etc) $phone = get_post_meta( $post_id, 'wpcf-phone-number', true ); update_user_meta($user_id, 'wpcf-phone', $phone); $pic = get_post_meta( $post_id, 'wpcf-profile-photo', true ); update_user_meta($user_id, 'wpcf-profile-image', $pic); //now the tricky part, we are handling with CheckBoxes, multiple Options! //AGAIN, THANK YOU +Ana Couto !!!! //Check if actually the post_meta Checkboxes field has value(s) if ( isset($_POST['wpcf-languages'])){ //if yes, get all the user meta fields and custom fields information (of user + post) $langs_user = get_option( 'wpcf-usermeta' );//users fields $langs_fields = get_option( 'wpcf-fields' );//posts fields //refine this, we only want specific fields, actually the "languages" and "support-languages" fields //being those the checkboxes fields (with options), once for user, once for post $langs_user_options = $langs_user['support-languages']['data']['options'];//user field $langs_fields_options = $langs_fields['languages']['data']['options'];//post field //check the field's values against each other (user_meta vs post_meta field values) $langs_selected = $_POST['wpcf-languages'];//get values from post field (all of them) //define the data with which we are going to update user_meta field $user_langs = array(); foreach ($langs_selected as $fields_option_key) { if (isset($langs_fields_options[$fields_option_key])) { $value = $langs_fields_options[$fields_option_key]['set_value']; foreach ($langs_user_options as $user_option_key => $user_option) { if ($user_option['set_value'] == $value) { $user_langs[$user_option_key] = array($value); break; } } } } //update the user_meta field update_user_meta($user_id, 'wpcf-support-languages', $user_langs); } //Now it get's easy again :) //Update post_name (slug) with user_name (author) //we don't want author to enter a title for his profile, this should be done automatically $custom_title = wp_get_current_user();//get user, define where to get info from for new title $new_title = $custom_title->user_login;//get login name from user //collect data and define new title $my_post = array( 'ID' => $post_id, 'post_name' => $new_title, 'post_title' => $new_title,//we NEED to do that, otherwise, AUTO DRAFT will be post name ); // Update the post into the database wp_update_post( $my_post ); } } add_action('cred_save_data', 'user_save_data_action',10,2);
Download and add to your theme:
http://malsup.github.com/jquery.form.js
Add to your Functions PHP:
wp_enqueue_script("jquery"); wp_enqueue_script( 'jquery-form-js', get_template_directory_uri() . '/js/jquery.form.js', array('jquery'), '3.51.0-2014.06.20', true );
In any SINGLE CRED form JS Editor:
jQuery(document).ready(function() { jQuery('#cred_form_4988_1').ajaxForm(function() { alert("Thank you for your comment!"); }); });
Replace #cred_form_4988_1
with the actual ID of the CRED Form you’ll see in your browser console asa in this:
<form enctype="multipart/form-data" id="cred_form_4988_1" class="cred-form cred-keep-original" action="/toolset/2015/07/08/ajax-edit/?cred-edit-form=4988&_tt=1436396327" method="post">
If you have MULTIPLE CRED FORMS (of same form), outputted in a Views Loop
Means, you got x posts, and therefore x CRED Forms of same type,the ID of CRED is continuously increased by one, so the a above jQuery breaks. You should use here:
jQuery(document).ready(function() { jQuery("[id^=cred_form_1096]").ajaxForm(function() { alert("Thank you for your comment!"); }); });
We use now ("[id^=PART_OF_ID]")
You can also seek for all elements which contain (*=)
or end up with ($=)
known part of ID/class/type.
(example, id^, id*, id$)
DOC Links:
http://malsup.com/jquery/form/#getting-started
You can retrieve that data through tmp_name. The first and second indexes of this array are width and height, respectively
function validate_image_width_height($field_data, $form_data){ list($fields,$errors)=$field_data; $image_info = $fields['wpcf-image-1']['value']['tmp_name']; $image_width = $image_info[0]; $image_width = $image_info[1]; if ($image_width < 100){ $errors['image-1'] = __('Wrong width', 'nbt'); } var_dump($fields['wpcf-image-1']); return array($fields,$errors); } add_filter('cred_form_validate','validate_image_width_height',10,2);
Use the Code Snippet button to enter php and html snippets.
Limit the number of featured posts which user can submit. This Code limit to 1 featured post for each user.
The “wpcf-mls” is the custom field which define if the post is featured or not. It should store 1 for featured.
This hook will be triggered as soon as this custom field sent “1”.
Then check how many featured posts the current user has. Limit this number to “1”.
add_filter('cred_form_validate','my_validation',10,2); function my_validation($field_data, $form_data){ global $wpdb; list($fields,$errors)=$field_data; if ($form_data['id']==241){ if ($fields['wpcf-mls']['value'] == 1){ $results = $wpdb->get_results( 'SELECT * FROM '.$wpdb->posts.' post, '.$wpdb->postmeta.' meta WHERE post.ID = meta.post_id and post.post_status = "publish" and meta.meta_key = "wpcf-mls" and meta.meta_value = "1" AND post.post_author = "'.get_current_user_id().'"', OBJECT ); if (count($results) > 1){ $errors['mls']='Your limit for featured posts is 1'; } } return array($fields,$errors); } }