Write to a php file via wordpress

I’ve built a web page that is able to send text messages to employees at the company where I work. With new employees being added and removed on a constant basis, I want to integrate this app with wordpress where the employees can be managed without editing the code.

Each post would contain the user’s name and phone number. The name would be pulled in on the webpage as an option for the user to contact. When the form is submitted, it would go to a php form that runs an if/else to find the employee and match that employee with their phone number like so:

Read More
//Who the text message is to, Establish their phone #
if($employee == 'brad'):
     $text_to[] = '+15555555555';
elseif ($employee == 'mary'):
     $text_to[] = '+15555555555'; 
elseif ($employee == 'tom'):
     $text_to[] = '+15555555555'; 
elseif ($employee == 'bill'):
     $text_to[] = '+15555555555'; 
elseif ($employee == 'joe'):
endif;

I want to be able to not only pull these names from wordpress via a loop to display onto my page, but also to be able to add or remove the new entries, along with their phone number, to this php contact form.

I know how to loop through the wordpress posts to display the names on the page. I want to know if it’s possible to also use this data to modify this contact form, and if so, how to set this up. Each time someone edits the wordpress entry for Joe, the PHP form gets this update so that when Joe is sent a message, it finds his phone number and sends him the message.

Any help is greatly appreciated. I should also note that I’m using Twilio to send the texts messages.

Related posts

3 comments

  1. The use of a DMBS works well, but is not the only solution.

    include ‘users.inc’; // flat file creating array $userList
    which could be an array( ofArrays ) for complex structures

    [assume $userList = array($user => $phone, …); ]
    then foreach( $userList as $user => $phone ) { // process($user, $phone); }

  2. Kudos for trying to solve a simple problem yourself. It’s obvious that you are new to the idea of databases, and quite frankly, you’re doing this the wrong way.

    Let’s talk about how your initial approach can be improved:

    Each post would contain the user’s name and phone number.

    Perhaps instead of posts, we have one database table to store employee information.

    You’ll find tons of information on databases and how to use them with your wordpress account with a simple google search.

    I want to be able to not only pull these names from wordpress via a loop to display onto my page, but also to be able to add or remove the new entries, along with their phone number, to this php contact form.

    Again, if you had a database to store employee information, this is basic.

    For example, an employees database table may have the following columns:

    • id
    • name
    • phone
    • created_at
    • updated_at

    Since PHP has built in functions for communicating with a MYSQL database (which is most likely what WordPress is already using), you can do things like:

    Get all employee data

    SELECT * FROM employees

    Get a certain employee’s data

    SELECT * FROM employees WHERE name = '$name'

    $name is a variable that can be set via POST request from a WordPress form

    Update a certain employee’s data

    UPDATE employees SET phone = '+15555555555' WHERE name = '$name'

    Remove a certain employee’s data

    DELETE FROM employees WHERE id = 5

    You are also able to do things like:

    SELECT * FROM employees WHERE created_at > '3/1/2015'

    Which will return all employees that were added after 3/1/15.

  3. So everything that you all suggested was helpful. I may have not been as clear as I could have that I really needed the creation/editing/deletion of entries to be done via wordpress posts. I was able to set it up like this by doing the following:

    //pull variables from html form input. Employee variables are post ID's that I will then be able to use to retrieve the post_content which contains their phone numbers
       $employee1 = $_POST['employeeName1'];
       $employee2 = $_POST['employeeName2'];
       $employee3 = $_POST['employeeName3'];
       $customMsg = $_POST["textMessage"];
    
    //Create array from above variables and exclude any that lack post data
    $employees = array($employee1, $employee2, $employee3);
    $setEmployeeIDs = array();
    foreach ($employees as $employee) {
        if (!empty($employee)) {
            $setEmployeeIDs[] = $employee;
        }
    }
    
    $servername = "xxxx";
    $username = "xxxx";
    $password = "xxxx";
    $dbname = "xxxx";
    
    // Create connection
    $conn = new mysqli($servername, $username, $password, $dbname);
    // Check connection
    if ($conn->connect_error) {
        die("Connection failed: " . $conn->connect_error);
    } 
    //Get array of Phone Numbers connected to each ID and add to array
    $sql = "SELECT * FROM wp_posts WHERE ID IN (".implode(',',$setEmployeeIDs).")";
    $result = $conn->query($sql);
    $phoneNumbers = array();
    if ($result->num_rows > 0) {
        // output data of each row
        while($row = $result->fetch_assoc()) {
           $phoneNumbers[] = $row["post_content"] ;
        }
    } else {
        echo "0 results";
    }
    $conn->close();
    

    So to sum it up, I used wordpress to publish/edit/delete that data as posts, then sent the ID’s selected from the webpage to the php form, and then was able to retrieve the phone numbers from the SQL database using Raphael’s direction.

Comments are closed.