1

I'm trying to unseting actions for Parent terms only but unsetting the action "view" doesn't work. Terms is always clickable.

Here is what i'm trying to do :

Edit 'Code got malfunctions don't use it'

$user = wp_get_current_user();
if ( in_array( 'editor', (array) $user->roles ) ) {

    add_filter( 'tag_row_actions', 'remove_row_actions_term_parent', 10, 2 );
    function remove_row_actions_term_parent( $actions, $term ) {
        if ($term->parent == 0) {
            unset( $actions['delete'] );
            unset( $actions['trash'] );
            unset( $actions['edit'] );
            unset( $actions['view'] );
            unset( $actions['inline hide-if-no-js'] );
        }
        return $actions;
    }

    add_action( 'pre_delete_term', 'restrict_taxonomy_parent_deletion', 10, 2 );
    function restrict_taxonomy_parent_deletion( $term, $taxonomy ) {
        if ($term->parent == 0) {
            wp_die( 'Cette catégorie est protégée.' );
        }
    }

    add_action('edit_terms', 'restrict_taxonomy_parent_edit', 10, 2);
    function restrict_taxonomy_parent_edit( $term, $taxonomy ) {
        if ($term->parent == 0) {
            wp_die('Cette catégorie ne peut être modifiée');
        }
    }

    add_action( 'admin_head', function () {
        $current_screen = get_current_screen();

        // Hides the "Delete" link on all term edit page.
        if ( 'term' === $current_screen->base &&
        'category' === $current_screen->taxonomy ) :
        ?>
            <style>#delete-link { display: none !important; }</style>
        <?php
        endif;
    } );

    // Jquery Unset "-1" on <select></select> for add taxonomies. This will unset the possibility to add parent tax.

    add_action( 'admin_enqueue_scripts', 'load_custom_script' );
    function load_custom_script() {
        wp_enqueue_script('custom_js_script', get_stylesheet_directory_uri().'/js/admin-script.js', array('jquery'));
    }
}
// Jquery Unset "Aucun" on <select></select> for add taxonomies. This will unset the possibility to add parent tax.
jQuery(document).ready(function(){
    // Unset on cat page
    jQuery("#parent option[value='-1']").remove();
    // Unset on post-new page
    jQuery("#newcategory_parent option[value='-1']").remove();

});

I'm not sure if i'm doing the wright way, maybe is there another solution easier ?

4
  • 1
    I copy and pasted this code and it worked as expected. The View link was removed. Maybe the problem lies elsewhere? Higher priorities maybe?
    – Howdy_McGee
    Commented Jan 28, 2020 at 20:33
  • @Howdy_McGee I think I expressed myself badly, the view link was removed but the term is always clickable. changing priority don't change anything.
    – FloJDM
    Commented Jan 29, 2020 at 12:58
  • so you're trying to prevent the parent term from being editable? I'm not sure you can do that. Would a redirect work?
    – Howdy_McGee
    Commented Jan 29, 2020 at 14:30
  • @Howdy_McGee Yes, editable and deletable. Actually maybe i can try that with a redirect.
    – FloJDM
    Commented Jan 29, 2020 at 15:15

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Browse other questions tagged or ask your own question.