0

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

0

1 Answer 1

2

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.

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