- This topic has 18 replies, 2 voices, and was last updated 7 years, 7 months ago by
egnsh98.
-
AuthorPosts
-
-
June 19, 2014 at 10:07 am #1849
egnsh98MemberGood morning!
First off I’d like to say fantastic work developing Piklist. I made the decision to switch over to your framework half way through developing a plugin, and I’ve never looked back!
I do have some questions however. I’m finding myself needing to include a file within a settings file in /parts/settings/. I’m developing a twitter plugin and within /settings/ I have a “twitter.php”. Within that file are the settings associated with authenticating. What I am trying to do is include the twitter API library from that settings file so I can store the user’s profile data in option fields. Everytime I try to include the file (whether its an absolute, or relative path), I get a “No such file” error.
If this is not possible, how would I go about passing data from my main plugin class file (that isn’t yet in the database) to the settings field files.
Let me know.
Thanks! 🙂
-
June 19, 2014 at 10:33 am #1850
SteveKeymaster@egnsh98– You should be able to
required_oncein any php file in the parts/ folder. Is it possible you are not including the correct or full path to the file?Fortunately, Piklist can help you with this since it stores the paths of all Piklist Plugins in:
piklist::$paths['my-plugin-folder']So if your code is located in /my-plugin/api/api.php, try including it like this:
require_once(piklist::$paths['my-plugin'] . '/api/api.php');Make sure you include
Plugin Type:in your plugins header block, so Piklist will store the path.Let us know if that works for you.
-
June 19, 2014 at 10:56 am #1851
egnsh98MemberThank you that seemed to do the trick! How would I go about passing data from my main class, to a PHP file within /parts/. It is not in the same class so I can’t use “$this” and I don’t to use super globals.
-
June 19, 2014 at 11:08 am #1852
-
June 20, 2014 at 10:53 am #1853
egnsh98MemberStill having some trouble, OOP is still new to me. I am trying to call a Class method from outside my main plugin class. My
get_user()call works fine inside of my “Main” class, but when trying to call it from outside,NULLis returned. Here is what I have done so far, I have included only the code I thought was relevant to the problem and have put comments where necessary.<?php class Main { protected $user = null; public function __construct() { // Init and admin init action hooks } public function init() { // Check for piklist plugin // Enqeue admin styles and scripts // Action hook on authenticate function } public function authenticate() { $this->set_user( "John Smith" ); } public function set_user( $new_user ) { $this->user = $new_user; } public function get_user() { return $this->user; } } /* ------------------------------------------------ * Code from /parts/settings/twitter.php * ------------------------------------------------*/ $instance = Main::get_instance(); $user = $instance->get_user(); var_dump ( $user ); // Hidden field gets the username and registers it as a setting in the database piklist('field', array( 'type' => 'hidden' ,'field' => 'user_name' ,'value' => $user )); ?> -
June 20, 2014 at 3:50 pm #1854
egnsh98MemberI’ve got the getter methods working now, and when I print them out in /parts/settings/twitter.php, they display, but don’t seem to be getting picked up by the hidden field. Should I be setting the $user variable on a different parameter?
-
June 20, 2014 at 7:02 pm #1855
SteveKeymasterPlease post the full code for file: /parts/settings/twitter.php
-
June 21, 2014 at 3:37 pm #1862
egnsh98MemberHere it is. And as I said,
$userhas a value if I echo it, but does not get passed into the value parameter of the hidden field.<?php /* Title: Twitter Setting: twitter_settings Tab: Twitter Settings Order: 200 */ // Consumer Key piklist('field', array( 'type' => 'text' ,'field' => 'consumer_key' ,'label' => __('Consumer Key') ,'value' => __('Consumer Key') ,'help' => __('Enter the Consumer Key found in your Twitter Apps settings') ,'capability' => 'manage_options' ,'position' => 'wrap' ,'serialize' => 'false' ,'attributes' => array( 'title' => 'Twitter API Application Consumer Key' ,'alt' => 'Twitter API Application Consumer Key' ,'tabindex' => '1' ,'onfocus' => "if(this.value == 'Consumer Key') { this.value = ''; }" ,'columns' => '12' ) )); // Consumer Secret piklist('field', array( 'type' => 'text' ,'field' => 'consumer_secret' ,'label' => __('Consumer Secret') ,'value' => __('Consumer Secret') ,'help' => __('Enter the Consumer Secret found in your Twitter Apps settings') ,'capability' => 'manage_options' ,'position' => 'wrap' ,'serialize' => 'false' ,'attributes' => array( 'title' => 'Twitter API Application Consumer Secret' ,'alt' => 'Twitter API Application Consumer Secret' ,'tabindex' => '2' ,'onfocus' => "if(this.value == 'Consumer Secret') { this.value = ''; }" ,'columns' => '12' ) )); // Access Token piklist('field', array( 'type' => 'text' ,'field' => 'oauth_token' ,'label' => __('Access Token') ,'value' => __('Access Token') ,'help' => __('Enter the Access Token found in your Twitter Apps settings') ,'capability' => 'manage_options' ,'position' => 'wrap' ,'serialize' => 'false' ,'attributes' => array( 'title' => 'Twitter API Application Access Token' ,'alt' => 'Twitter API Application Access Token' ,'tabindex' => '1' ,'onfocus' => "if(this.value == 'Access Token') { this.value = ''; }" ,'columns' => '12' ) )); // Access Token Secret piklist('field', array( 'type' => 'text' ,'field' => 'oauth_token_secret' ,'label' => __('Access Token Secret') ,'value' => __('Access Token Secret') ,'help' => __('Enter the Access Token Secret found in your Twitter Apps settings') ,'capability' => 'manage_options' ,'position' => 'wrap' ,'serialize' => 'false' ,'attributes' => array( 'title' => 'Twitter API Application Access Token Secret' ,'alt' => 'Twitter API Application Access Token Secret' ,'tabindex' => '1' ,'onfocus' => "if(this.value == 'Access Token Secret') { this.value = ''; }" ,'columns' => '12' ) )); // Get the user's profile information $instance = Main::get_instance(); $user = $instance->get_user(); // Twitter Name piklist('field', array( 'type' => 'hidden' ,'field' => 'twitter_name' ,'value' => $user )); ?> -
June 21, 2014 at 5:52 pm #1863
SteveKeymasterTry changing your functions to STATIC:
public static function get_user()You should be able to use:
'value' => main::get_user() -
June 21, 2014 at 6:41 pm #1864
SteveKeymasterHow do you know it’s not working? When you save the form are you checking the post meta?
-
June 22, 2014 at 12:11 pm #1877
egnsh98MemberI’m not sure how changing my functions to static would help. The getter and setter functions I have in my other class reference
$thisso changing to static would place that out of context. If I understand correctly, when I save the settings the value of the hidden field would be$userand it would get stored into the database? Because that is what I am trying to achieve.Within /parts/settings/twitter.php if I echo the value of
$userbefore the hidden field it prints out what I would expect, but when I check the options in the database the value of the hidden field is 0, and not the value of$user -
June 22, 2014 at 4:14 pm #1878
SteveKeymasterIf you want us to look at your plugin please zip it up and email to [email protected]
-
June 23, 2014 at 7:02 am #1881
egnsh98MemberSure I can do that, but I have one question first. If I am understanding the hidden field correctly, any value passed to the ‘value’ parameter of the field will be registered in the options database table?
-
June 23, 2014 at 7:34 am #1882
egnsh98MemberAfter investigating further, even manually typing a string into the value parameter of the hidden field registers nothing in the database. This is leaning me towards thinking the syntax of my hidden field is wrong? The hidden field does not exist on a post, user, or taxonomy, but on a custom admin page that I have created and registered with WordPress.
-
June 23, 2014 at 9:17 am #1883
-
June 23, 2014 at 9:29 am #1884
egnsh98MemberAs requested, I have emailed a copy of the code.
Thanks!
-
June 23, 2014 at 3:15 pm #1887
SteveKeymaster@egnsh98– After looking at your code I think there may be a better way to do it. We suggest using the Piklist Filter
<a href="http://piklist.com/user-guide/docs/piklist_pre_update_option/">piklist_pre_update_option</a>. This filter will allow you to manipulate the data entered into the settings page BEFORE the page is saved.You probably want to do something like this:
add_filter('piklist_pre_update_option','my_update_option',10,3); function my_update_option($settings, $new, $old) { $field_one = $settings['field_one']; $new_field_one = my_function($field_one); if (isset($_REQUEST['autotweet_settings'])) { $settings['field_one'] = 'new_field_one'; } return $settings; }Let me know if that works for you.
-
June 26, 2014 at 11:16 am #1903
egnsh98MemberThanks Steve!
We are definitely moving in the right direction. Currently I need to click “Save Settings” twice for the fields to be registered in the database. In addition the WordPress “Settings saved.” notification appears twice, not sure if this is related though, just an observation.
Regards.
-
July 4, 2014 at 1:54 pm #1953
egnsh98MemberI still haven’t been able to figure this out. I’ve done some research but nothing seems to be relevant to Piklist. Has anyone else experienced this?
-
-
AuthorPosts
- The topic ‘Include a file within Piklist’ is closed to new replies.