0

I have a plugin that is syncing data from a third party service into a custom post type. The idea is that the post type acts as a slave to the third party service, so any changes there overwrite any on the wordpress site. However there will be a few extra custom fields that will be made to add information from the wordpress side to the data from the third party service.

What I am looking for is a way to more or less disable the fields that will be overwritten (title, body, 50-60 custom fields) Leaving only the ones that will not be overwritten to be editable.

I know how to hide title/body (although unsure if it completely removes them), however I feel it would be beneficial to keep them visible, just grayed out / disabled.

This is how I am disabling title/body:

function remove_edit_fields(){
    remove_post_type_support( 'property_listings', 'title' );
    remove_post_type_support( 'property_listings', 'editor' );
}

add_action( 'admin_init', 'remove_edit_fields' );

Is there any way to do this while keeping them visible and do the same for custom fields?

1

To begin with, adding an _ to the start of the custom field key will make it a hidden field not available for editing from the post writing screen.

ie. using _address instead of just address.

I think this is what you are looking for..?

If you then want to display these hidden field values (read-only) on the post writing screen then you can probably add a metabox that will output the display.

  • Perfect, finding an answer is difficult when you don't know the best way to term it :). I will give it a try but it seems like a good way to go about it. – Jordan Ramstad Mar 11 '16 at 17:06
  • Been working on this and it does seem to work to an extent. However at least with ACF using an underscore does not hide the field in the edit screen and I ended up having to create a custom function for it to be able to be pulled into another meta box (default acf based custom fields functions do ignore fields with an underscore, just not when creating its edit meta box). So based on what I can see, underscore makes it only available on the edit screen, so you might have it mixed up. – Jordan Ramstad Mar 11 '16 at 19:51
  • I didn't realize you were using ACF at all, my answer describes the default WordPress behaviour for the standard Custom Fields Metabox. I am really not sure how this would be done for ACF if it ignores the _ ... maybe jQuery to hide the fields? elliotcondon.com/conditional-logic-for-advanced-custom-fields – majick Mar 12 '16 at 5:52
  • I figured out a way, created a custom input type that only had a bit of css to hide the element, then a custom meta box to pull and the display the data without being editable. I will post an answer of the idea. – Jordan Ramstad Mar 14 '16 at 16:14
0

In order to do this I had to hide the fields in the default ACF output. There is no way to do this in the core, underscores can work to disable the output everywhere else but the edit area is an exception.

I had used the following code to create a hidden field type, it outputs no input and hides its own label. It is based off of https://github.com/gerbenvandijk/acf-hidden-field

<?php
class acf_field_hidden_field extends acf_field
{
    // vars
    var $settings, // will hold info such as dir / path
        $defaults; // will hold default field options
    /*
    *  __construct
    *
    *  Set name / label needed for actions / filters
    *
    *  @since   3.6
    *  @date    23/01/13
    */
    function __construct()
    {
        // vars
        $this->name = 'hidden_field';
        $this->label = __('Hidden field');
        $this->category = __("Basic",'acf'); // Basic, Content, Choice, etc
        $this->defaults = array(
            // add default here to merge into your field.
            // This makes life easy when creating the field options as you don't need to use any if( isset('') ) logic. eg:
            //'preview_size' => 'thumbnail'
        );
        // do not delete!
        parent::__construct();
        // settings
        $this->settings = array(
            'path' => apply_filters('acf/helpers/get_path', __FILE__),
            'dir' => apply_filters('acf/helpers/get_dir', __FILE__),
            'version' => '1.0.0'
        );
    }
    /*
    *  create_field()
    *
    *  Create the HTML interface for your field
    *
    *  @param   $field - an array holding all the field's data
    *
    *  @type    action
    *  @since   3.6
    *  @date    23/01/13
    */
    function render_field( $field )
    {
        // defaults?
        /*
        $field = array_merge($this->defaults, $field);
        */
        // perhaps use $field['preview_size'] to alter the markup?
        // create Field HTML
        ?>
        <style>div[data-key="<?php echo $field['key'];?>"]{ display: none;}</style>
        <?php
    }
}
// create field
new acf_field_hidden_field();
?>

I also used my own custom meta box to bring the data in, my way. I had "labeled" the fields by naming them in such a way as to know they were technically hidden fields (like hide_title and hide_desc). By using a custom one it does not affect the regular front end display.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.