(我是 php 新手)最近,我发现了一个 php 片段添加到我的 wordpress 子主题中,这似乎工作得很好,限制了可以在我的网站前端上传的图像大小。快乐的日子,直到我开始阅读更多关于 php 的内容并意识到我的代码段有一个结束标记
原来的子主题functions.php文件只有一个开始标签,那么这是否意味着我不能添加更多的php片段(在最后一个带有结束标签的片段下面),而不添加一个新的开始标签?
这一切对我来说似乎有点令人困惑,尤其是空白的东西?我的之后没有空格
<?php
// Enqueue child theme style.css
add_action( 'wp_enqueue_scripts', function() {
wp_enqueue_style( 'child-style', get_stylesheet_uri() );
if ( is_rtl() ) {
wp_enqueue_style( 'mylisting-rtl', get_template_directory_uri() . '/rtl.css', [], wp_get_theme()-
>get('Version') );
}
}, 500 );
/**
* WP - LIMIT FILE UPLOAD SIZE (PHP SNIPPET)
**/
function max_image_size( $file ) {
$size = $file['size'];
$size = $size / 2560;
$type = $file['type'];
$is_image = strpos( $type, 'image' ) !== false;
$is_pdf = strpos( $type, 'pdf' ) !== false;
$limit = 2560;
$limit_output = '2.5MB';
if ( $is_image && $size > $limit ) {
$file['error'] = 'Image files must be smaller than ' . $limit_output;
}//end if
if ( $is_pdf && $size > $limit ) {
$file['error'] = 'PDF files must be smaller than ' . $limit_output;
}//end if
return $file;
}//end max_image_size()
add_filter( 'wp_handle_upload_prefilter', 'max_image_size' );
/**
* WP - LIMIT FILE UPLOAD SIZE - CHANGE FRONT END TEXT
**/
add_action('wp_head', 'file_size_limit_string');
function file_size_limit_string(){
?>
<script type="text/javascript">
(function($){
$(document).ready(function($){
//CHANGE THE FILE UPLOAD SIZE LIMIT MESSAGE ON FRONT-END IMAGE UPLOADERS
$('#submit-job-form .file-upload-field small').text('Maximum file size: 2.5MB.');
});
})(jQuery);
</script>
<?php
}
感谢您对此提供任何建议/帮助。我不想不小心把我的网站变成僵尸......
拉风的咖菲猫