2

(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...

Chris
  • 85
  • 1
  • 4
  • 1
    Leaving out the final closing `?>` in a PHP script file is considered good practice, see https://stackoverflow.com/questions/4410704/why-would-one-omit-the-close-tag – 04FS Jan 16 '20 at 07:44
  • Thanks! I just read through that. Does the code I provided look ok to you? I'm still not sure why there is a closing tag, ?> followed by – Chris Jan 16 '20 at 08:00
  • Since ` – 04FS Jan 16 '20 at 08:08
  • Thank you so much, that really helps me. I appreciate your help! – Chris Jan 16 '20 at 09:47

1 Answers1

1

When executing your PHP script, the PHP engine ignore anything outside the opening (<?php) and closing (?>) tag. The engine do not manipulate them and just forward them to the output. In your case, this piece of code is not processed and just be forwarded to the final HTML output while the rest of code will be processed by the engine.

<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>

If there is nothing but whitespaces (spaces, new lines, ...) after a closing tag in a PHP file. It is recommended to omit the closing tag and to have one new line in the end of the file.

Hieu Le
  • 8,288
  • 1
  • 34
  • 55
  • 1
    Thank you Hieu Le, for a great reply to my question. This also helps me understand the reason why there is a closing tag. I appreciate your help. – Chris Jan 17 '20 at 08:44