一段时间以来,我一直在为这件事头痛。我的插件中有一段非常直接的代码:
add_action(\'init\', \'register_shortcodes\');
$shortcodeRegistrationRan = False;
function register_shortcodes() {
global $shortcodeRegistrationRan;
$shortcodeRegistrationRan = True;
echo "Ran = True";
add_shortcode( \'accountability\', array($hfMain, \'processAccountabilityShortcode\') );
}
问题是
$shortcodeRegistrationRan
从不设置为
True
, “Ran=True”也不会出现在页面的任何位置。我错过了什么?
我正在使用SimpleTest for Wordpress 检查我的跑步标志的状态。我还有很多其他通过的测试,所以测试套件运行良好。我的测试通过以下方式进行检查:
public function testShortcodeRegistrationRan() {
global $shortcodeRegistrationRan;
$this->assertEqual($shortcodeRegistrationRan, True);
}
在测试套件开始时:
require_once(dirname(__FILE__) . \'/../[PLUGIN NAME].php\');
EDIT: 根据@s\\u ha\\u dum的建议,我使用
shortcode_exists
功能:
public function testShortcodeRegistration() {
$this->assertEqual(shortcode_exists(\'accountability\'), True);
}
不幸的是,它也没有通过。
EDIT: 以下是完整的上下文:
<?php
/*
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");
}
register_activation_hook(__FILE__,"my_plugin_activate");
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)) {
//Actions
add_action(\'init\', \'register_shortcodes\'); //PROBLEMATIC LINE
//Filters
$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\') );
}