I am trying to write a plugin in wordpress and have been doing it in stages, I understand how to add javascript to the wp_head however adding multiple function calls to the plugins options is something I don’t think I’m understanding. This is my first attempt at trying to write a plugin so I know I am going about this wrong. Any feedback is greatly appreciated!
If I have:
function tweet_feed_it() {
echo '<script type="text/javascript">
$(document).ready(function() {
$(".tweet").tweet({
modpath: "path/to/plugin/twitter/",
username: "twitter_username",
join_text: "auto",
count: tweet_count,
auto_join_text_default: "We tweeted,",
auto_join_text_ed: "We",
auto_join_text_ing: "We were",
auto_join_text_reply: "We replied to",
auto_join_text_url: "We were checking out",
loading_text: "loading tweets..."
});
});
</script>'};
// Add to head
add_action('wp_head', 'tweet_feed_it');
The script shows up in wp_head fine.
But in my wordpress plugin I have set some options and I want to call them to use within the script tags.
I will show what i have tried so you get an idea of what I am trying to accomplish at least:
class TweetFeedIt {
function tweet_feed_it() {
function twitter_username_callback() {
function tweet_count_callback() {
printf( '<script type="text/javascript">
$(document).ready(function() {
$(".tweet").tweet({
modpath: "http://test.phoenixlaef.com.au/wp-content/plugins/Tweet-Feed/twitter/",
username: "twitter_username",
join_text: "auto",
count: tweet_count,
auto_join_text_default: "We tweeted,",
auto_join_text_ed: "We",
auto_join_text_ing: "We were",
auto_join_text_reply: "We replied to",
auto_join_text_url: "We were checking out",
loading_text: "loading tweets..."
});
});
</script>
'
, esc_attr( $this->options['twitter_username']), esc_attr( $this->options['tweet_count'])
);
}
}
}
}
// Add to head
add_action('wp_head', 'tweet_feed_it', 'twitter_username_callback', 'tweet_count_callback');
Do I need to register the option settings to use it like this or is what I am trying to do just not practical?
Thanks for your help in advance 🙂
Nesting functions in PHP very rarely makes sense. There are some advanced reason for doing it, but I doubt they are needed in this case.
The
add_action()
function can take up to 4 parameters, but those parameters are not the names of the nested functions you wrote above.Read the documentation for PHP, for
add_action()
and for adding scripts. PHP does exactly what you tell it to do, but you have to follow the conventions of the language.