• Resolved carymillsap

    (@carymillsap)


    I would like to store constants, like usernames and passwords, in a centralized constants.php file and then include that file in my snippet. However, within my snipped, global variables appear uninitialized. Example:

    Snippets Code:
    include “/home/me/public_html/cgi-bin/constants.php”; // defines $x=42
    add_shortcode(“my_shortcode”, “f”);
    function f($atts) {
    // …not using $atts for this example
    return “x is ‘$x'”;
    }

    Page’s Text Block content:
    …just regular Text Block content showing that [my_shortcode].

    Page’s output:
    …just regular Text Block content showing that x is ”.

    Page’s output that I was hoping for:
    …just regular Text Block content showing that x is ’42’.

    Thank you so much.

Viewing 10 replies - 1 through 10 (of 10 total)
  • Plugin Author Shea Bunge

    (@bungeshea)

    One way to do this to include the file inside the function itself. There are other ways, but this is probably one of the better ones.

    add_shortcode(“my_shortcode”, “f”);
    function f($atts) {
    	include “/home/me/public_html/cgi-bin/constants.php”; // defines $x=42
    	// …not using $atts for this example
    	return “x is ‘$x'”;
    }
    Thread Starter carymillsap

    (@carymillsap)

    Thank you, Shea.

    …I didn’t think I needed to tell you the whole story, but now I do. I’ve got functions, too, that I need to include. When I wrote my original ticket to you, I had constants and functions both in constants.php. (For some reason that I don’t understand, the functions make it into the symbol table, but the variables don’t. This is why I didn’t mention the functions before.) But the functions use the constants.

    I can’t include a file within my snippet function that contains function definitions, because I get a “Fatal error: Cannot redeclare function_name()”. I’ve separated the functions from the constants, so now I have both constants.php and functions.php. When I include functions.php outside of the snippet function, the functions can’t see the constants (even though functions.php does include “/full/path/constants.php”). When I include functions.php within the snippet function, I get the fatal error.

    Am I even going down a road where success is possible here?

    Thank you again for your help.

    • This reply was modified 7 years, 9 months ago by carymillsap.
    Plugin Author Shea Bunge

    (@bungeshea)

    One way to fix this is to use include_once instead of include. This way, it doesn’t matter how many times you try and include the file, only the first time you do will actually load the functions and constants.

    Another thing you can do: if you really want to include your file outside of the function, inside the function you will need to use the global keyword to allow the variables defined in your constants file to work inside the function.

    So, for example, if in your constants file you had this:

    $x = 42;

    In your snippet, you could include the file outside the function and then use the $foo variable inside your function like so:

    include_once '/home/me/public_html/cgi-bin/constants.php';
    
    add_shortcode('my_shortcode', function (atts) {
    	global $x;
    	return "x is '$x'";
    }

    The key thing is that you include global $var at the beginning of each function where you want to use the variable.

    Thread Starter carymillsap

    (@carymillsap)

    Shea, I sure appreciate the help. Your response helps me put my finger on the core problem, which I can demonstrate without even mentioning include:

    $x = 42;
    add_shortcode("s", "f");
    function f($atts) {
      global $x;
      return "x is '$x'";
    }

    Using the shortcode [s] produces the text x is ''.

    Furthermore, if I include a file that has something like this in it:

    $constant=42;
    function g() {
      global $constant;
      return $constant;
    }

    …then g() returns an empty result as well, to the snippet. When I’m including the same file from https://mysite.com/something.php that calls g(), in that context, it returns 42. But it returns empty when called from a Snippet.

    • This reply was modified 7 years, 9 months ago by carymillsap.
    • This reply was modified 7 years, 9 months ago by carymillsap.
    Thread Starter carymillsap

    (@carymillsap)

    My whole site is down now. I tried to define a second snippet, which includes the same “functions.php” that we’ve been talking about. Now every page on my site (even my admin pages) renders:

    Fatal error: Cannot redeclare distribution_filename() (previously declared in /home/methodr8/public_html/cgi-bin/functions.php:10) in /home/methodr8/public_html/cgi-bin/functions.php on line 15

    I know I need to change include to include_once, but now I cannot even open my Dashboard. I’m going to need help (please!) fixing this from an SSH command line.

    Thread Starter carymillsap

    (@carymillsap)

    I got my site back up, by renaming wp-content/plugins/code-snippets to …/code-snippets-xxx, and I’ve found the Safe Mode instructions in the readme.txt file now, so I’m pretty sure I’m going to live :-).

    Thread Starter carymillsap

    (@carymillsap)

    It’s pretty darned impressive, actually, the documentation you’ve provided with the product. I ’bout lost my liver when my site broke, but your instructions on what to do what that happens were crystal clear and effective. By using include_once instead of include, now I’m good.

    I’m still interested in your response to my problem with global, though. Will eagerly await what you have to say about that.

    Thanks again for the help!

    Plugin Author Shea Bunge

    (@bungeshea)

    I’m glad to hear you could sort things out, and that everything appears to be working now.

    In regards to your global question, you will also need to introduce the global keyword outside the function when you first set the variable – like so:

    global $x;
    $x = 42;
    add_shortcode("s", "f");
    function f($atts) {
      global $x;
      return "x is '$x'";
    }
    Thread Starter carymillsap

    (@carymillsap)

    Thank you, Shea. Problem solved.

    Plugin Author Shea Bunge

    (@bungeshea)

    Awesome, that’s great to hear.

Viewing 10 replies - 1 through 10 (of 10 total)
  • The topic ‘Global variables’ is closed to new replies.