Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

WordPress

Varayut Lerdkanlayanawat
PLUS
Varayut Lerdkanlayanawat
Courses Plus Student 5,439 Points

How to force a user to install required plugins for my theme?

I just studied Wordpress Development courses. The Custom Post Type UI and Advanced Custom Fields plugins are recommended in order to handle new post types in a theme.

Let's say if I wanted to use them to create a Car type and a Train type, when I public this theme, how to let a user install these plugins and have these types ready for them to use?

Thanks in advance for your help.

2 Answers

Andrew Shook
Andrew Shook
31,709 Points

Well, you can simple code the post types into you theme using register_post_type. Which will take care of any post types, but will still leave ACF.

Officially, WP doesn't have a theme activation hook, but here is a tutorial for making your own. The article is old, so you may have to update the code a little. Once you get the theme activation hook going, you can then run something like this.

require_once( ABSPATH . '/wp-admin/includes/plugin.php' );
        if ( is_plugin_active( 'advanced-custom-fields/acf.php' ) ){
            require_once ( WP_PLUGIN_DIR . '/advanced-custom-fields/acf.php' );
        }else{
                  wp_die("'<div class="error"><p><strong>My Theme requires the Advances Custom Fields Plugin</strong></p></div>';");
            }
Zac Gordon
STAFF
Zac Gordon
Treehouse Guest Teacher

We're going to get into this in our advanced theme dev course later this year :)