Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

This question already has an answer here:

I'm coding a custom plugin to perform some actions at specific times. I'm playing with add_action and add_filter but the current task I'm working on is posting the post url to Twitter account. Problem, is I would like to display twitter API HTTP response as an Admin Notice (Codex of Admin Notice) but I'm completely lost.

Here's my code Logic :


function Twit_the_url( $post_ID )  
{
    //connect to twitter api

    //send tweet with url

    //get twitter response

    //send response to admin notice :
    add_action('admin_notices', create_function( "", "echo \"

$httpstatus

\";" ) ); return $post_ID; } add_action( 'publish_post', 'Twit_the_url');

Everything goes well until I call the admin_notices. the tweet is created, $httpstatus exist but my message is not displayed ! I've been looking around for tips and how-to but I can't understand why it's not working. I've looked at two differents plugins to see how they do it but it change nothing.

So if anyone could tell me where I'm wrong that would be great ! Thx

share|improve this question

marked as duplicate by toscho Jun 9 '13 at 4:43

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

1 Answer 1

This would be an good use PHP 5.3+ anonymous functions.

Closure example:

<?php
function Twit_the_url( $post_ID )  
{
    //connect to twitter api

    //send tweet with url

    //get twitter response

    //send response to admin notice :
    add_action('admin_notices', function() use ($httpstatus) {
        echo '<div class="error"><p>', esc_html($httpstatus), '</p></div>';
    });

    return $post_ID;
}
add_action( 'publish_post', 'Twit_the_url');

Alternatively, you could wrap up both the Twit_the_url and admin_notices callback into a class and use class property.

share|improve this answer

Not the answer you're looking for? Browse other questions tagged or ask your own question.