Viewing 5 replies - 1 through 5 (of 5 total)
  • Plugin Author Shabti Kaplan

    (@shabti)

    Good question.

    This hook will work on the frontend as well

    Thread Starter amirosein

    (@amirosein)

    Dear Shabti,
    Thank you for your prompt response and the amazing plugin.
    I’m here to confirm that the action ‘prepare_field’ has worked properly on frontend too.

    Here is a code example for anybody who looking for:

    //Prepare random string for unique ID field
    function generate_random_string($field) {
    $characters = 'abcdefghijklmnopqrstuvwxyz0123456789';
    $random_string = '';
    for ($i = 0; $i < 3; $i++) {
    $random_string .= $characters[mt_rand(0, strlen($characters) - 1)];
    }
    $field['value'] = $random_string;
    return $field;
    }


    add_filter('acf/prepare_field/key=field_66d69048c559e', 'generate_random_string'); //backend
    add_filter('acf/prepare_field/key=field_66db0c2164c50', 'generate_random_string'); //frontend

    Thread Starter amirosein

    (@amirosein)

    Dear Shabti,
    I’m struggling with another isuue that is related to Prepare Field filter, In below example:

    function prepare_sample_text_field($field){
    	$post_id = isset($_GET['post_id']) ? intval($_GET['post_id']) : 0;
    	$field['value'] = 'Current ID: ' . $post_id;
    	return $field;
    }
    add_filter('acf/prepare_field/key=field_66b15ffe777f6', 'prepare_sample_text_field');

    Everything is working well after loading the page and it fills the value of the field with URL query, but after hit the submit button (with no action after form submission – ajax submit), it can’t handle query again and just returns 0.

    This is a simplified example of what actually I want to achieve, that is enable/disable some fields based on current URL query value, but they appears again after saving the form.

    It would be great if you give me a hint to solve this issue.
    Thanks in advance

    • This reply was modified 5 months, 1 week ago by amirosein. Reason: style
    Thread Starter amirosein

    (@amirosein)

    @shabti Kindly I need your help here.
    I’ve already asked gpt, copilot, gemini, and searched all across the web and found no solution.

    Plugin Author Shabti Kaplan

    (@shabti)

    @amirosein Your code doesn’t work because the $_GET array isn’t set in the ajax function. Instead use $_POST.

    You can do this to see what’s in the $_POST array:
    error_log( print_r( $_POST, true ) );

    To get the post id is actually a bit complicated. You need to do this:

    if( ! empty(  $_POST['_acf_objects'] ) ){
    $objects = fea_decrypt( $_POST['_acf_objects'] );
    $objects = json_decode( $objects, true );
    $post_id = $objects['post'];
    }

    It is better to use this hook:
    https://www.dynamiapps.com/frontend-admin-save_post/

Viewing 5 replies - 1 through 5 (of 5 total)
  • You must be logged in to reply to this topic.