• Liltiger

    (@liltiger)


    Hey There,

    I need to hide some field options in the edit post template.

    I’ve modified this code in wp-admin/edit-form-advanced.php

    if ( current_user_can( 'manage_options' ) ) {
            return;
    if ( $theme_support || $audio_post_support || $video_post_support )
    	add_meta_box('postimagediv', __('Featured Image'), 'post_thumbnail_meta_box', null, 'side', 'low');
    
    if ( post_type_supports($post_type, 'excerpt') )
    	add_meta_box('postexcerpt', __('Excerpt'), 'post_excerpt_meta_box', null, 'normal', 'core');
    
    if ( post_type_supports($post_type, 'trackbacks') )
    	add_meta_box('trackbacksdiv', __('Send Trackbacks'), 'post_trackback_meta_box', null, 'normal', 'core');
    
    if ( post_type_supports($post_type, 'custom-fields') )
    	add_meta_box('postcustom', __('Custom Fields'), 'post_custom_meta_box', null, 'normal', 'core');
    }

    It works pefect when an author is editing a post, but it doesn’t work for the admin. All I get is this:

    “Thank you for creating with WordPress.
    Version 3.8.1
    This content is currently locked. If you take over, username will be blocked from continuing to edit.
    Listings Preview Take over
    The backup of this post in your browser is different from the version below. Restore the backup.
    Post restored successfully. Undo.”

    I’m not proficient w/ PHP at all. I can handle snips here and there so any help would be greatly appreciated!

Viewing 4 replies - 1 through 4 (of 4 total)
  • Moderator bcworkz

    (@bcworkz)

    Please do not modify the edit-form-advanced.php file. It is the wrong approach, that approach will never go well for you.

    What you might try is hook the ‘add_meta_boxes’ action and manipulate the global $wp_meta_boxes array based on whatever conditions are appropriate.

    Another approach would be to hide the fields once the form loads by using jQuery to change the CSS of the fields.

    Thread Starter Liltiger

    (@liltiger)

    Thanks for replying, bcworkz.

    Where would I go to hook the ‘add_meta_boxes’? I’m actually not sure what you mean by hooking too.

    I’m using the eList theme by Elegant Themes. I don’t see a pagemetabox.php file like I have on others. Would I have to create one?

    I can barely comprehend PHP, so jQuery is way above my head.

    Moderator bcworkz

    (@bcworkz)

    Sorry about being so cryptic. We get all sorts of skill levels here, it’s often hard to judge skill level from one post.

    “Hooks” are the primary technique we use to alter the basic operation of WordPress. More on hooks. Hook code may either reside on a simple plugin or a child theme.

    Other than the required files to create a plugin or functions.php of a child theme, you do not need to create any special metabox files. It may be useful to locate the metabox definitions added by your theme. These are often in functions.php, but may be elsewhere.

    On further thought, while my initial suggestion should work fine, the proper approach would be to instead hook ‘admin_init’ and use remove_meta_box() (which also manipulates $wp_mets_boxes) to prevent display of these boxes.

    One thing to watch out for if you get into coding this is to ensure your action hook is called later than the theme’s, or there will not yet be any meta boxes to remove. The add_action() function used to hook actions has a priority parameter which you can set to be sure your action comes later.

    Your requirements are a bit too complicated and there’s much about your theme I don’t know, so I’m unable to provide actual functional code for you. If you’re going to take this on yourself, I’ll try to clear up any confusion that you may run into, but I can’t write a tutorial for you either.

    Thread Starter Liltiger

    (@liltiger)

    Hey bcworkz,

    Thanks so much for pointing me into the right direction! I was able to search remove_meta_box and found a guide here: https://premium.wpmudev.org/blog/remove-wordpress-meta-boxes/

    I used this in my theme’s functions.php:

    function remove_listing_metaboxes() {
            if ( !current_user_can( 'manage_options' ) ) {
            remove_meta_box( 'postexcerpt','listing','normal' );
            remove_meta_box( 'postcustom','listing','normal' );
            }
    }
    add_action('admin_menu','remove_listing_metaboxes');

    It works like a charm, but removing the featured image, remove_meta_box( 'postimagediv','listing','normal' );
    and
    remove_meta_box( 'postimagediv','listing','side' );
    doesn’t work. No biggies though.

    Thanks Again!

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘How To Hide Edit Fields From Non-Admins’ is closed to new replies.