Load module based on CPT

I’m able to do this by using:

function zestsms_load_conditional_bb_files() {
  global $post;
  if($post->post_type == 'people' || is_admin()) {
    require_once ZESTSMS_MODULES_DIR . 'practice-areas/practice-areas.php';
  }
}
add_action( 'init', 'zestsms_load_conditional_bb_files');
add_action( 'wp', 'zestsms_load_conditional_bb_files');

This way it shows up in wp-admin and only on the CPT “people”…great. But this seems a bit sloppy and I would need to have a few if statements for other CPT/modules.

Is there something more straightforward than this, perhaps in the __construct function when extending FLBuilderModule? For example:

class ZestSMSPracticeAreasModule extends FLBuilderModule {
    public function __construct()
    {
      global $wp_query;
        parent::__construct(array(
            'name'                => __('Practice Areas', 'zestsms'),
            'description'         => __('Display practice areas for current person.', 'zestsms'),
            'category'            => __('Custom Modules', 'zestsms'),
            'dir'                 => ZESTSMS_MODULES_DIR . 'practice-areas/',
            'url'                 => ZESTSMS_MODULES_URL . 'practice-areas/',
            'enabled'             => ($post->post_type == 'people' || is_admin()) ? true : false
        ));

    }
}

Or is this impossible since the module is being loaded in init?

My previous method doesn’t seem to work either – since the module is only loaded on init for wp-admin, the settings form doesn’t work.

Is loading a module based on CPT possible?

Hey Josh,

Just letting you know that we are looking into this and will get back to you shortly.

Thanks!

KC

Hey Josh,

Sorry for the delay, I was out yesterday.

Regarding the __construct function, there’s nothing there currently that would make this possible, however, I think your original method should work. After doing some testing, it looks like you need to change up your actions a bit. Specifically, I think the wp action needs to run sooner. Also, $post won’t exist on init, so that’s likely throwing an error. Give this a shot…

function zestsms_load_conditional_bb_files() {
	global $post;
	if(($post && $post->post_type == 'people') || is_admin()) {
		require_once ZESTSMS_MODULES_DIR . 'practice-areas/practice-areas.php';
	}
}
add_action('admin_init', 'zestsms_load_conditional_bb_files');
add_action('wp', 'zestsms_load_conditional_bb_files', 1);

Regarding the many if statements, you could abstract your logic that includes the files. Here’s an example of including two different modules that way.

function zestsms_load_conditional_bb_files() {

	global $post;
	
	$files = array(
		array(
			'path' => ZESTSMS_MODULES_DIR . 'practice-areas/practice-areas.php',
			'post_type' => 'people'
		),
		array(
			'path' => ZESTSMS_MODULES_DIR . 'books/books.php',
			'post_type' => 'books'
		)
	);
	
	foreach ( $files as $file ) {
		if ( ( $post && $post->post_type == $file['post_type'] ) || is_admin() ) {
			require_once $file['path'];
		}	
	}
}
add_action('admin_init', 'zestsms_load_conditional_bb_files');
add_action('wp', 'zestsms_load_conditional_bb_files', 1);

Let me know if you have any questions about that.

Justin

Hey guys, I approached this one from a slighting different angle but with the same general affect. I wanted to require my module classes regardless of what the post type is but then enable or disable them on CPT. Generally the same idea, but my version looks like this.


// In module constructor, setup action to check enabled later
function __construct() {
    // do module setup first.

    add_action('wp', array($this, 'set_enabled'));
}

function set_enabled() {
    global $post;
    if ($post->post_type != 'workspace') {
        $this->enabled = false;
    }
}

This lets me still use the class regardless of the post type but only display the module in the builder on the post type I want to scope it to.