2

I'm studying the theme "one page" and there is the following code:

function onepage_sections() {
$sections = array();

$sections['service_section'] = array(
    'id' => 'service_section',
    'label' => __('Service Section', 'one-page'),
    'callback' => 'onepage_service_section',
);

$sections['blog_section'] = array(
    'id' => 'blog_section',
    'label' => __('Blog Section', 'one-page'),
    'callback' => 'onepage_blog_section',
);

return apply_filters('onepage_sections', $sections);
}

From what I have read the function apply_filters creates a tag (a key name that can be accessed later) and a content that will be susceptible to change whenever someone uses add_filter(key_name, function_to_alter_content_in_key_name). Correct?

What I don't get is that in this theme, there are no calls to add_filter('onepage_sections'). It is simply declared in the preceding call to apply_filters. Could someone clarify the concept of these functions?

  • Filters are added so that the plugins or child themes can easily modify/filter, so if the theme doesn't any have add_filter() doesn't mean it isn't useful – bravokeyl Apr 29 '16 at 16:11
  • But the thing is, if i remove or alter this function the front end content will be affected, but i don't see how since there is no add_filter – Selhar Apr 29 '16 at 16:12
  • Isn't this function being used any where in the theme? – bravokeyl Apr 29 '16 at 16:14
  • It's used by another function, that also adds another apply_filter. – Selhar Apr 29 '16 at 16:14
  • apply_filters gives you the option of altering $sections before it is returned from the function. if there are no filters added to onepage_sections, then apply_filters just passes the unaltered value through unchanged. – Milo Apr 29 '16 at 16:15
0
add_filter ( string $tag, callable $function_to_add, int $priority = 10, int $accepted_args = 1 )

Hook a function or method to a specific filter action.

WordPress offers filter hooks to allow plugins to modify various types of internal data at runtime.

A plugin can modify data by binding a callback to a filter hook. When the filter is later applied, each bound callback is run in order of priority, and given the opportunity to modify a value by returning a new value.

The following example shows how a callback function is bound to a filter hook.

Note that $example is passed to the callback, (maybe) modified, then returned:

function example_callback( $example ) {
    // Maybe modify $example in some way.
    return $example;
}
add_filter( 'example_filter', 'example_callback' );

Bound callbacks can accept from none to the total number of arguments passed as parameters in the corresponding apply_filters() call.

In other words, if an apply_filters() call passes four total arguments, callbacks bound to it can accept none (the same as 1) of the arguments or up to four. The important part is that the $accepted_args value must reflect the number of arguments the bound callback actually opted to accept. If no arguments were accepted by the callback that is considered to be the same as accepting 1 argument. For example:

// Filter call.
$value = apply_filters( 'hook', $value, $arg2, $arg3 );

// Accepting zero/one arguments.
function example_callback() {
    ...
    return 'some value';
}
add_filter( 'hook', 'example_callback' ); // Where $priority is default 10, $accepted_args is default 1.

// Accepting two arguments (three possible).
function example_callback( $value, $arg2 ) {
    ...
    return $maybe_modified_value;
}
add_filter( 'hook', 'example_callback', 10, 2 ); // Where $priority is 10, $accepted_args is 2.

Note: The function will return true whether or not the callback is valid. It is up to you to take care. This is done for optimization purposes, so everything is as quick as possible.

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.