开发者

Wordpress plugin, trying to pass variable from admin page to function

I'm trying to add an admin panel to a WordPress plugin. It's my first attempt with working on a plugin and I don't use PHP frequently. My problem is getting values entered in the admin panel to where they need to be used in the function bp_tweet_button_activity_filter(). The values are saved and displayed fine on the admin page. I've been hacking it for a bit and may have added a little bit of superfluous code. I think the problem is in the beginning lines of that function, or maybe towards the beginning of the tweet_urls_menu_options function.

The plugin takes the link of an activity stream post and shortens it with a custom YOURLS URL shortener, so the values needed are the username, password, and URL of the API for the shortener.

Thank you for any help or advice, I'll be continuing to read up on PHP and plugin development in the meantime.

Note: plugin originally modified from BuddyPress Tweet Button by modem looper.

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://twitte开发者_开发问答r.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');


I just figured it out, thanks to any who looked at this.

My problem was in this part of the code, going from this:

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

to this:

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')
));
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜