WordPress.org

Ready to get started?Download WordPress

Forums

Restrict conditions for something with mutliple add_actions? (9 posts)

  1. Rob
    Member
    Posted 8 years ago #

    I'm trying to develop a plugin which displays a small message in the top corner (including the Fade Anything Technique) when a comment is posted. This is very similar to the messages in the admin section when you publish a post. The problem I have is with the add_action.

    If I use comment_post, I get an error as it tries to post the message (a simple div) in the header. Of course, if I use wp_head, then it posts on every page, not just when the comment is posted.

    There was a similar problem on this topic, which was never resolved.

    Any ideas how I could do it? I'm afraid I'm fairly new to plugin code.

  2. Rob
    Member
    Posted 8 years ago #

    To put it another way, can I combine the following two actions:
    add_action('comment_post', 'fade_comment');
    add_action('wp_footer', 'fade_comment');

    So that fade_comment is executed in the footer when a comment is posted.

  3. skippy
    Member
    Posted 8 years ago #

    You could tackle this in several ways.

    You could register your plugin against the wp_footer action, and then check for some indication that a comment was posted.

    Or you could do something a little more bulky, but easier to understand:
    register an action against comment_post that sets a global variable (something like global $mybar; $myvar = 1;).
    register an action against wp_footer that checks the value of $myvar and executes some action only if $myvar = 1.

  4. Rob
    Member
    Posted 8 years ago #

    Thanks. I've tried to get this to work, but although this has removed the errors and doesn't display the box all the time, it doesn't seem to work, and nothing happens. I suspect something is wrong with this part of the code, but what?

    add_action('comment_post', 'create_myvar');

    add_action('wp_footer', 'fade_comment');

    function create_myvar() {
    global $myvar;
    $myvar = 1;
    }

    function fade_comment() {
    if ($myvar == 1) {
    echo "<p id='commentsaved' class='fade'>Comment saved!
    ";
    }
    }

  5. skippy
    Member
    Posted 8 years ago #

    Inside fade_comment you need to specifically state that you want the global $myvar, otherwise you're getting a local $myvar for use only inside fade_comment.

    function fade_comment() {
    global $myvar;
    if ($myvar == 1) {
    echo "<p id='commentsaved' class='fade'>Comment saved!
    ";
    }
    }

    You'll also want to catch (and return) the comment ID in your comment_post action:
    function create_myvar($id = '') {
    global $myvar;
    $myvar = 1;
    return $id;
    }

    If you don't, plugins that fire after your's might not get the comment ID, and might break.

  6. Rob
    Member
    Posted 8 years ago #

    Thanks again for the reply, and especially for spotting the bug with the comment id. Still doesn't seem to be working though.

    I've put the full source so far here: http://www.cunningtitle.co.uk/fadecomments.txt

    Any idea what I've done wrong? I'm pretty new to PHP, and had hoped this would be a simple plugin!

  7. skippy
    Member
    Posted 8 years ago #

    What is happening? Anything? Above your add_action() calls, why not stick $myvar = 0; to ensure that there is a global $myvar. I don't honestly expect that to fix the problem, but it likely won't hurt.

  8. Rob
    Member
    Posted 8 years ago #

    I'm getting no results at all. It's acting as though the plugin isn't even there (and yes, I have made sure it's activated!)

  9. Rob
    Member
    Posted 8 years ago #

    *desperate bump*
    Is this perhaps a WordPress bug then? I don't think many plugins use comment_post - certainly not to display anything.

Topic Closed

This topic has been closed to new replies.

About this Topic

Tags