2011-03-20 68 views
0

我正在嘗試將管理面板添加到WordPress插件。這是我第一次嘗試使用插件,我不經常使用PHP。我的問題是將值輸入到管理面板中,以在功能bp_tweet_button_activity_filter()中需要使用它們的位置。這些值保存並在管理頁面上正常顯示。我一直在竊聽它,可能已經添加了一些多餘的代碼。我認爲問題出現在該功能的開頭,或者可能是功能的開始。Wordpress插件,試圖將變量從管理頁面傳遞到函數

該插件獲取活動流帖子的鏈接並將其縮短爲自定義YOURLS URL縮短,因此需要的值是縮略詞API的用戶名,密碼和URL。

感謝您的任何幫助或建議,我將繼續閱讀PHP和插件開發的同時。

注意:插件最初從調制解調器looper的BuddyPress按鈕修改。

function bp_example_init() { 
    require(dirname(__FILE__) . '/bp-tweet-urls.php'); 
} 

define ('BP_TWEET_BUTTON_VERSION', '0.1'); 

//Admin Menu 
add_action('admin_menu', 'tweet_urls_menu'); 

function tweet_urls_menu() { 
    add_options_page('Tweet Urls Menu Options', 'Tweet Urls', 'manage_options', 'my-unique-identifier', 'tweet_urls_menu_options'); 
} 

function tweet_urls_menu_options() { 
    if (!current_user_can('manage_options')) { 
     wp_die(__('You do not have sufficient permissions to access this page.')); 
    } 

     // variables for the field and option names 
    $opt_name = 'username'; 
    $opt_pass = 'password'; 
    $opt_api = 'api_url'; 
    $hidden_field_name = 'mt_submit_hidden'; 
    $data_field_name = 'username'; 
    $data_field_pass = 'password'; 
    $data_field_api = 'api_url'; 

    // Read in existing option value from database 
    $opt_val = get_option($opt_name); 

    $opt_val_pass = get_option($opt_pass); 
    $opt_val_api = get_option($opt_api); 

    // See if the user has posted us some information 
    // If they did, this hidden field will be set to 'Y' 
    if(isset($_POST[ $hidden_field_name ]) && $_POST[ $hidden_field_name ] == 'Y') { 
     // Read their posted value 
     $opt_val = $_POST[ $data_field_name ]; 
     $opt_val_pass = $_POST[ $data_field_pass ]; 
     $opt_val_api = $_POST[ $data_field_api ]; 

     // Save the posted value in the database 
     update_option($opt_name, $opt_val); 
     update_option($opt_pass, $opt_val_pass); 
     update_option($opt_api, $opt_val_api); 
     // Put a settings updated message on the screen 
?> 
<div class="updated"><p><strong><?php _e('settings saved.', 'menu-test'); ?></strong></p></div> 
<?php 

    } 
    // Now display the settings editing screen 
    echo '<div class="wrap">'; 
    // header 
    echo "<h2>" . __('Tweet Urls Settings', 'menu-test') . "</h2>"; 

    // settings form 
    ?> 
<form name="form1" method="post" action=""> 
<input type="hidden" name="<?php echo $hidden_field_name; ?>" value="Y"> 

<p><?php _e("Username:", 'menu-test'); ?> 
<input type="text" name="<?php echo $data_field_name; ?>" value="<?php echo $opt_val; ?>" size="20"> 
</p><hr /> 

<p><?php _e("Password:", 'menu-test'); ?> 
<input type="text" name="<?php echo $data_field_pass; ?>" value="<?php echo $opt_val_pass; ?>" size="20"> 
</p><hr /> 

<p><?php _e("API:", 'menu-test'); ?> 
<input type="text" name="<?php echo $data_field_api; ?>" value="<?php echo $opt_val_api; ?>" size="20"> 
</p><hr /> 

<p class="submit"> 
<input type="submit" name="Submit" class="button-primary" value="<?php esc_attr_e('Save Changes') ?>" /> 
</p> 

</form> 
</div> 

<?php 

} 

/** 
* bp_tweet_button_activity_filter() 
* 
* Adds tweet button to activity stream. 
* 
*/ 
function bp_tweet_button_activity_filter() { 

    //admin username 
    $username = get_option('opt_val'); 
    //admin password 
    $password = get_option('opt_val_pass'); 
    //url to your custom YOURLS site API 
    $api_url = get_option('opt_val_api'); // 

    //get activity stream post link 
    $url = bp_get_activity_thread_permalink(); 
    $ch = curl_init(); 
    curl_setopt($ch, CURLOPT_URL, $api_url); 
    curl_setopt($ch, CURLOPT_HEADER, 0);   // No header in the result 
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); // Return, do not echo result 
    curl_setopt($ch, CURLOPT_POST, 1);    // This is a POST request 
    curl_setopt($ch, CURLOPT_POSTFIELDS, array( // Data to POST 
     'url'  => $url, 
     'keyword' => $keyword, 
     'format' => $format, 
     'action' => 'shorturl', 
     'username' => $username, 
     'password' => $password 
    )); 
    // Fetch and return content 
    $data = curl_exec($ch); 
    curl_close($ch); 
    $newurl=$data; 
    $ttitle = get_the_title(); 
    if(strlen($ttitle>110)) { 
     $ttitle = substr($ttitle, 0,110); 
     $ttitle .='…'; 
    } 
    $ttitle .=' '; 
    $turl = 'http://twitter.com/home?status='.$ttitle.$newurl; 
    echo '<a target="_blank" href="'.$turl.'">Tweet This!</a>'; 
} 
add_action('bp_activity_entry_meta', 'bp_tweet_button_activity_filter'); 
/** 
* bp_tweet_button_blog_filter() 
* 
* Adds tweet button to blog posts. 
* 
*/ 
function bp_tweet_button_blog_filter() { 

    echo '<span class="twitter-share-blog-button"><a href="http://twitter.com/share" class="twitter-share-button" data-count="none">Tweet</a><script type="text/javascript" src="http://platform.twitter.com/widgets.js"></script></span>'; 

} 
add_action('bp_before_blog_single_post', 'bp_tweet_button_blog_filter'); 

function bp_tweet_button_insert_head() { 
?> 
<style type="text/css"> 
span.twitter-share-button { 
    position: relative; 
    top: 6px; 
} 
.twitter-share-blog-button { 
    float: right; 
} 
</style> 
<?php 
} 
add_action('wp_head', 'bp_tweet_button_insert_head'); 

回答

0

我只是想通了,感謝任何看着這個的人。

我的問題是在這部分代碼,從這個打算:

function bp_tweet_button_activity_filter() { 

    //admin username 
    $username = get_option('opt_val'); 
    //admin password 
    $password = get_option('opt_val_pass'); 
    //url to your custom YOURLS site API 
    $api_url = get_option('opt_val_api'); // 

    //get activity stream post link 
    $url = bp_get_activity_thread_permalink(); 
    $ch = curl_init(); 
    curl_setopt($ch, CURLOPT_URL, $api_url); 
    curl_setopt($ch, CURLOPT_HEADER, 0);   // No header in the result 
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); // Return, do not echo result 
    curl_setopt($ch, CURLOPT_POST, 1);    // This is a POST request 
    curl_setopt($ch, CURLOPT_POSTFIELDS, array( // Data to POST 
     'url'  => $url, 
     'keyword' => $keyword, 
     'format' => $format, 
     'action' => 'shorturl', 
     'username' => $username, 
     'password' => $password 

這樣:

function bp_tweet_button_activity_filter() { 


    //get activity stream post link 
$url = bp_get_activity_thread_permalink(); 
$ch = curl_init(); 
curl_setopt($ch, CURLOPT_URL, get_option('api_url')); 
curl_setopt($ch, CURLOPT_HEADER, 0);   // No header in the result 
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); // Return, do not echo result 
curl_setopt($ch, CURLOPT_POST, 1);    // This is a POST request 
curl_setopt($ch, CURLOPT_POSTFIELDS, array( // Data to POST 
    'url'  => $url, 
    'keyword' => $keyword, 
    'format' => $format, 
    'action' => 'shorturl', 
    'username' => get_option('username'), 
    'password' => get_option('password') 
));