(I'm new to php) Recently, I discovered a php snippet to add to my wordpress child theme, that seems to work perfectly, limiting the image size that can be uploaded in the frontend of my website. Happy days, until I started reading a bit more about php and realised that my snippet has an end tag
The original child theme functions.php file only has an opening tag, so does this mean that I cannot add any more php snippets (below the last snippet with the closing tag), without adding a new opening tag?
It's all seems a bit confusing to me, especially the whitespace thing? Mine has no whitespace after the
<?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
}
Thanks for any advice/help on this one. I don't want to accidentally turn my site into a zombie...