WordPress.org

Ready to get started?Download WordPress

Forums

Problem of use of add_filter within a class plugin (4 posts)

  1. olivier89
    Member
    Posted 3 years ago #

    I made simple class plugin to test upload capacity in a personnalized directory. After a search i came to this : http://pastebin.com/RkpZ9vKh But as you can see if i call add_filter from within the class :

    add_filter('upload_dir', array(&$this, 'myplug_upload_dir_inside'));
    or
    add_filter('upload_dir', 'myplug_upload_dir_outside');
    my upload is done to the standard directory

    But if i use the add_filter from outside the class, the upload is correctly directed to where i expected. The problem here is this filter is then available to all pages : when i then upload from the Library, the file is set to the new directory too.

    How to make this filter have a limited action to this plugin / page using my code structure ?

    Thanks

  2. markparolisi
    Member
    Posted 3 years ago #

    I usually set all of my WP actions in a static method and call them like this in my constructor or other setup method

    add_filter('upload_dir', array(__CLASS__, 'static_method_name'));

  3. olivier89
    Member
    Posted 3 years ago #

    You're right. But this won't do.
    Sorry for thos who looked at my paste , it was the wrong one (for a topic closed).
    The real paste for this problem is http://pastebin.com/HY3RCzgM

    I must add that the upload part is directly taken from the wp-admin/media-upload.php page

    I tried to add a
    if (isset($_GET["page"]) && $_GET["page"] == "myplug_menu")
    add_filter('upload_dir', 'myplug_upload_dir_outside');
    but this doesn't work since i use the media-upload page code

  4. Wykydtron
    Member
    Posted 3 years ago #

    olivier did you ever figure out how to use add_filter inside an class?

Topic Closed

This topic has been closed to new replies.

About this Topic