Hide every page but one if user is not logged in

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');

Views Custom Query Filter – Check for custom Author cap.

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;
}

Submit CRED Form(s) with AJAX

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&amp;_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

Validate width and height of image with CRED

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 a ‘n’ of posts users can submit with CRED

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);
   }
}