• My ultimate goal is to have the ability to comment on all pages within my site … however not all pages are handled with WordPress. The pages outside of WordPress are all based on a template called “player.php” and are filled with dynamic information from a database based on “playerID” variable.

    For example, mysite.com/player.php?playerID=1 would show a page about Michael Jordan. mysite.com/player.php?playerID=2 would show a page about LeBron James.

    My solution was to make a WordPress page for each individual playerID. All each page would have as its content is the following (using a PHP plugin of course):

    <?php
    $playerID = 1;
    include("player.php");
    ?>

    The include works correctly, but the variable NEVER gets passed. I’ve been trying so many ways to get this over but I can’t seem to make any progress. Any suggestions out there?

    Thanks in advance!

Viewing 9 replies - 1 through 9 (of 9 total)
  • There is not much documentation on how to do it, or at least there was not when I first needed it, but here you go:

    First, set it up:

    function my_query_vars ($vars) {
      $vars[] = "playerID";
      return $vars;
    }
    
    add_filter('query_vars', 'my_query_vars');

    Then to use it:

    if (get_query_var("playerID")) {
      $playerID = get_query_var("playerID");
    }

    Have fun ??

    Thread Starter gunsnros3s

    (@gunsnros3s)

    Intriguing … though I’m afraid I’m not positive where to stick this code!

    Should I place the set up on a functions php? And then the use code should be in player.php? Where would I set $playerID? Thanks, again!

    It goes in your plugin. Or did you not have the code in plugin form yet?

    The first set of code can go anywhere in the plugin, but it does have to be in a plugin. It does not even need to be in the plugin you are using the data in though.

    The second set of code just goes wherever in your plugin you need to reference the contents of the $playerID variable it sets.

    Thread Starter gunsnros3s

    (@gunsnros3s)

    Whoops … I think I may have overstepped my bounds!

    I’ve never done any plugin work with WordPress. I was just putting in the PHP code into the contents of my Page. I was able to do this because I have a plugin called PHP EXEC that allows <?php ?> in the content of posts/pages.

    Are you saying that I will need to create my own plugin to accept a variable defined from a post/page in order to pass it to an included php?

    I really appreciate all the help, DD!

    For your needs, it will not be as complicated then.

    Save this code as query_plugin.php and upload it to your plugins folder and activate it:

    <?php
    /*
    Plugin Name: Query Plugin Code
    Plugin URI:
    Description: Adds code to get the playerID
    Author:
    Version:
    Author URI:
    */
    
    function playerID_query_vars ($vars) {
      $vars[] = "playerID";
      return $vars;
    }
    
    add_filter('query_vars', 'playerID_query_vars');
    
    ?>

    Then just add this in where the PHP code you wrote is:

    if (get_query_var("playerID")) {
      $playerID = get_query_var("playerID");
    }

    It sets $playerID to the value passed to the page, then you can do whatever you want with it.

    *** I have not used PHP EXEC before (I always just add code to plugins, or directly in the template or core files) so I am not sure if it processes WordPress functions (like get_query_var) but you can give it a shot ??

    Thread Starter gunsnros3s

    (@gunsnros3s)

    Quit when you’ve had enough!

    Things still aren’t working out. I uploaded query_plugin.php as you specified with the code. Thanks! In the second section of code should it be:

    $playerID = playerID_query_vars("playerID");

    instead of

    $playerID = get_query_var("playerID");

    ? Otherwise I don’t know when I’m ever supposed to be using the function playerID_query_vars() we built in the plugin.

    Finally where do the actual values of $playerID ever get passed from? I don’t see where “$playerID = 1”, for example, would ever be coded. That would have to somehow be attached to a WordPress page. Are those still somehow defined on the individual content pages in WordPress?

    Ugh … someday this will work!

    Actually, no, it should be

    $playerID = get_query_var("playerID");

    get_query_var is a WordPress function ??

    You do not use the plugin from the function directly, it is called by this line:

    add_filter('query_vars', 'playerID_query_vars');

    Which basically just tells wordpress “when the WP query stuff runs, also run the playerID_query_vars function” and then that function just adds a new query to the list of those WP will recognize (in this case, playerID).

    All you have to do in your actual (non-plugin) code is this:

    if (get_query_var("playerID")) {
      $playerID = get_query_var("playerID");
    }

    Question, what would the structure be for multiple variables? Would I need a different parsing function for each? I found something like this:

    function my_query_vars ($vars) {
    	foreach( (array) $vars as $k => $v ){
    		$a[] =  $v ;
    	}
    	return $a;
    }

    but it doesn’t seem to do it for me….
    any shove in the right direction would be great..

    ps. god bless word press. :oD

    slow day, not enough coffee, in case anyone else is looking:

    function my_query_vars ($vars) {
    
    	$vars[] = "VAR1";
    	$vars[] = "VAR2";
    	$vars[] = "VAR3";
    	$vars[] = "VAR4";
    	$vars[] = "VAR5";
            /* .... etc .... */
    	return $vars;
    }
    add_filter('query_vars', 'my_query_vars');
Viewing 9 replies - 1 through 9 (of 9 total)
  • The topic ‘Pass variable from page to included PHP’ is closed to new replies.