kovshenin / post-options-api

Post Options API is an alternative to Custom Fields
http://theme.fm/2011/08/post-options-api-an-alternative-to-custom-fields-1265/
57 stars 4 forks source link

assign value to field based on other field value #8

Closed ghost closed 13 years ago

ghost commented 13 years ago

as requested, I'm posting this question here. This is the premise : I'm using a CPT with the post options API . Let's say I have a radio group, and depending on which radio option is selected, when the post is saved, a textarea metabox on the same post but in a different section will contain a different text depending on the selected radio button. another example: 2 sections : "calculation" and "result". Calculation section has 2 text fields where you can enter numbers. when you click save/update on the post, a text box in the result section gets the sum of these 2 fields How can it be done?

thanks

tollmanz commented 13 years ago

If I'm understanding you correctly, I believe that this could be done right now. Since you can define a "sanitize_callback" function when setting up the fields, you can use that function to process the data in any way you like. For instance, imagine that your radio group name is "my_radio_group" and your textarea is "my_textarea". For your textarea, you have define a "sanitize_callback" of "my_textarea_callback", you could do something like the following:

function my_textarea_callback($value)
{
    if(isset($_POST['my_radio_group']) && $_POST['my_radio_group'] == 1)
    {
        // Do something here
    }
    elseif(isset($_POST['my_radio_group']) && $_POST['my_radio_group'] == 2)
    {
        // Do something else here
    }
    else
    {
        // Setup a fallback if funny stuff is happening
    }
}

This should definitely work for you. You still have full access within your sanitize callback functions to the $_POST array.

ghost commented 13 years ago

hmm, my textarea function:

    $post_options->register_post_option( array(
        'id' => 'wpc-output-textarea',
        'title' => 'code output',
        'section' => 'output-section',  
        'callback' => $post_fields->textarea( array(
            'description' => 'output',
            'sanitize_callback' => 'wpc_custom_output_callback',
            'sanitize_callback_args' => array( 1, 2, 3 ) // These will be passed on as the second argument to the sanitize callback func.
        ) )
    ) );

but $_POST seems to be empty, as well as the $value and $args. I can't really wrap my head around the concept of the callback function.

thanks for your help

tollmanz commented 13 years ago

The "sanitize_callback" function is a function that is executed prior to the value being saved to the database. It is VERY important that you do not trust user input when building your meta boxes. For instance, if you build a meta box that should only contain positive integers and the user puts "hello" in the box, it will likely cause problems somewhere in the system. As such, you need to validate and sanitize the value before sending it to the database. The "sanitize_callback" function parameter allows you to specify the name of the function that will be run prior to the data being saved. In the code at the bottom of this comment, you will see that I specified "wpc_custom_output_callback" as the "sanitize_callback" function. What this means is that before the contents of the "wpc-output-textarea" textarea is saved, the function "wpc_custom_output_callback" will be executed. This function will receive $value (the value of the field) and $args, which are specified in "sanitize_callback_args". Because you've specified that "sanitize_callback" is "wpc_custom_output_callback", you need to provide a function to handle this. Take a look at this sample. Hopefully it'll help:

$post_options->register_post_option( 
    array(
        'id' => 'wpc-output-textarea',
        'title' => 'code output',
        'section' => 'output-section', 
        'callback' => $post_fields->textarea( 
            array(
                'description' => 'output',
                'sanitize_callback' => 'wpc_custom_output_callback',
                'sanitize_callback_args' => array( 1, 2, 3 )
            ) 
        )
    ) 
);

function wpc_custom_output_callback($value, $args)
{
    if(isset($_POST['my_radio_group']) && $_POST['my_radio_group'] == 1)
    {
        // Do something here that manipulates $value
        // In other words, you know that the radio value is "1", change the $value
        // of the textarea based on that here
    }
    elseif(isset($_POST['my_radio_group']) && $_POST['my_radio_group'] == 2)
    {
        // Do something here that manipulates $value
        // In other words, you know that the radio value is "2", change the $value
        // of the textarea based on that here    
    }
    else
    {
        // Setup a fallback if funny stuff is happening
    }
}
ghost commented 13 years ago

thanks for the explanation Zack the callback function always shows the value of the else condition, so it seems the $_POST is not set. and if I do a print_r($_POST) in the callback function it doesn't display anything

tollmanz commented 13 years ago

Try:

function wpc_custom_output_callback($value, $args)
{
    var_dump($_POST); die();
    if(isset($_POST['my_radio_group']) && $_POST['my_radio_group'] == 1)
    {
        // Do something here that manipulates $value
        // In other words, you know that the radio value is "1", change the $value
        // of the textarea based on that here
    }
    elseif(isset($_POST['my_radio_group']) && $_POST['my_radio_group'] == 2)
    {
        // Do something here that manipulates $value
        // In other words, you know that the radio value is "2", change the $value
        // of the textarea based on that here    
    }
    else
    {
        // Setup a fallback if funny stuff is happening
    }
}
ghost commented 13 years ago

yay! that allowed to show the $_POST array and figure out the problem:

function wpc_custom_output_callback($value, $args)
{
    var_dump($_POST); //die();
    if(isset($_POST['post-options']['wpc-enqueue-jquery']) && $_POST['post-options']['wpc-enqueue-jquery'] == 'option-1')
    {
        // Do something here that manipulates $value
        // In other words, you know that the radio value is "1", change the $value
        // of the textarea based on that here
        return "salt and vinegar";
    }
    elseif(isset($_POST['post-options']['wpc-enqueue-jquery']) && $_POST['post-options']['wpc-enqueue-jquery'] == 'option-2')
    {
        // Do something here that manipulates $value
        // In other words, you know that the radio value is "2", change the $value
        // of the textarea based on that here    
        return "cheese and onion";      
    }
    else
    {
        // Setup a fallback if funny stuff is happening
            return "other";     
    }
}