Get Admin Email Address From External PHP page

I am setting up a “built in” contact form that I need to pull the administrators email address from an external php page.

For instance, my form action="path/to/process_form.php"

Read More

Within that proces_form.php I have a few options set up.

$msg = "Form Contents: nn";
foreach ($this->fields as $key => $field)
    $msg .= "$key :  $field n";

$to = 'static@email.com';
$subject = 'New Form Submission';
$from = $this->fields['email'];

My goal is to dynamicly grab the admin email address from this page and populate the $to = '' area.

Any help would be much much appreciated!

Related posts

Leave a Reply

3 comments

  1. How about this:

    <?php
    // Connect to WP 
    require('/path/to/wp-blog-header.php'); 
    global $wpdb;
    
    $admin_email = get_option( 'admin_email' ); ?> 
    
    $msg = "Form Contents: nn";
    foreach ($this->fields as $key => $field)
        $msg .= "$key :  $field n";
    
    $to = $admin_email;
    $subject = 'New Form Submission';
    $from = $this->fields['email'];
    
    ?>
    
  2. Basically what you’re looking to do is create a separate page within a WordPress site that (I’m sort of guessing here) you’re not creating within WordPress itself, but you want to pull WordPress data.

    There’s a “cheater” method and an officially sanctioned method to do what you want.

    The “cheater” method is to call wp-load.php from your separate PHP file. This will load the WP database stuff and give you access to WP functions. Many people think this is easier, but I do not recommend it because it can cause problems in certain setups.

    The official way to do it is to go from within WordPress itself. Specifically, by hooking the template_redirect action you can redirect any URL you like to whatever PHP file you want — while still having access to WP because you’re running from within WP.

    Then use get_userdata() to fetch the User’s email address.

  3. You won’t be able to include it without executing it, and it looks like that would be a problem.

    Unless you can edit the “external php page” and set the variables in a more convenient way you are probably going to be stuck with reading the file and regex-ing the email out of it. The problem with that approach is that you don’t have control of the file so it could change and your code would break. It could be memory intensive and slow as well. I’d only do it that way if I had to.

    The best solution is to set the email value elsewhere, perhaps as a constant, and forget about trying to extract an email address. It would help to see the code for the “external php page”.