Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I've been banging my head against this for a while. I have this very straight-forward code in my plugin:

add_action('init', 'register_shortcodes');

$shortcodeRegistrationRan = False;

function register_shortcodes() {
    global $shortcodeRegistrationRan;
    $shortcodeRegistrationRan = True;
    echo "Ran = True";

    add_shortcode( 'accountability', array($hfMain, 'processAccountabilityShortcode') );

The problem is $shortcodeRegistrationRan never gets set to True, nor does "Ran = True" appear anywhere on the page. What am I missing?

I'm using SimpleTest for Wordpress to check the status of my run flag. I have plenty of other passing tests, so the test suite is running fine. My test checks this way:

public function testShortcodeRegistrationRan() {
    global $shortcodeRegistrationRan;
    $this->assertEqual($shortcodeRegistrationRan, True);

At the beginning of the test suite:

require_once(dirname(__FILE__) . '/../[PLUGIN NAME].php');

EDIT: Per @s_ha_dum's advice, I added a new test using the shortcode_exists function:

public function testShortcodeRegistration() {
    $this->assertEqual(shortcode_exists('accountability'), True);

Unfortunately, it, too, does not pass.

EDIT: Here is the full context:

Plugin Name: Nathan's Awesome Widget
Plugin URI: http://NathanArthur.com/
Description: This plugin does awesome things
Author: Nathan Arthur
Version: 1.0
Author URI: http://NathanArthur.com/

/*global $wp_version;

if ( !version_compare($wp_version,"3.0",">=") ) {
    die("You need at least version 3.0 of Wordpress to use the copyright plugin");

function my_plugin_activate() {
    error_log("my plugin activated");


if (!class_exists("HfAccountability")) {
    class HfAccountability {
        private $currentUser;
        function HfAccountability() { //constructor
            $this->currentUser = wp_get_current_user();
        function getCurrentUserLogin() {
            return $this->currentUser->user_login;
        function processAccountabilityShortcode( $atts ) {
            #$to = '[MY EMAIL ADDRESS]';
            #$subject = 'Testing Mandrill integration';
            #$message = "Yup, that's all we're doing";
            #wp_mail( $to, $subject, $message, $headers, $attachments );
            return 'We tried.';

if (class_exists("HfAccountability")) {
    $hfMain = new HfAccountability();

//Actions and Filters
/* if (isset($hfMain)) {
    add_action('init', 'register_shortcodes'); //PROBLEMATIC LINE


    $actionsRan = True;
} */

add_action('init', 'register_shortcodes');

$shortcodeRegistrationRan = False;

function register_shortcodes() {
    global $shortcodeRegistrationRan;
    $shortcodeRegistrationRan = True;
    echo "Ran = True";

    add_shortcode( 'accountability', array($hfMain, 'processAccountabilityShortcode') );
share|improve this question
The question is where do you access this variable? A variable in functions.php isn't accessible anywhere else. This is a matter of scope. –  kraftner Feb 17 '14 at 16:36
Yes, I realized this after I posted the question — see the edited question for how I attempted to address this. Unfortunately, my attempt didn't resolved the issue. –  Nathan Arthur Feb 17 '14 at 16:38
Did you try printing $shortcodeRegistrationRan in testShortcodeRegistrationRan() to see if the problem is the value not being there or something further on in your testing tool? –  kraftner Feb 17 '14 at 16:42
I did, but the code has changed enough that it'd be worth trying again. I'll do that right now. –  Nathan Arthur Feb 17 '14 at 16:43
@kraftner: Just tried a simple echo (see edit). Still no luck. –  Nathan Arthur Feb 17 '14 at 16:47

2 Answers 2

I am going to suggest that what you are doing is overly complex, at least insofar as I understand you.

You can determine whether your shortcode has been registered using the Core function shortcode_exists, which returns a boolean. As far as I can tell, using that should provide completely equivalent functionality to your code above.

share|improve this answer
Wow, that is a great function! Thank you! I added a test that uses this function, but unfortunately it does not pass. –  Nathan Arthur Feb 17 '14 at 19:59
Then there is probably something contextual happening that isn't clear from your question. –  s_ha_dum Feb 17 '14 at 20:01
I guess I can add the context. –  Nathan Arthur Feb 17 '14 at 20:03
Just edited to include the surrounding code. –  Nathan Arthur Feb 17 '14 at 20:08
up vote 0 down vote accepted

I feel very un-smart. My problem was two-fold:

  1. First, my plugin had somehow become deactivated.
  2. Second, it turned out that I needed to give the add_shortcode function the name of the class rather than an instance of that class. It'd be great if someone could explain why this is. This page indicated that either way would work.

So, it's finally all working and my tests are passing.

share|improve this answer
It is possible to register class functions in object context. In this/your case, however, you first have to globalize your variable/object. Just put global $hfMain; in your register_shortcodes function and all should work fine. –  t f Feb 18 '14 at 15:15
Oh, right! That makes sense. I can see right now I need to be a lot more careful with scope. lol –  Nathan Arthur Feb 18 '14 at 15:31

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.