Retrieving selections from a multiselect Select Field with select2.js

Recently I needed to add a multi-select field to a project. I used the Select2JS library to achieve just that.

Just to always remember, you can retrieve the selection with PHP (form handler) by setting the field’s name attribute to an array, e.g.:

<select name="myFieldValues[]">

Then on the PHP form handler file you can catch the fields as follows:

$valuesArray = $_POST['myFieldValues'];

Do a var_dump of the array so you can see the array.

echo '<pre>'.
var_dump($valuesArray).
'</pre>';

An array of values ‘PHP’, ‘HTML’, ‘NODEJS’, ‘WORDPRESS’, ‘WORDPRESS HOOKS’, should display as follows:

Array display

Remove post types from the WordPress search feature

Edited: February 4th, 2023.

The base content type structure in WordPress are posts. Pages, Comments, Attachments, etc., are posts in WordPress.

Some plugins add their own custom post types to take advantage of the base content structure. For example, WooCommerce adds the ‘product’ custom post type. LearnDash, adds the ‘sfwd-courses’ custom post type for courses. And so on.

These custom post types, by default, are enabled to be searched. When you do a search in WordPress, the WordPress search will bring all the matching posts within the custom post types enabled to be searched.

But sometimes you may want to prevent the WordPress search from including results from certain post types.

You can remove post types from the WordPress search feature by hooking into the register_post_type_args action in WordPress.

Post types are registered with a series of arguments, that set the properties they will have. Within the arguments, you can find one called ‘exclude_from_search’.

Keep in mind, you need to know the name of the custom post type you want to exclude from the search.

Let’s say, you want to exclude WooCommerce products.

A function that exclude products would be as follows:

function obi_exclude_woocommerce_products_from_search($args, $post_type){
     if( 'product' == $post_type){
          $args['exclude_from_search'] = true;
     }
     return $args;
}

add_action('register_post_type_args', obi_exclude_woocommerce_products_from_search');

Edit:

To exclude multiple post types from the search, you can use a code like this:

/* LOOP POST TYPES */

add_action( 'register_post_type_args', 'mws_loop_post_types', 1, 2);
 
function mws_loop_post_types( $args, $post_type ) {
	
	$mws_post_types = array('sfwd-courses', 'sfwd-lessons'); // Add the slug for as many post types as you want.
	
	// EXCLUDES FROM SEARCH ONLY THE SPECIFIC POST TYPES IN THE ARRAY mws_post_types
	
	for ($i = 0; $i <= (sizeof($mws_post_types)) ; $i++) {
		if( ($mws_post_types[$i]) == $post_type ){
			$args['exclude_from_search'] = false;
		}
	}
	
	
	return $args;
}

I have nothing to hide

Years ago, I was part of the “next shiny thing” tribe.

Something new popped up, that something I had to buy it.

A new Twitter method, a new ebook, a new software tool, whatever, on how to make money online.

That behavior was not healthy at all.

But thanks to that, I stumbled upon an idea that help me move towards what I want: “Do a business that you can tell your mom about”.

Well, I was a teenager and I don’t recall the exact same words. But the main idea stuck with me.

In businesses, and life in general, do something that you can tell your mom about. Whether you feel comfortable telling your mom you are a drug lord or a dentist, whatever it is, go for it.

There is really nothing you can’t do. Of course, always within your moral sense, ethic and the law.

Redirect visitors from the course page to the product or landing page in LearnDash

You have a course set to Closed access mode. That means users will purchase the course either through a third-party eCommerce like WooCommerce, or a membership plugin like Paid Memberships Pro.

Sweet.

When a visitor clicks the course from the course list, they are taken to the single course page by default. Then, they click the ‘Take this course’ call to action to go to the purchase page.

Too many steps.

What if you want to cut the process to click the course > go to checkout?

What if you want to prevent visitors from seeing the course content before enrolling?

What if you want both?

You need to redirect the user to the value you set in the Button URL field in your course access mode setting.

But how?

Interestingly, the solution is straightforward. The steps are as follows

  1. Hook into the template_redirect action.
  2. Check if the current post is a course.
  3. Check if the current user is admin. //admin
  4. Check if the current user is not enrolled in the current course.
  5. Retrieve the course options (serialized array).
  6. Check if the access mode is set to Closed.
  7. Check if the Button URL field is not empty.

Now, with the steps defined, let’s get working.

NOTE: You can find the final code here: https://gist.github.com/j2machado/c45e979fca472e8cab2520ec0dce195a

1 – Hook into the template_redirect action.

The template_redirect action is fired before determining which template will be loaded. This is perfect to decide whether to allow the single course page to load, or redirect the user to our specific destination.

Create a function that will hold all the logic for this feature. I’ll call it obi_closed_course_reditect().

function obi_closed_course_reditect(){
//Code will go here

}

Then hook the function into the template_redirect action:

add_action('template_redirect', 'obi_closed_course_redirect');

Good. Your function is now hooked into the template_redirect action. As it doesn’t have any logic yet, nothing will happen.

2 – Check if the current post is a course.

Courses in LearnDash are defined with the ‘swfd-courses’ post type. The get_post_type() function allows you to retrieve the current post’s post type.

Set a conditional to check if the post is not a course. If true, return false.

if( 'sfwd-courses' != get_post_type() ){
     return
}

3 – Check if the current user is admin.

You are only interested in redirecting a user if they are non-logged-in visitors or regular users on your website but have not enrolled in your course yet.

You can achieve this by checking the user by their current capabilities. A unique capability for admins is ‘manage_options’. Then, filter users out like this:

if (current_user_can('manage_options'){
     return
}
if (current_user_can('manage_options'){
     return
}

4 – Check if the current user is not enrolled in the current course.

Give a current enrollee access to the single course page. LearnDash provides a function to check the current user enrollment status: ld_course_check_user_access().

if( ld_course_check_user_access( get_the_ID() , get_current_user_ID() )){
		return;
	}

5 – Retrieve the course options (serialized array).

The course options are stored in the database as metadata of the course post. The relevant meta key is ‘_sfwd-courses’. Use the get_post_meta() function to retrieve the course options and store them in a variable.

$courseOptionsSerialized = get_post_meta( get_the_ID(), '_sfwd-courses', true);

6- Check if the access mode is set to Closed.

The course options are stored in the $courseOptionsSerialized variable as a serialized array.

You can access any option within the array by key. The course access mode is stored in the database as course price type. You can get this value like this

$courseOptionsSerialized[‘sfwd-courses_course_price_type’];.

You can store that value in a variable as well, as follows:

$courseAccessMode = $courseOptionsSerialized['sfwd-courses_course_price_type'];

Then, create a conditional to filter out courses whose access mode is other than Closed.

if($courseAccessMode != "closed"){
		return;
	}

7 – Check if the Button URL field is not empty.

A course access mode can be set to Closed, but the Button URL is empty. Prevent the redirect if the Button URL value is not set.

if($courseProductURL == ""){
		return;
	}

Final Step!

Redirect the user after all conditions have been passed. You can use the wp_safe_redirect() function.

wp_safe_redirect( $courseProductURL );
exit;

You now have a redirection for your courses based on a set of conditions. The redirection works for all courses on your website.

You can find the final code here: https://gist.github.com/j2machado/c45e979fca472e8cab2520ec0dce195a