我想用is_plugin_active
在功能内。php。
我需要把我的useragent_shortcode
内的功能is_plugin_active
因为当联系人表单7停用时,我会看到一个白色屏幕。
我已经包括在内了wp-admin/includes/plugin.php
如其他WPSE答案所示,因为这超出了管理范围,但没有运气。shortcode函数不会运行,原始的shortcode会回显到页面。
有什么想法吗?对于我想要做的事情,我的代码是否过于复杂?
4/16/12 Update: My own answer is below.
这在函数中起作用。php:
If (in_array( \'contact-form-7/wp-contact-form-7.php\', apply_filters( \'active_plugins\', get_option( \'active_plugins\' ) ) ) ) {
function useragent_shortcode($tag) {
if ( ! is_array( $tag ) )
return \'\';
$options = (array) $tag[\'options\'];
foreach ( $options as $option ) {
if ( preg_match( \'%^name:([-0-9a-zA-Z_]+)$%\', $option, $matches ) ) {
$name_att = $matches[1];
}
$userAGENT .= $_SERVER[\'HTTP_USER_AGENT\'];
}
$user_agent = $userAGENT;
$html = \'<input type="hidden" name="\' . $name_att . \'" value="\'.$user_agent.\'" />\';
return $html;
}
wpcf7_add_shortcode(\'useragent\', \'useragent_shortcode\', true);
}
最合适的回答,由SO网友:markratledge 整理而成
This works:
If (in_array( \'contact-form-7/wp-contact-form-7.php\', apply_filters( \'active_plugins\', get_option( \'active_plugins\' ) ) ) ) {
function useragent_shortcode($tag) {
if ( ! is_array( $tag ) )
return \'\';
$options = (array) $tag[\'options\'];
foreach ( $options as $option ) {
if ( preg_match( \'%^name:([-0-9a-zA-Z_]+)$%\', $option, $matches ) ) {
$name_att = $matches[1];
}
$userAGENT .= $_SERVER[\'HTTP_USER_AGENT\'];
}
$user_agent = $userAGENT;
$html = \'<input type="hidden" name="\' . $name_att . \'" value="\'.$user_agent.\'" />\';
return $html;
}
wpcf7_add_shortcode(\'useragent\', \'useragent_shortcode\', true);
}
SO网友:Rutwick Gangurde
这个怎么样?
function useragent_shortcode($tag) {
include_once( ABSPATH . \'wp-admin/includes/plugin.php\' );
if ( is_plugin_active(\'contact-form-7/wp-contact-form-7.php\') ) {
if ( ! is_array( $tag ) )
return \'\';
$options = (array) $tag[\'options\'];
foreach ( $options as $option ) {
if ( preg_match( \'%^name:([-0-9a-zA-Z_]+)$%\', $option, $matches ) ){
$name_att = $matches[1];
}
$userAGENT .= $_SERVER[\'HTTP_USER_AGENT\'];
}
$user_agent = $userAGENT;
$html = \'<input type="hidden" name="\' . $name_att . \'" value="\'.$user_agent.\'" />\';
return $html;
} else {
//CF7 not found, hence do this
}
}
wpcf7_add_shortcode(\'useragent\', \'useragent_shortcode\', true);
我对您的代码进行了一些修改,以将条件移到shortcode函数本身中。