我有以下短代码:
// Add Shortcode
function custom_shortcode_phptest() {
function generatePassword($length = 6) {
$characters = \'0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ\';
$charactersLength = strlen($characters);
$pass = \'\';
for ($i = 0; $i < $length; $i++) {
$pass .= $characters[rand(0, $charactersLength - 1)];
}
return $pass;
}
$password = generatePassword();
$data = time();
$test = \'/home/blablabla/public_html/ot/docs/test/\' . $password . \'.txt\';
$file = fopen($test,"w");
fwrite($file,$data);
fclose($file);
echo \'Your pass is \' . $password . \' and it is valid for 3 days.\';
}
add_shortcode( \'phptest\', \'custom_shortcode_phptest\' );
它将生成密码并将其保存在文件中。但当我在Wordpress管理面板发送的电子邮件中使用[phptest]短代码时,它就不起作用了。在php短代码中使用函数内部的函数可以吗?
最合适的回答,由SO网友:Ethan O\'Sullivan 整理而成
根据OP问题下的评论,以下是使用return
而不是echo
:
add_shortcode( \'phptest\', \'custom_shortcode_phptest\' );
function custom_shortcode_phptest() { // Add Shortcode
function generatePassword( $length = 6 ) {
$characters = \'0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ\';
$charactersLength = strlen( $characters );
$pass = \'\';
for ( $i = 0; $i < $length; $i++ ) {
$pass .= $characters[rand( 0, $charactersLength - 1 )];
}
return $pass;
}
$password = generatePassword();
$data = time();
$test = \'/home/blablabla/public_html/ot/docs/test/\' . $password . \'.txt\';
$file = fopen( $test, "w" );
fwrite( $file, $data );
fclose( $file );
return \'Your pass is \' . $password . \' and it is valid for 3 days.\';
}