Required Plugin Trick


Image from page 148 of “The Phynodderree, and other legends of the Isle of Man” (1882) flickr photo by Internet Archive Book Images shared with no copyright restriction (Flickr Commons) 1

Mark Luetke wrote a really nice plugin2 for us when he was here that helped faculty setup mother blogs. It requires FeedWordPress (FWP) to be active to work. This morning I had a faculty member who set everything up correctly except for FWP. It’s happened a few other times so I figured I’d do something about it.

I’d seen plugins that warn you if you don’t have a required plugin working so I went that route first.
The code below was lifted straight from this stackoverflow response and set to look for FWP.


dd_action( 'admin_init', 'child_plugin_has_parent_plugin' );
function child_plugin_has_parent_plugin() {
    if ( is_admin() && current_user_can( 'activate_plugins' ) &&  !is_plugin_active( 'feedwordpress/feedwordpress.php' ) ) {
        add_action( 'admin_notices', 'child_plugin_notice' );

        deactivate_plugins( plugin_basename( __FILE__ ) ); 

        if ( isset( $_GET['activate'] ) ) {
            unset( $_GET['activate'] );
        }
    }
}

function child_plugin_notice(){
    ?><div class="error"><p>Sorry, but Child Plugin requires the Parent plugin to be installed and active.</p></div><?php
}

It was decent and functional but I felt like I could make the notification better which led down a winding road.

Modal Popup

This code is straight from this codepen.3 And the little child_plugin_notice function got a bit larger.

unction child_plugin_notice(){
    ?><div class="modal" id="modal"> <button class="close-button" id="close-button" onclick="getEm()">Close this warning</button>
  		<div class="modal-guts">
  			<h2>Your Attention Please</h2>
  			<p>The ALT Lab MotherBlog plugin requires the FeedWordPress plugin to be installed and active. 
        I humbly ask you to activate FeedWordPress and then re-activate this plugin.</p>
        <p>FeedWordPress will be highlighted in <span class="fwp-focus">green</span> and after you close this window you'll probably automatically scroll down to it.</p>
  		</div>
  		<!--UGLY I know but it was late and the normal paths were not working-->
  		<script  type="text/javascript" charset="utf-8" >
  		
      var modal = document.querySelector("#modal");
      var modalOverlay = document.querySelector("#modal-overlay");
      var closeButton = document.querySelector("#close-button");
      var openButton = document.querySelector("#open-button");

        closeButton.addEventListener("click", function() {
        modal.classList.toggle("closed");
        modalOverlay.classList.toggle("closed");
        getEm();
        console.log('foo');
      });

ID FWP

Then I thought it’d be neat if I could scroll them down to FWP when they closed the modal popup. This could have been easy if the WP plugins table had ID elements that were rationale but it doesn’t. That led to a bit of an odyssey with various flavors of document.getElements. I still don’t understand why some of what I tried didn’t work but I eventually went with the following. It grabs all the text wrapped in strong tags and finds the one that matches FeedWordPress and then gives it an id of fwp-focus.

function getEm() {
            var b = document.getElementsByTagName("strong");         
            for (var i in b)
                if (b.hasOwnProperty(i)) {
                    var plugin = (b[i].innerHTML);
                    console.log(plugin);           
                    if (plugin === "FeedWordPress") {
                        b[i].setAttribute("id", "fwp-focus");

                    }
                }                
        }

Scroll To FWP

Now to auto scroll to our newly ID’d element on the close of the modal . . .

 jQuery("#close-button").click(function() {
        jQuery('html, body').animate({
            scrollTop: jQuery("#fwp-focus").offset().top-200}, 2000);
        });

I need to re-look at things when I’m less tired4 and get the scripts properly arranged and what not but it’s a fun little example in any case.


1 I do really enjoy being able to make stuff. Even if it’s not perfect. It’s really just so much fun.

2 TWO YEARS AGO!!! That means I haven’t had any technology help in roughly two years. Man.

3 Noting a theme here?

4 I help coach basketball for 6 year olds on Weds. and somehow I got stuck with dribbling.

3 thoughts on “Required Plugin Trick

  1. That’s rather slick, I need to give that a go. On reading first t thought it was heavy handing to deactivate a plugin but I see in video it’s at the point of activation for a plugin that won’t work w/o the required on.

    That’s a bit different than http://tgmpluginactivation.com

    but does this child plugin have any checks for the presence of FWP?

    The scroll trip is a winner 😉 and footnotes FTW

    1. That was the issue. You can activate MotherBlog without FWP and it’ll do 80% of what you expect . . . and 0% of what you want. Initially, I just did the warning (hey, you need FWP) but didn’t feel it’d necessarily be enough for faculty doing this only once or twice a year.

      I thought the scroll piece might be handy for others. PHP/JS-wise it’s awkward but in terms of user experience I think it’s pretty nice.

      Another thing I might try to figure out is activating FWP automatically (if it’s there) . . . not sure that’s possible. Seems like it could be a security issue.

Comments are closed.