Note that there are some explanatory texts on larger screens.

plurals
  1. POWordpress plugin, trying to pass variable from admin page to function
    primarykey
    data
    text
    <p>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 <code>bp_tweet_button_activity_filter()</code>. 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 <code>tweet_urls_menu_options</code> function. </p> <p>The plugin takes the link of an activity stream post and shortens it with a custom <em>YOURLS URL</em> shortener, so the values needed are the username, password, and URL of the API for the shortener.</p> <p>Thank you for any help or advice, I'll be continuing to read up on PHP and plugin development in the meantime. </p> <p><strong>Note:</strong> plugin originally modified from BuddyPress Tweet Button by modem looper.</p> <pre><code>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 ]) &amp;&amp; $_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 ?&gt; &lt;div class="updated"&gt;&lt;p&gt;&lt;strong&gt;&lt;?php _e('settings saved.', 'menu-test' ); ?&gt;&lt;/strong&gt;&lt;/p&gt;&lt;/div&gt; &lt;?php } // Now display the settings editing screen echo '&lt;div class="wrap"&gt;'; // header echo "&lt;h2&gt;" . __( 'Tweet Urls Settings', 'menu-test' ) . "&lt;/h2&gt;"; // settings form ?&gt; &lt;form name="form1" method="post" action=""&gt; &lt;input type="hidden" name="&lt;?php echo $hidden_field_name; ?&gt;" value="Y"&gt; &lt;p&gt;&lt;?php _e("Username:", 'menu-test' ); ?&gt; &lt;input type="text" name="&lt;?php echo $data_field_name; ?&gt;" value="&lt;?php echo $opt_val; ?&gt;" size="20"&gt; &lt;/p&gt;&lt;hr /&gt; &lt;p&gt;&lt;?php _e("Password:", 'menu-test' ); ?&gt; &lt;input type="text" name="&lt;?php echo $data_field_pass; ?&gt;" value="&lt;?php echo $opt_val_pass; ?&gt;" size="20"&gt; &lt;/p&gt;&lt;hr /&gt; &lt;p&gt;&lt;?php _e("API:", 'menu-test' ); ?&gt; &lt;input type="text" name="&lt;?php echo $data_field_api; ?&gt;" value="&lt;?php echo $opt_val_api; ?&gt;" size="20"&gt; &lt;/p&gt;&lt;hr /&gt; &lt;p class="submit"&gt; &lt;input type="submit" name="Submit" class="button-primary" value="&lt;?php esc_attr_e('Save Changes') ?&gt;" /&gt; &lt;/p&gt; &lt;/form&gt; &lt;/div&gt; &lt;?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' =&gt; $url, 'keyword' =&gt; $keyword, 'format' =&gt; $format, 'action' =&gt; 'shorturl', 'username' =&gt; $username, 'password' =&gt; $password )); // Fetch and return content $data = curl_exec($ch); curl_close($ch); $newurl=$data; $ttitle = get_the_title(); if(strlen($ttitle&gt;110)) { $ttitle = substr($ttitle, 0,110); $ttitle .='…'; } $ttitle .=' '; $turl = 'http://twitter.com/home?status='.$ttitle.$newurl; echo '&lt;a target="_blank" href="'.$turl.'"&gt;Tweet This!&lt;/a&gt;'; } 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 '&lt;span class="twitter-share-blog-button"&gt;&lt;a href="http://twitter.com/share" class="twitter-share-button" data-count="none"&gt;Tweet&lt;/a&gt;&lt;script type="text/javascript" src="http://platform.twitter.com/widgets.js"&gt;&lt;/script&gt;&lt;/span&gt;'; } add_action('bp_before_blog_single_post', 'bp_tweet_button_blog_filter'); function bp_tweet_button_insert_head() { ?&gt; &lt;style type="text/css"&gt; span.twitter-share-button { position: relative; top: 6px; } .twitter-share-blog-button { float: right; } &lt;/style&gt; &lt;?php } add_action('wp_head', 'bp_tweet_button_insert_head'); </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload