我使用WordPress的插件WP-Members,我需要修改登录表单。将数组输入函数后,出现以下错误:
"致命错误:在null"
上调用成员函数get_text()
这是我在functions.php中的代码
add_filter( 'wpmem_login_form', 'my_login_form_filter' );
function my_login_form_filter( $form, $action = 'login' )
{
/**
* The login form is brought in with $form
* You can append to it or filter it
*
* $action specifies the form being constructed. It
* can be: login|pwdreset|pwdchange
*/
$form = array(
array(
'name' => $wpmem->get_text( "login_username" ),
'type' => 'text',
'tag' => 'log',
'class' => 'username',
'div' => 'div_text',
),
array(
'name' => $wpmem->get_text( "login_password" ),
'type' => 'password',
'tag' => 'pwd',
'class' => 'password',
'div' => 'div_text',
),
array(
'name' => $wpmem->get_text( "login_number" ),
'type' => 'text',
'tag' => 'log',
'class' => 'username',
'div' => 'div_text',)
);
return $form;
}
这是插件代码的一部分:
if ( ! function_exists( 'wpmem_inc_login' ) ):
/**
* Login Dialog.
*
* Loads the login form for user login.
*
* @since 1.8
*
* @global object $wpmem The WP_Members object.
* @global string $wpmem_regchk The WP-Members message container.
* @global object $post The WordPress Post object.
*
* @param string $page
* @param string $redirect_to
* @return string $str The generated html for the login form.
*/
function wpmem_inc_login( $page = "page", $redirect_to = null, $show = 'show' ) {
global $wpmem, $wpmem_regchk, $post;
$str = '';
if ( $page == "page" ) {
if ( $wpmem_regchk != "success" ) {
$dialogs = get_option( 'wpmembers_dialogs' );
// This shown above blocked content.
$msg = $wpmem->get_text( 'restricted_msg' );
$msg = ( $dialogs['restricted_msg'] == $msg ) ? $msg : __( stripslashes( $dialogs['restricted_msg'] ), 'wp-members' );
$str = "<p>$msg</p>";
/**
* Filter the post restricted message.
*
* @since 2.7.3
*
* @param string $str The post restricted message.
*/
$str = apply_filters( 'wpmem_restricted_msg', $str );
}
}
// Create the default inputs.
$default_inputs = array(
array(
'name' => $wpmem->get_text( 'login_username' ),
'type' => 'text',
'tag' => 'log',
'class' => 'username',
'div' => 'div_text',
),
array(
'name' => $wpmem->get_text( 'login_password' ),
'type' => 'password',
'tag' => 'pwd',
'class' => 'password',
'div' => 'div_text',
),
);
/**
* Filter the array of login form fields.
*
* @since 2.9.0
*
* @param array $default_inputs An array matching the elements used by default.
*/
$default_inputs = apply_filters( 'wpmem_inc_login_inputs', $default_inputs );
$defaults = array(
'heading' => $wpmem->get_text( 'login_heading' ),
'action' => 'login',
'button_text' => $wpmem->get_text( 'login_button' ),
'inputs' => $default_inputs,
'redirect_to' => $redirect_to,
);
/**
* Filter the arguments to override login form defaults.
*
* @since 2.9.0
*
* @param array $args An array of arguments to use. Default null.
*/
$args = apply_filters( 'wpmem_inc_login_args', '' );
$arr = wp_parse_args( $args, $defaults );
$str = ( $show == 'show' ) ? $str . wpmem_login_form( $page, $arr ) : $str;
return $str;
}
endif;
答案 0 :(得分:2)
好像你试图在对象$Services = @'
SUPER-PC,Microsoft Office ClickToRun Service,ClickToRunSvc,C:\Program Files\Microsoft Office 15\ClientX64\OfficeClickToRun.exe /service,Auto
SUPER-PC,Adobe Acrobat Update Service,AdobeARMservice,C:\Program Files (x86)\Common Files\Adobe\ARM\1.0\armsvc.exe,Auto
'@
# I prefer a more OS-agnostic newline pattern
$Services = $Services -split "`r?`n"
# Create a new array, defaults to Object[], so can contain arrays as well
$MultiDimensionalServices = @()
# Iterate over each string in $Services
foreach($ServiceString in $Services){
# use the unary array operator (,) to avoid flattening the array
$MultiDimensionalServices += ,@($ServiceString -split ',')
}
上调用函数get_text()
,但它是$wpmem
。
您可以尝试在null
函数定义之上添加global $wpmem;
,如下所示:
my_login_form_filter
答案 1 :(得分:1)
$wpmem
在您的函数中为null。将global $wpmem
添加到您的函数以使用全局值。
答案 2 :(得分:0)
问题是您正在尝试使用作为对象一部分的方法,但是该对象在使用它的上下文中不存在。
get_text()
是WP_Members对象$wpmem
($wpmem->get_text()
)的方法。
这是一个全局声明的对象,因此,只要您在已定义的地方使用它,就可以通过在函数开始时声明全局变量来访问它:
global $wpmem;
但是插件有一个API function for this method,因此您可以使用直接函数而不是对象方法-这样,已经为您声明了对象
因此,请使用$wpmem->get_text()
而不是wpmem_gettext()
。
另一个问题是您正在混淆过滤器。您的过滤器代码正在使用wpmem_login_form
。虽然这是插件中的合法过滤器挂钩,但这并不是正确的挂钩(甚至在您发布的函数中也没有定义)。您需要使用wpmem_inc_login_inputs
来更改输入(在发布的函数中查找定义位置)。否则,您尝试执行的操作将永远无法进行。
它应该是这样:
add_filter( 'wpmem_inc_login_inputs', 'my_login_form_filter' );
function my_login_form_filter() {
$form = array(
array(
'name' => wpmem_gettext( "login_username" ),
'type' => 'text',
'tag' => 'log',
'class' => 'username',
'div' => 'div_text',
),
array(
'name' => wpmem_gettext( "login_password" ),
'type' => 'password',
'tag' => 'pwd',
'class' => 'password',
'div' => 'div_text',
),
array(
'name' => "Login Number", // "login_number" does not exist in wpmem_gettext().
'type' => 'text',
'tag' => 'log',
'class' => 'username',
'div' => 'div_text',
)
);
return $default_inputs;
}