Tagged: ,

Viewing 18 reply threads
  • Author
    Posts
    • #1849
      egnsh98
      Member

      Good 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! 🙂

    • #1850
      Steve
      Keymaster

      @egnsh98– You should be able to required_once in 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.

    • #1851
      egnsh98
      Member

      Thank 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.

    • #1852
      Steve
      Keymaster

      @egnsh98–- Instead of $this, you can call it using your class and function:

      class::function() the same way you just called piklist::$paths().

      If you still need help, let us know the name of your class and function

    • #1853
      egnsh98
      Member

      Still 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, NULL is 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
      	));
      
      ?>
    • #1854
      egnsh98
      Member

      I’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?

    • #1855
      Steve
      Keymaster

      Please post the full code for file: /parts/settings/twitter.php

    • #1862
      egnsh98
      Member

      Here it is. And as I said, $user has 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
      	));
        
      ?>
    • #1863
      Steve
      Keymaster

      Try changing your functions to STATIC: public static function get_user()

      You should be able to use: 'value' => main::get_user()

    • #1864
      Steve
      Keymaster

      How do you know it’s not working? When you save the form are you checking the post meta?

    • #1877
      egnsh98
      Member

      I’m not sure how changing my functions to static would help. The getter and setter functions I have in my other class reference $this so 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 $user and 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 $user before 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

    • #1878
      Steve
      Keymaster

      If you want us to look at your plugin please zip it up and email to [email protected]

    • #1881
      egnsh98
      Member

      Sure 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?

    • #1882
      egnsh98
      Member

      After 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.

    • #1883
      Steve
      Keymaster

      @egnsh98– I reviewed your code and it worked for me. The fastest way to resolve this is to email us your code.

    • #1884
      egnsh98
      Member

      As requested, I have emailed a copy of the code.

      Thanks!

    • #1887
      Steve
      Keymaster

      @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.

    • #1903
      egnsh98
      Member

      Thanks 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.

    • #1953
      egnsh98
      Member

      I 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?

Viewing 18 reply threads
  • The topic ‘Include a file within Piklist’ is closed to new replies.