WordPress.org

Make WordPress Core

#24786 closed defect (bug) (invalid)

filter set-screen-option not triggering

Reported by: robbcaldwell Owned by:
Milestone: Priority: normal
Severity: normal Version: 3.5.2
Component: Administration Keywords:
Focuses: Cc:

Description

Displaying my own table of records, I want to have a "Per Page" option in the Screen Options pull down. So I setup my option:

function add_options()
{
    $option = 'per_page';

    $args = array(
        'label' => 'Properties',
        'default' => 20,
        'option' => 'my_custom_per_page'
    );

    add_screen_option( $option, $args );
    add_filter('set-screen-option', 'my_custom_set_option', 10, 3);
}

function my_custom_set_option($status, $option, $value)
{
    return $value;
}


This works fine, the option is created and displayed in the Screen Options pull down. However, my_custom_set_option is NEVER called and when you change the value of my_custom_per_page in the Screen Options, it will revert back to its original value and never save.

Furthermore, looking at the core code, wp-admin/includes/misc.php, line 361, version 3.5.2

$value = apply_filters('set-screen-option', false, $option, $value);

$value is always returned as false.

Looking up the apply_filters function, wp-includes/plugin.php, line 137

function apply_filters($tag, $value)

The second parameter of apply_filters is $value, but passed 'false'

If I comment out this line (361) it saves fine.

Shouldn't line 361 be more like:

$value = apply_filters('set-screen-option', $value, $other_arguments);

Change History (7)

comment:1 AntoineH10 months ago

I'm having the same problem in my plugin. Would be nice if there was a response to this bug report.

comment:2 DrewAPicture10 months ago

  • Keywords close added

I think this is expected behavior -- that the filter defaults to false.

Also, is there a particular reason why you're adding the filter right after adding the screen option?

I only ask because moving the add_filter() call outside of add_options() works and saves as expected for me, with add_options() hooked to the in_admin_header action.

<?php
function add_options() {
        $option = 'per_page';

        $args = array(
                'label' => 'Properties',
                'default' => 20,
                'option' => 'my_custom_per_page'
        );

        add_screen_option( $option, $args );
}
add_action( 'in_admin_header', 'add_options' );

function my_custom_set_option( $status, $option, $value ) {
        return $value;
}
add_filter( 'set-screen-option', 'my_custom_set_option', 10, 3 );
Last edited 10 months ago by DrewAPicture (previous) (diff)

comment:3 follow-up: DrewAPicture10 months ago

  • Keywords reporter-feedback added

comment:4 in reply to: ↑ 3 ; follow-up: robbcaldwell10 months ago

Replying to DrewAPicture:

My add_options() and my_custom_set_option() functions are part of a class, therefore calling the add_filter outside, in your example, is not an option, unless I call it after I run add_options() during my class construction. I will have to give it a try.

comment:5 robbcaldwell10 months ago

  • Keywords reporter-feedback removed

comment:6 in reply to: ↑ 4 DrewAPicture10 months ago

Replying to robbcaldwell:

Replying to DrewAPicture:

My add_options() and my_custom_set_option() functions are part of a class, therefore calling the add_filter outside, in your example, is not an option, unless I call it after I run add_options() during my class construction. I will have to give it a try.

My question was more along the lines of why you want to filter it right after setting the default. From a logic standpoint, the filter should be called separately anyway. I still can't figure out why you'd want to add the filter inside of add_options().

comment:7 SergeyBiryukov10 months ago

  • Component changed from General to Administration
  • Keywords close removed
  • Milestone Awaiting Review deleted
  • Resolution set to invalid
  • Status changed from new to closed

Which action add_options() is hooked to in your example? Looks like you're adding the filter too late, it should not be in the same function as add_screen_option().

The example from comment:2 works fine. You can do the same with class methods, it's just a matter of using the correct hooks and the appropriate calling sequence.

Note: See TracTickets for help on using tickets.