I want to make a function that will send automatic tweet when ever I publish some post on WordPress website.

I have included Codebird library inside functions.php, and I included it right.

But I have problem with creating function for tweeting, it doesn’t work

function post_to_twitter($message)
{
    $consumer_key = '....';
    $consumer_secret = '....';
    $access_token = '....';
    $access_secret = '....';

    CodebirdCodebird::setConsumerKey($consumer_key, $consumer_secret);
    $cb = CodebirdCodebird::getInstance();
    $cb->setToken($access_token, $access_secret);

    $params = array(
      'status' => $message
    );
    $reply = $cb->statuses_update($params);
}

$message = 'New post published';
post_to_twitter($message);

When I put it outside the function, it works, like this

$consumer_key = '....';
$consumer_secret = '....';
$access_token = '....';
$access_secret = '....';
$message = 'New post published';

CodebirdCodebird::setConsumerKey($consumer_key, $consumer_secret);
$cb = CodebirdCodebird::getInstance();
$cb->setToken($access_token, $access_secret);

$params = array(
  'status' => $message
);
$reply = $cb->statuses_update($params);

Read more here: Codebird library code owrks outside, but not inside a function


Solution:

If you know the solution of this issue, please leave us a reply in Comment section, to update the question.


Wordpress related questions and answers: