0

I have a class in my plugin with the following function:

protected function get_post_types($output='names') {
    if (! is_array($post_types = get_post_types(array( 'show_ui' => true ), $output)))
        return array();

    // WE CAN DISABLE HERE THE MEDIA POST TYPE
    unset($post_types['attachment']);
    return $post_types;
}

And this other function which executes a tweak for WordPress, in this case for removing the bulk actions per post type.

function tweak()
{
    foreach( $this->get_post_types() as $type => $data) {
        if($this->setting('disable_bulk_' . $type))
        {
            add_filter('bulk_actions-edit-' . $type, '__return_empty_array');
        }
    }
}

It is working perfectly for pages, posts and attachments (if I don't unset this last one) post types, but not for other CPTs (it is working only with the built in CPTs, but I am not passing the builtin true as an argument).

Here is the full class:

class My_Tweaks_Component_Content_Bulk_Actions_Removal extends My_Tweaks_Component {
    //Set Component's Environment Type
    function env()
    {
        return 'admin';
    }

    protected function get_post_types($output='names') {
        if (! is_array($post_types = get_post_types(array( 'show_ui' => true ), $output)))
            return array();

        // WE CAN DISABLE HERE THE MEDIA POST TYPE
        unset($post_types['attachment']);
        return $post_types;
    }

    // Set Component's Fields
    public function fields()
    {
        // Empty array of fields
        $fields = array();

        // Add every field
        $fields[] = array(
            'name' => $this->name('enabled'),
            'type' => 'checkbox',
            'label' => __('Enable this option?', POMTWEAKS_SLUG),
            'description' => __('Remove the bulk actions from posts, pages and custom post types listings in admin.', POMTWEAKS_SLUG),
        );
        $fields[] = array(
            'name' => $this->name('separator'),
            'type' => 'separator',
        );

        foreach( $this->get_post_types() as $type => $data)
        {

            $postTypeObject = get_post_type_object($type);

            $fields[] = array(
                'name' => $this->name('disable_bulk_' . $type),
                'type' => 'checkbox',
                'label' => sprintf( esc_html__( 'Disable bulk actions for %s?', POMTWEAKS_SLUG ), __( $postTypeObject->labels->name ) ),
                'description' => null,
            );

        }

        return $fields;
    }

    // Check if the component has a value that determines if the tweak has to be executed
    public function is_active()
    {
        $this->settings = $this->all_fields();
        if ($this->setting('enabled')) $this->disabled = false;
        return $this;
    }

    // Set When the Component must be Executed
    function run()
    {
        if($this->setting('enabled'))
        {
            add_action('admin_init', array($this, 'tweak'));
        }
    }

    // Remove the bulk actions from posts, pages and custom post types listings in admin.
    function tweak()
    {
        foreach( $this->get_post_types() as $type => $data) {
            if($this->setting('disable_bulk_' . $type))
            {
                add_filter('bulk_actions-edit-' . $type, '__return_empty_array');
            }
        }
    }
}

Anyone has any clue?

Thanks a lot in advance!

  • please complete your code, how your class called, how and where you call function tweak(), and how $this->setting('disable_bulk_' . $type) work? – Jevuska Apr 26 '16 at 10:26
  • and make sure you use hook admin_init – Jevuska Apr 26 '16 at 11:02
  • Hi @Jevuska, thanks a lot for your answer. I edited my question and added the full class so you can see all the stuff happening there. I was already using the admin_init hook. Thanks again! – G. Oronoz Apr 26 '16 at 13:41
  • where is $this->setting() function? how about is_active, where is you call this function? – Jevuska Apr 26 '16 at 14:03
  • Hi @Jevuska, thanks again. That functions are in a file from the core of a framework that was not coded by me nor my mates. is_active only checks that the first option has a true value and then continues by executing the tweak with the run function... – G. Oronoz Apr 26 '16 at 16:01
0

Welcome to Stack exchange :)

Some other plugin ( or your plugin elsewhere ) may be getting executed later, and maybe adding some elements to the array.

Try to give your hook a higher priority, replacing your tweak method with following...

function tweak()
{
    foreach( $this->get_post_types('objects') as $type => $data) {
        if($this->setting('disable_bulk_' . $type))
        {
            if ( ! empty( $data->rewrite['slug'] ) ) {
                $type = $data->rewrite['slug'];
            }
            add_filter('bulk_actions-edit-' . $type, '__return_empty_array',999);
        }
    }
}

This should delay the execution you function. Lemme know if that helps :)

Cheers

----------------EDIT------------------------

Gets post type objects instead of default labels. Uses slug instead of post type name when slug is defined ( and not empty ) in post type object.

  • Hello @Shramee Srivastav, thanks a lot for your answer, but unfortunately it did not work. I even tried with PHP_INT_MAX instead of 999 but no luck. I also checked the codex and saw that the priority should go before the args, but anyway it didn't work. It is really weird... – G. Oronoz Apr 26 '16 at 15:56
  • Fun fact is that if I replace the $type by the slug of the CPT (e.g. 'articles') it works. If I echo $type (for example in the admin_notices hook) it displays properly the slugs of all CPTs. – G. Oronoz Apr 26 '16 at 16:02
  • Hi G. Oronez, Edited my answer to use post type slugs whenever available. Some plugins may use a different slug then the post type name so it gets a bit tricky. – shramee Apr 27 '16 at 3:22
  • Hello, once more, many thanks, but I got no luck with that. I also tried to change the get_post_types function to return objects instead of names and then get the slug, but it did not work... I will keep digging! – G. Oronoz Apr 28 '16 at 11:20

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.