2011-03-20 7 views
0

WordPress 플러그인에 관리자 패널을 추가하려고합니다. 그것은 플러그인에 대한 작업으로 내 첫 시도이고 나는 자주 PHP를 사용하지 않습니다. 내 문제는 관리자 패널에 입력 된 값을 기능 bp_tweet_button_activity_filter()에서 사용해야하는 곳으로 가져 오는 것입니다. 값은 저장되고 관리자 페이지에 잘 표시됩니다. 나는 그것을 조금 해킹 해왔고 약간의 불필요한 코드를 추가했을지도 모른다. 나는 문제가 그 기능의 시작 라인에 있거나, 아마도 tweet_urls_menu_options 기능의 시작 부분에 있다고 생각한다.Wordpress 플러그인이 관리자 페이지에서 함수로 변수를 전달하려고합니다.

플러그인은 활동 스트림 게시물의 링크를 가져 와서 사용자 정의 YOURLS URL 단축키로 단축합니다. 따라서 필요한 값은 단축키 API의 사용자 이름, 비밀번호 및 URL입니다.

도움이나 조언을 주셔서 감사합니다. 그동안 PHP 및 플러그인 개발에 대한 글을 계속 읽으겠습니다.

참고 : 원래 BuddyPress에서 수정 된 플러그인 Tweet button 모뎀 루퍼로.

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') 
)); 
관련 문제