Forums

passing results between filters (7 posts)

  1. lakong
    Member
    Posted 3 years ago #

    I am trying to manipulate a post prior to saving. I first am using the category_save_pre filter to change the category based on some internal logic related to my app. It works fine.

    Once the category is changed I need to alter the post content depending on the value of the new category. I'm having trouble getting the second function which will manipulate the post content to know what the new category is.

    I have tried using a global variable in the first function and I know it works because if I create two functions outside of WP the second function will know the value of the global variable. So for example, I have something like so (note: I have simplified the logic to highlight what I'm trying to do):

    function category_change($cat_holder)
    {
    global $replaced_cat;

    if ($cat_holder[0] == 200)
    {
    $cat_holder[0] = 100;
    }
    return $cat_holder;
    }

    function content_change($text) {
    global $replaced_cat;
    $text = str_replace('%sbname%',$replaced_cat,$text);
    return $text;
    }

    add_filter('category_save_pre','category_change');
    add_filter('content_save_pre','content_change');

    ** So, if the category I'm trying to save is ID=200, the plugin will successfully change it to ID=100. However, if the text %sbname% is in the post content, it does not get replaced with the number 100. Instead it gets replaced with NULL.

  2. Roger Theriault
    Member
    Posted 3 years ago #

    At some point that global needs to have something assigned TO it. Did you simplify that out?

  3. lakong
    Member
    Posted 3 years ago #

    Roger, oops, I made a mistake in the example. Sorry. The first function will definitely assign $cat_holder to $replaced_cat like:

    $replaced_cat = $cat_holder[0];

    So the issue is that the value of $replaced_cat is not available to the second function. It does work outside of the plugin if I I call each function in sequence, but it does not work in WP when they are called in order via the two add_filter calls.

  4. lakong
    Member
    Posted 3 years ago #

    bump

  5. Roger Theriault
    Member
    Posted 3 years ago #

    @lakong - please don't bump

    Did you ever declare this variable outside of the scope of a function?

    Sounds like a php issue. The answer here: http://us2.php.net/global

  6. lakong
    Member
    Posted 3 years ago #

    Thanks for taking the time to answer this Roger. I actually saw the same post but the functions work fine outside of WP. The issue, I believe, is in the order that WP acts on the filters. I believe it first does the content filter and then the category filter. It doesn't matter what order I reference them with the add_filter constructs. I think I'll need to use a different hook for the content. Perhaps one of the action hooks.

  7. Roger Theriault
    Member
    Posted 3 years ago #

    You'll need to trace back the order of execution; you're right, it certainly does matter, at least in WordPress. (Sorry, there are enough "I pasted this stuff together why doesn't it work" questions lately... seems nobody wants to go and read the PHP man pages)

    For tracing the sequence of events, aside from the php files themselves, this resource might be helpful: http://adambrown.info/p/wp_hooks/version/2.7. There can be many reasons leading up to a particular action or filter being triggered... enough that I tend to favor the "go back and collect the data when it's needed" method - even if it means an extra db query or two - instead of the "I just published a new post so let's increment counter X" approach. Good luck.

Topic Closed

This topic has been closed to new replies.

About this Topic

Tags

No tags yet.