Support

Account

Home Forums Backend Issues (wp-admin) Set Textarea to Uneditable

Solving

Set Textarea to Uneditable

  • I have a textarea that I populate with data from another source, and am looking to make the field uneditable when displayed in the backend (and in the front end, if it comes to that).

    I am currently using an acf/load_field filter to attempt to accomplish this, as all forum threads I’ve read suggest this is the best way to do so. However, the following code accomplishes nothing, and all other suggestions that I’ve found online have similarly been ineffective. Is this a functionality that still doesn’t exist in ACF? It seems like a pretty standard feature.

    add_filter( 'acf/load_field/name=funbotic_parents', 'funbotic_load_parents' );
    
    function funbotic_load_parents( $field ) {
    	$field['readonly'] = 1;
    
    	return $field;
    }
  • try setting the priority >10

    
    add_filter( 'acf/load_field/name=funbotic_parents', 'funbotic_load_parents', 20 );
    
  • Gave this a try, still no luck. The textarea remains editable, and any changes I make to it are saved when I save the user profile. (For clarification, I am currently viewing this field when editing user profiles.) I appreciate the quick response though!

  • I just did a test and it worked for me.

    Where are you adding your code?

  • Interesting. I have a custom plugin I’m creating, and am adding this code in one of the files in the /includes folder for that plugin.

    The file name is funbotic-parent-child-relationships.php. And in the funbotic.php file, I’ve made sure to include it:
    require_once dirname( __FILE__ ) . '/includes/funbotic-parent-child-relationships.php';

    Does this need to be in functions.php, or something similar? If so, why would it not function as part of the code of a plugin? (I’m still new to WordPress and web development in general, so I’m trying to learn as much as I can!)

  • It depends on if that file is actually loaded or not. Is the loading of this file conditional on something? When is it loaded?

  • I’m gonna dump the funbotic.php file here – as far as I can tell it’s loaded at whatever priority a standard plugin would be. All the other code I’ve written in the plugin shows up, and I’ve not personally defined the plugin to be loaded only conditionally.

    <?php
    
    /**
     * The plugin bootstrap file
     *
     * This file is read by WordPress to generate the plugin information in the plugin
     * admin area. This file also includes all of the dependencies used by the plugin,
     * registers the activation and deactivation functions, and defines a function
     * that starts the plugin.
     *
     * @link              https://www.funbotic.com
     * @since             1.0.0
     * @package           Funbotic
     *
     * @wordpress-plugin
     * Plugin Name:       Funbotic
     * Plugin URI:        https://www.funbotic.com
     * Description:       Logged in/logged out menu.  Conditional shortcodes.  Custom fields.
     * Version:           1.0.0
     * Author:            Alexander LaBrie
     * Author URI:        https://www.funbotic.com
     * License:           GPL-2.0+
     * License URI:       http://www.gnu.org/licenses/gpl-2.0.txt
     * Text Domain:       funbotic
     * Domain Path:       /languages
     */
    
    // If this file is called directly, abort.
    if ( ! defined( 'WPINC' ) ) {
    	die;
    }
    
    /**
     * Currently plugin version.
     * Start at version 1.0.0 and use SemVer - https://semver.org
     * Rename this for your plugin and update it as you release new versions.
     */
    define( 'PLUGIN_NAME_VERSION', '1.0.0' );
    
    /**
     * The code that runs during plugin activation.
     * This action is documented in includes/class-funbotic-activator.php
     */
    function activate_funbotic() {
    	require_once plugin_dir_path( __FILE__ ) . 'includes/class-funbotic-activator.php';
    	Funbotic_Activator::activate();
    }
    
    /**
     * The code that runs during plugin deactivation.
     * This action is documented in includes/class-funbotic-deactivator.php
     */
    function deactivate_funbotic() {
    	require_once plugin_dir_path( __FILE__ ) . 'includes/class-funbotic-deactivator.php';
    	Funbotic_Deactivator::deactivate();
    }
    
    register_activation_hook( __FILE__, 'activate_funbotic' );
    register_deactivation_hook( __FILE__, 'deactivate_funbotic' );
    
    /**
     * The core plugin class that is used to define internationalization,
     * admin-specific hooks, and public-facing site hooks.
     */
    require plugin_dir_path( __FILE__ ) . 'includes/class-funbotic.php';
    
    /**
     * Registers logic for displaying menus conditionally.
     */
    require_once dirname( __FILE__ ) . '/includes/funbotic-conditional-menus.php';
    
    /**
     * Registers shortcodes and logic to display content conditionally.
     */
    require_once dirname( __FILE__ ) . '/includes/funbotic-conditional-shortcodes.php';
    
    /**
     * Provides custom fields to be used when uploading media, focused on making a per-user gallery possible.
     */
    require_once dirname( __FILE__ ) . '/includes/funbotic-media-fields.php';
    
    /**
     * Generates shortcode for gallery populated dynamically with images tagged with the current Subscriber.
     */
    require_once dirname( __FILE__ ) . '/includes/funbotic-dynamic-user-gallery.php';
    
    /**
     * Creates relationships between user accounts to denote parent/child relationships, giving the ability for parents to monitor their children's progress.
     */
    require_once dirname( __FILE__ ) . '/includes/funbotic-parent-child-relationships.php';
    
    /**
     * Begins execution of the plugin.
     *
     * Since everything within the plugin is registered via hooks,
     * then kicking off the plugin from this point in the file does
     * not affect the page life cycle.
     *
     * @since    1.0.0
     */
    function run_funbotic() {
    
    	$plugin = new Funbotic();
    	$plugin->run();
    
    }
    
    run_funbotic();
  • This is using the plugin bootstrap, something I’ve never used. I find that it just over-complicates things (over abstraction, my opinion only) for simple plugins, especially for someone that’s new to WP or dev in general.

    But yes, ACF hooks will work in a plugin if they are added when they need to be added.

    What you need to know is, when during the course of the loading of WP is the line of code that adds your filter run. When I talk about “When” this may not have anything to do with when a file is loaded (unless that line does not depend on anything else, see below.) WordPress’s action/filter hook system means that you need to know when during the operation in terms of the hooks order does something actually happen.

    The only way that that your add_filter() will be run automatically when the file is loaded is if it’s just on a line in the file outside of any functions or classes. If it is in a function or in a method of a class then it more than likely depends on when the hook fires to run the function or when the class is instantiated or when the hook that runs the method in the class is fired.

    Sorry for the long winded answer that didn’t really answer anything.

  • Haha on the contrary, you’ve given me a lot to look into! The line for adding the filter sits by itself in the funbotic-parent-child-relationships.php file, but it sounds like what you’re saying is that because the plugin bootstrap is being used, this changes how that file will be loaded, and thus when that filter/action will fire.

    It sounds like I need to have a better understanding of how all those lower level pieces fit together before I’ll be able to effectively troubleshoot this issue. I very much appreciate the time and effort you’ve taken to answer this question!

  • Actually, no, because it is sitting on a line all by itself then it doesn’t make sense that it’s not working. So I’m a little confused.

    Try this

    
    add_filter( 'acf/load_field/name=funbotic_parents', 'funbotic_load_parents' );
    
    function funbotic_load_parents( $field ) {
    	$field['readonly'] = 1;
            echo '<pre>'; print_r($field); echo '</pre>';
    	return $field;
    }
    

    when you load the edit page where the field would appear that should output all of the field settings for the field above the field.

  • I could be over thinking the issue, but it’s hard to say without seeing all of the code (and I really don’t want to see all of it) and knowing more about this field it’s not working on. It can be hard to troubleshoot issues this way.

    The questions are:

    1) Is the line of code being run to add the filter.

    
    echo 'here'; die;
    add_filter( 'acf/load_field/name=funbotic_parents', 'funbotic_load_parents' );
    

    if here is echoed and execution stops then the line is run, if not then you need to figure out why

    2) is the filter being called when your field is loaded

    This is the code from above, if the field settings are shown then it’s running

    
    add_filter( 'acf/load_field/name=funbotic_parents', 'funbotic_load_parents' );
    
    function funbotic_load_parents( $field ) {
    	$field['readonly'] = 1;
            echo '<pre>'; print_r($field); echo '</pre>';
    	return $field;
    }
    

    Based on the results of these 2 tests we can move on.

  • Below are the settings that are listed. I was using var_dump to examine these previously, and didn’t see anything particularly odd, but I’m definitely not the expert on this!

    Array
    (
        [key] => field_5ada292c76da5
        [label] => Funbotic Parents
        [name] => funbotic_parents
        [_name] => funbotic_parents
        [type] => textarea
        [order_no] => 0
        [instructions] => These are the parents currently associated with this user.  To alter the user's parents, go to one of these parents' profiles, or any other Customer (to add a new parent.)
        [required] => 0
        [id] => acf-field-funbotic_parents
        [class] => textarea
        [conditional_logic] => Array
            (
                [status] => 0
                [rules] => Array
                    (
                        [0] => Array
                            (
                                [field] => null
                                [operator] => ==
                            )
    
                    )
    
                [allorany] => all
            )
    
        [default_value] => 
        [placeholder] => 
        [maxlength] => 
        [rows] => 
        [formatting] => br
        [field_group] => 4617
        [readonly] => 1
    )

    For the first test you mentioned, “here” is indeed echoed.

  • So the filter is running, but your setting of readonly is not sticking. The only explanation for this is that something else is changing the setting after you set it.

    This means that some other filter that can change the field is running after yours. This can be either an “acf/load_field” or “acf/prepare_field” filter. My next step would be to do a search of the entire site for both of these and see if I find anything that might be effecting the field.

    Setting a priority of 20 will eliminate ACF being the cause of interference.

  • Thank you so much, John. I’ll dig into this now and see if I’m able to turn up whatever the possible source of another filter might be.

  • Sorry for the long path to get here.

    The filter should be working, I use these filters in plugins all the time. The issue here is that something is causing your setting to be ignored. The only thing that I can think of is that some other filter is altering the field again after your filter.

  • This is quite strange. I did a search of the entire Local By Flywheel directory where this dev site is currently being worked on, and the only “acf/load_field” or “acf/prepare_field” that weren’t referenced in the actual ACF files were entirely unrelated, and referenced different fields. (And there were very few of them in the first place.) I suppose the investigation will have to continue.

  • Can you give me more information about this field? Is it a top level field? Is the field in a repeater, clone, group, flex field or nested in some other way?

  • It exists as the sole field within a field group entitled “Do Not Alter: Parent Relationship”. I didn’t even realize you could create fields outside of groups (which it sounds like is something you’re saying is possible). It does not exist in a repeater, clone, or flex field, and is not nested.

    Field Label: Funbotic Parents
    Field Name: funbotic_parents
    Field Type: Text Area
    Required?: No
    Conditional Logic: No

    I appreciate your continued support on this!

  • That’s what I meant. You can’t create fields that are not in a field group. If the field was nested in another field, like a repeater for example, that might have been a cause of the issue.

    If you don’t mind, can you put your entire plugin in a .zip file and attach it, or possibly create a github repo for it so I can take a closer look.

  • This reply has been marked as private.
  • You have something else going on somewhere. I just tried your files on a test site. The test site is a WP install, latest version. It is using the 2017 theme. It has only ACF Pro and your plugin active. I created a textarea field named ‘funbotic_parents’. When I go to a page where this field appears it is set to readonly.

    There is something in either the theme or another plugin that is interfering with your plugin on your site.

  • I added the field in ACF because I could not find anything in your code where this field was being added anywhere.

  • Yes, I had added that particular field manually on the site. Sorry, I should have specified that previously. But it sounds like that’s not best practice if fields can be generated within the plugin code itself.

    Thank you very much, I’ll start a process of elimination to narrow down which other plugin/theme is causing the issue.

  • Generally, when building a plugin I keep the fields in the admin of ACF and turn on local JSON for the theme I using during dev https://www.advancedcustomfields.com/resources/local-json/.

    Once the plugin is complete I make a decision on whether to put the files into a local json folder inside the plugin and add a load point or to move the field groups into php https://www.advancedcustomfields.com/resources/register-fields-via-php/. This depends on whether or not I want to make the fields’ labels and values available for translation.

  • That’s incredibly helpful to know. I’ll adopt those practices for this plugin, in that case. ACF has some seriously great features!

Viewing 25 posts - 1 through 25 (of 26 total)

The topic ‘Set Textarea to Uneditable’ is closed to new replies.