Force a user’s posts category

Ok thanks for your help i manage to make it work :

1 : When admin go inside user option page he can select wich categories he is into
2 : Then this user is creating a news and we can see i’m hiding the category box
3 : Then if we check we can see the news has the categories set from the one choose in the user

1
2

So let me give my code maybe it will help some one once 🙂

    function restrict_user_form_enqueue_scripts($hook) {
    if ( ! in_array($hook, array('profile.php', 'user-edit.php' )))
        return;
    wp_enqueue_script('jquery');
    wp_enqueue_script( 'jquery.multiple.select', get_template_directory_uri() . '/js/jquery.multiple.select.js' );
    wp_register_style( 'jquery.multiple.select_css', get_template_directory_uri() . '/css/multiple-select.css', false, '1.0.0' );
    wp_enqueue_style( 'jquery.multiple.select_css' );
}


add_filter('pre_option_default_category', 'jam_change_default_category');

function jam_change_default_category($ID) {
    // Avoid error or heavy load !
    if ( ! is_user_logged_in() )
        return $ID;
    $user_id = get_current_user_id();
    $restrict_cat = get_user_meta( $user_id, '_access', true);
    if ( is_array($restrict_cat) ) {
        return reset($restrict_cat);
    } else {
        return $ID;
    }
}

/**
* Exclude categories which arent selected for this user.
*/
add_filter( 'get_terms_args', 'restrict_user_get_terms_args', 10, 2 );

function restrict_user_get_terms_args( $args, $taxonomies ) {
    // Dont worry if we're not in the admin screen
    if (! is_admin() || $taxonomies[0] !== 'category')
        return $args;
    // Admin users are exempt.
    $currentUser = wp_get_current_user();
    if (in_array('administrator', $currentUser->roles))
        return $args;

    $include = get_user_meta( $currentUser->ID, '_access', true);

    $args['include'] = $include;
    return $args;
    //var_dump($include);
}
// Display and save data in admin dashboard
function restrict_user_form( $user ) {
    // A little security
    if ( ! current_user_can('add_users'))
        return false;
    $args = array(
        'show_option_all'    => '',
        'orderby'            => 'ID',
        'order'              => 'ASC',
        'show_count'         => 0,
        'hide_empty'         => 0,
        'child_of'           => 0,
        'exclude'            => '',
        'echo'               => 0,
        'hierarchical'       => 1,
        'name'               => 'allow',
        'id'                 => '',
        'class'              => 'postform',
        'depth'              => 0,
        'tab_index'          => 0,
        'taxonomy'           => 'category',
        'hide_if_empty'      => false,
        'walker'             => ''
    );

    $dropdown = wp_dropdown_categories($args);
    // We are going to modify the dropdown a little bit.
    $dom = new DOMDocument();
    //$dom->loadHTML($dropdown, LIBXML_HTML_NOIMPLIED | LIBXML_HTML_NODEFDTD);
    $dom->loadHTML( mb_convert_encoding($dropdown, 'HTML-ENTITIES', 'UTF-8') );
    $xpath = new DOMXpath($dom);
    $selectPath = $xpath->query("//select[@id='allow']");

    if ($selectPath != false) {
        // Change the name to an array.
        $selectPath->item(0)->setAttribute('name', 'allow[]');
        // Allow multi select.
        $selectPath->item(0)->setAttribute('multiple', 'yes');

        $selected = get_user_meta( $user->ID, '_access', true);
        // Flag as selected the categories we've previously chosen
        // Do not throught error in user's screen ! // @JamViet
        if ( $selected )
        foreach ($selected as $term_id) {
            // fixed delete category make error !
            if (!empty($term_id) && get_the_category_by_ID($term_id) ){
                $option = $xpath->query("//select[@id='allow']//option[@value="$term_id"]");
                $option->item(0)->setAttribute('selected', 'selected');
            }
        }
    }
?>
    <h3><?php _e('Catégories de cet utilisateur', 'restrict-author-posting'); ?></h3>
    <table class="form-table">
        <tr>
            <th><label for="access"><?php _e('Choisissez les categories', 'restrict-author-posting') ?>:</label></th>
            <td>
                <?php echo $dom->saveXML($dom);?>
                <span class="description"><?php _e('', 'restrict-author-posting') ?></span>
            </td>
        </tr>

    </table>
    <table class="form-table">
        <tr>
            <th><label for="access"><?php _e('Voir seulement ces fichiers medias', 'restrict-author-posting') ?></label></th>
            <td>
                    <fieldset>
                    <legend class="screen-reader-text"><span><?php _e('Oui', 'restrict-author-posting') ?></span></legend>
                    <label for="_restrict_media">
                    <input type="checkbox" <?php checked (get_user_meta($user->ID, '_restrict_media', true), 1, 1 ) ?> value="1" id="_restrict_media" name="_restrict_media">
                <?php _e('Oui', 'restrict-author-posting') ?></label>
                    </fieldset>
            </td>
        </tr>
    </table>
    <script>
    <!--
        jQuery('select#allow').multipleSelect();
    -->
    </script>
<?php
}


// Restrict Save Data
function restrict_save_data( $user_id ) {
    // check security
    if ( ! current_user_can( 'add_users' ) )
        return false;
    // admin can not restrict himself
    if ( get_current_user_id() == $user_id )
        return false;
    // and last, save it
    if ( ! empty ($_POST['_restrict_media']) ) {
        update_user_meta( $user_id, '_restrict_media', $_POST['_restrict_media'] );
    } else {
        delete_user_meta( $user_id, '_restrict_media' );
    }
    if ( ! empty ($_POST['allow']) ) {
        update_user_meta( $user_id, '_access', $_POST['allow'] );
    } else  {
        delete_user_meta( $user_id, '_access' );
    }
}

// Remove meta box for non admin    
function remove_metaboxes() {
remove_meta_box( 'categorydiv','news','normal' );
remove_meta_box( 'categorydiv','etablissement','normal' );
}

// Save Category News
function save_category_news($post_ID, $post) {
    $currentUser = wp_get_current_user();
    $cat = get_user_meta( $currentUser->ID, '_access', true);
    $cat = array_map('intval', $cat);
    wp_set_object_terms($post_ID, $cat, 'category', true);

}

// Save Category Etablissement
function save_category_etablissement($post_ID, $post) {
    $currentUser = wp_get_current_user();
    $cat = get_user_meta( $currentUser->ID, '_access', true);
    $cat = array_map('intval', $cat);
    wp_set_object_terms($post_ID, $cat, 'category', true);

}
// Action let's go
add_action( 'admin_enqueue_scripts', 'restrict_user_form_enqueue_scripts' );
add_action( 'show_user_profile', 'restrict_user_form' );
add_action( 'edit_user_profile', 'restrict_user_form' );
add_action( 'personal_options_update', 'restrict_save_data' );
add_action( 'edit_user_profile_update', 'restrict_save_data' );
add_action( 'save_post_news', 'save_category_news', 10, 3 );
add_action( 'save_post_etablissement', 'save_category_etablissement', 10, 3 );
add_action( 'admin_menu', 'remove_metaboxes');