Support

Search results for ""

Sorry, no results found. Perhaps you would like to search the documentation?
All Topics
matt31

Disable inline edit based on custom field

HI,

I am trying to disable inline edit if field wpcf-organic-traffic is set to 1.

I can’t figure out why my custom code is not working, any ideas as to why, is the hook still available?

function my_acp_disable_inline_editing($editable, AC\Column $column) {
    if ($column->get_type() === 'status') {
        $post_id = $column->get_id();

        // Retrieve the value of the custom field 'wpcf-organic-traffic'
        $organic_traffic_value = get_post_meta($post_id, 'wpcf-organic-traffic', true);

        // Check if 'wpcf-organic-traffic' is set to 1
        if ($organic_traffic_value === '1') {
            return false; // Disable inline editing
        }
    }

    return $editable;
}

add_filter('acp/editing/view', 'my_acp_disable_inline_editing', 10, 2);

Thanks.

4 months, 4 weeks ago
Stefan van den Dungen Gronovius
Developer

The hook you currently use, cannot be used for a specific post (record) context. So it is impossible to change the view behavior based on a specific ID. The $column->get_id() call you’re using is not giving you the Post ID. Instead, if will give you the column’s unique name that can be used to target the column, which is not what you are looking for.

In your case, you could use the following hook and return null when you don’t want editing to work.

https://github.com/codepress/admin-columns-hooks/blob/master/acp-editing-value.php

The code could look something like this:

add_filter('acp/editing/value', function ($value, $id, AC\Column $column) {
    if ($column->get_type() === 'status') {
        return get_post_meta($id, 'wpcf-organic-traffic', true) === '1'
            ? null
            : $value;
    }

    return $value;
}, 10, 3);

The only thing I’m still not sure about, is the column type check. There is no default ‘status’ column in WordPress. If you want to target our ‘Status’ column, you could use this: ‘column-status’

4 months, 3 weeks ago
matt31

Ahhhh got it thank you for your assistance with this! This is working great now.

4 months, 3 weeks ago

You must be logged in to reply to this topic.