Error on using mu-plugin

This is my first time in using mu-plugin. I’m getting an error message at the end of my code but can’t see why. Would appreciate some assistance.

Attempting to set the custom capability at the start of the website based on a custom field value.

Using the following code within file s2-hack.php within the mu-plugins directory.

<?php function setDirectory() { if ((get_user_field('InDirectory')) and (current_user_is("s2member_level1")) or (current_user_is("s2member_level2"))) { $current_user->add_cap('access_s2member_ccap_dir'); } else { $current_user->remove_cap('access_s2member_ccap_dir'); } add_action('init', 'setDirectory'); ?>

Getting the following error message:

Parse error: syntax error, unexpected end of file in /home/howard57/public_html/wp-content/mu-plugins/s2-hacks.php on line 10

You have missed a closing }. I’d also strongly recommend (as does the PHP development team) that you omit the closing ?> and that you add some security at the top of the file. Also your function name looks more like it’s following JavaScript conventions than those of PHP.

So I suggest something like this:

<?php if ( ! defined( 'ABSPATH' ) ) { die(); }

function set_directory() {
	if ( ( get_user_field( 'InDirectory' ) ) and ( current_user_is('s2member_level1' ) ) or ( current_user_is( 's2member_level2' ) ) ) {
		$current_user->add_cap( 'access_s2member_ccap_dir' );
	} else { 
		$current_user->remove_cap( 'access_s2member_ccap_dir' );
add_action( 'init', 'set_directory' );

KRS915 - Thank you for your advice. I’m just learning php so I’m curious on why the direction to omit the closing ?> at the end since all the lessons so far always have the closing syntax?

When it’s at the end of a file, it can cause “headers already sent” errors. In addition, if you put any spaces after it, that can cause a white screen of death.