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?
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…
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.
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.