2

Myself wondering among the following methods which is better to update an option value where value to be updated is coming from an ajax callback. Since Wordpress Codex clearly says apply_filters is used to return the formatted values where as do_actions for logic function executions.

add_action('wp_ajax_test_action', 'testAction');

///****** Method 1 ********///
function testAction() {
    $test = apply_filters('test_filter', 'test');

    wp_send_json($test);
}

add_filter('test_filter', 'test_filter_function', 10, 1);

function test_filter_function($value) {
   $success = get_option('update_from_test_doaction') !== false ?
                    update_option('update_from_test_doaction', $value, 'yes') : add_option('update_from_test_doaction', $value, '', 'yes');
if($success)
return 'Updated';

return 'Not updated';
}


///****** Method 2 ********///
function testAction() {
    $test = apply_filters('test_filter', 'test');

      $test2 = get_option('update_from_test_doaction', '');

    do_action('test_doaction', $test);

$sendJson = 'Not updated';
if($test != $test2)
$sendJson = 'Updated';

    wp_send_json($sendJson);
}

add_filter('test_filter', 'test_filter_function', 10, 1);

function test_filter_function($value) {
   // $value .= '123';
    return $value;
}

add_action('test_doaction', 'test_doaction_functin', 10, 1);

function test_doaction_functin($value) {
    get_option('update_from_test_doaction') !== false ?
                    update_option('update_from_test_doaction', $value, 'yes') : add_option('update_from_test_doaction', $value, '', 'yes');
}
  • doesn't really matter whatever works for you, a filter is a just a kind of action really anyway... do_action seems to make slightly more sense here, but it's more a subjective thing rather than any real advantages. – majick Jun 9 '16 at 4:23
  • 2
    if you're asking which method follows the intended purpose of each function, method 1 is an "incorrect" use of filters. – Milo Jun 9 '16 at 4:46
0

Contrary to the comments... I'd say that you should use filter as in method 1 because...

  1. Message will depend on quite a few things right now, You just do a test if statement, later on message may depend on some other factors ( like maybe validating a nonce on failure giving message 'None invalid' ).
  2. Actions help in extending your code's functionality... later on some other plugin ( or maybe theme, if your plugin get's incredibly famous :D ) may wanna change the message to be more friendly like Sorry, couldn't update.

It's always easier with filters, remember whenever you have a value involved ( like an output message, or value saved into db ) it's best to use filters.

  • Its true @Shramee Srivastav :) But the codex confuses... – mpsbhat Jun 9 '16 at 8:51
  • 1
    filters are for filtering supplied values. in the example above, the input and what is returned are unrelated. this is not what filters are for. – Milo Jun 11 '16 at 16:55

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.