编辑Wordpress插件以添加我自己的功能

时间:2014-02-10 19:22:07

标签: php wordpress csv

我正在尝试为此php脚本添加更多字段。默认情况下,它允许访问您网站的访问者输入他们的姓名和电子邮件地址,将其注册到后端的列表中,然后我可以将其导出为.csv文件。目前我正在尝试添加电话号码,城市,州等的字段。我已经能够添加字段,但它们不会在后端注册。实际上,当我完成添加一个字段时,它会打破其他所有字段。这是我的PHP代码。任何引用数字或sml_number的东西都是我想要添加的东西。

sml.php:                 

            // Plugin Activation
            function sml_install() {
                global $wpdb;
                $table = $wpdb->prefix."sml";
                $structure = "CREATE TABLE $table (
                    id INT(9) NOT NULL AUTO_INCREMENT,
                    sml_name VARCHAR(200) NOT NULL,
                    sml_email VARCHAR(200) NOT NULL,
                    sml_number VARCHAR(200) NOT NULL,
                UNIQUE KEY id (id)
                );";
                $wpdb->query($structure);

            }
            register_activation_hook( __FILE__, 'sml_install' );

            // Plugin Deactivation
            function sml_uninstall() {
                global $wpdb;

            }
            register_deactivation_hook( __FILE__, 'sml_uninstall' );

            // Left Menu Button
            function register_sml_menu() {
                add_menu_page('Subscribers', 'Subscribers', 'add_users', dirname(__FILE__).'/index.php', '',   plugins_url('sml-admin-icon.png', __FILE__), 58.122);
            }
            add_action('admin_menu', 'register_sml_menu');

            // Generate Subscribe Form 

            function smlsubform($atts=array()){
                extract(shortcode_atts(array(
                    "prepend" => '',  
                    "showname" => true,
                    "nametxt" => 'Name:',
                    "nameholder" => 'Enter your name.',
                    "emailtxt" => 'Email:',
                    "emailholder" => 'Enter your email address.',
                    "numbertxt" => 'Phone Number:',
                    "numberholder" => 'Enter your Phone Number.',
                    "showsubmit" => true,
                    "submittxt" => 'Subscribe',
                    "jsthanks" => false,
                    "thankyou" => 'Thank you for subscribing to our mailing list!'
                ), $atts));

                $return = '<form class="sml_subscribe" method="post"><input class="sml_hiddenfield" name="sml_subscribe" type="hidden" value="1">';

                if ($prepend) $return .= '<p class="prepend">'.$prepend.'</p>';

                if ($_POST['sml_subscribe'] && $thankyou) { 
                    if ($jsthanks) {
                        $return .= "<script>window.onload = function() { alert('".$thankyou."'); }</script>";
                    } else {
                        $return .= '<p class="sml_thankyou">'.$thankyou.'</p>'; 
                    }
                }


                if ($showname) $return .= '<p class="sml_name"><label class="sml_namelabel" for="sml_name">'.$nametxt.'</label><input class="sml_nameinput" placeholder="'.$nameholder.'" name="sml_name" type="text" value=""></p>';
                $return .= '<p class="sml_email"><label class="sml_emaillabel" for="sml_email">'.$emailtxt.'</label><input class="sml_emailinput" name="sml_email" placeholder="'.$emailholder.'" type="text" value=""></p>';
                $return .= '<p class="sml_number"><label class="sml_numberlabel" for="sml_number">'.$numbertxt.'</label><input class="sml_numberinput" name="sml_number" placeholder="'.$numberholder.'" type="text" value=""></p>';
                if ($showsubmit) $return .= '<p class="sml_submit"><input name="submit" class="btn sml_submitbtn" type="submit" value="'.($submittxt?$submittxt:'Submit').'"></p>';
                $return .= '</form>';

                return $return;
            }
            add_shortcode( 'smlsubform', 'smlsubform' );

            // Handle form Post
            if ($_POST['sml_subscribe']) {
                $name = $_POST['sml_name'];
                $email = $_POST['sml_email'];
                $number = $_POST['sml_number'];
                if (is_email($email)) {

                    $exists = mysql_query("SELECT * FROM ".$wpdb->prefix."sml where sml_email like '".$wpdb->escape($email)."' limit 1");
                    if (mysql_num_rows($exists) <1) {
                        $wpdb->query("insert into ".$wpdb->prefix."sml (sml_name, sml_email, sml_number) values ('".$wpdb->escape($name)."', '".$wpdb->escape($email)."', '".$wpdb->escape($number)."')");
                    }
                }
            }

            function plugin_get_version() {
                $plugin_data = get_plugin_data( __FILE__ );
                $plugin_version = $plugin_data['Version'];
                return $plugin_version;
            }

            ?>

的index.php

<div class="wrap">

    <?php screen_icon( 'themes' ); ?>
    <h2>Mail list subscribers</h2>

    <div id="poststuff">

        <div id="post-body" class="metabox-holder columns-3">

            <!-- main content -->
            <div id="post-body-content">

            <form method="post" action="?page=<?php echo esc_js(esc_html($_GET['page'])); ?>">
            <input name="sml_remove" value="1" type="hidden" />
                        <?php 
                        if ($_SERVER['REQUEST_METHOD']=="POST" and $_POST['sml_remove']) {
                            if ($_GET['rem']) $_POST['rem'][] = $_GET['rem'];
                            $count = 0;
                            if (is_array($_POST['rem'])) {
                                foreach ($_POST['rem'] as $id) { 
                                    $wpdb->query("delete from ".$wpdb->prefix."sml where id = '".$wpdb->escape($id)."' limit 1"); 
                                    $count++; 
                                }
                                $message = $count." subscribers have been removed successfully.";
                            }
                        }

                        if ($_SERVER['REQUEST_METHOD']=="POST" and $_POST['sml_import']) {
                            $correct = 0;
                            if($_FILES['file']['tmp_name']) {
                                if(!$_FILES['file']['error'])  {
                                    $file = file_get_contents ($_FILES['file']['tmp_name']);
                                    $lines = preg_split('/\r\n|\r|\n/', $file);
                                    if (count($lines)) {
                                        $sql = array();
                                        foreach ($lines as $data) {
                                            $data = explode(',', $data);
                                            $num = count($data);
                                            $row++;

                                            if (is_email(trim($data[0]))) {
                                                $c = $wpdb->get_row("SELECT * FROM ".$wpdb->prefix."sml where sml_email LIKE '".$wpdb->escape(trim($data[0]))."' limit 1", ARRAY_A);
                                                if (!is_array($c)) {                                            
                                                    $wpdb->query("INSERT INTO ".$wpdb->prefix."sml (sml_email, sml_name, sml_number) VALUES ('".$wpdb->escape(trim($data[0]))."', '".$wpdb->escape(trim($data[1]))."', '".$wpdb->escape(trim($data[0]))."')");
                                                    $correct++;
                                                } else { $exists++; }
                                            } else { $invalid++; }
                                        }

                                    } else { $message = 'Oh no! Your CSV file does not apear to be valid, please check the format and upload again.'; }

                                    if (!$message) {
                                        $message = $correct.' records have been imported. '.($invalid?$invalid.' could not be imported due to invalid email addresses. ':'').($exists?$exists.' already exists. ':'');
                                    }

                                } else {
                                    $message = 'Ooops! There seems to of been a problem uploading your csv';
                                }
                            }                               
                        }
                        //echo $sql;
                        if ($message) { echo '<div style="padding: 5px;" class="updated"><p>'.$message.'</p></div>'; }

                        ?>

                        <table cellspacing="0" class="wp-list-table widefat fixed subscribers">
                          <thead>
                            <tr>
                                <th style="" class="manage-column column-cb check-column" id="cb" scope="col"><input type="checkbox"></th>
                                <th style="" class="manage-column column-name" id="name" scope="col">Name<span class="sorting-indicator"></span></th>
                                <th style="" class="manage-column column-email" id="email" scope="col"><span>Email Address</span><span class="sorting-indicator"></span></th>
                                <th style="" class="manage-column column-number" id="number" scope="col"><span>Number</span><span class="sorting-indicator"></span></th>

                           </thead>

                            <tfoot>
                            <tr>
                                <th style="" class="manage-column column-cb check-column" scope="col"><input type="checkbox"></th>
                                <th style="" class="manage-column column-name" scope="col"><span>Name</span><span class="sorting-indicator"></span></th>
                                <th style="" class="manage-column column-email" scope="col"><span>Email Address</span><span class="sorting-indicator"></span></th>
                                <th style="" class="manage-column column-number" scope="col"><span>Number</span><span class="sorting-indicator"></span></th>
                            </tfoot>

                            <tbody id="the-list">

                            <?php 

                                $results = $wpdb->get_results( "SELECT * FROM ".$wpdb->prefix."sml");
                                if (count($results)<1) echo '<tr class="no-items"><td colspan="3" class="colspanchange">No mailing list subscribers have been added.</td></tr>';
                                else {
                                    foreach($results as $row) {

                                        echo '<tr>
                                                    <th class="check-column" style="padding:5px 0 2px 0"><input type="checkbox" name="rem[]" value="'.esc_js(esc_html($row->id)).'"></th>
                                                    <td>'.esc_js(esc_html($row->sml_name)).'</td>
                                                    <td>'.esc_js(esc_html($row->sml_email)).'</td>
                                                    <td>'.esc_js(esc_html($row->sml_number)).'</td>

                                              </tr>';




                                    }
                                }

                            ?>


                            </tbody>
                        </table>
                        <br class="clear">
                        <input class="button" name="submit" type="submit" value="Remove Subscribers" /> <a class="button" href="<?php echo plugins_url( 'export-csv.php', __FILE__ ); ?>">Export List</a>
                </form>
                <br class="clear">


                <div class="meta-box-sortables">



            <div id="postbox-container-1" class="postbox-container">
                    <div class="meta-box-sortables">

                    </div> 
                </div> 
            </div>
            <br class="clear">
    </div>

</div> 

出口csv.php

<?php 
require_once '../../../wp-load.php';
if (current_user_can('manage_options')) {
    header("Content-type: application/force-download"); 
    header('Content-Disposition: inline; filename="subscribers'.date('YmdHis').'.csv"');  
    $results = $wpdb->get_results( "SELECT * FROM ".$wpdb->prefix."sml");
    echo "First Name,Last Name,Email Address\r\n";
    if (count($results))  {
        foreach($results as $row) {
            $n = doSplitName($row->sml_name);
            echo $n['first'].','.$n['last'].','.$row->sml_email."\r\n";
        }
    }
}

function doSplitName($name) {
    $results = array();

    $r = explode(' ', $name);
    $size = count($r);

    if (mb_strpos($r[0], '.') === false) {
        $results['salutation'] = '';
        $results['first'] = $r[0];
    } else {
        $results['salutation'] = $r[0];
        $results['first'] = $r[1];
    }

    if (mb_strpos($r[$size - 1], '.') === false) {
        $results['suffix'] = '';
    } else {
        $results['suffix'] = $r[$size - 1];
    }

    $start = ($results['salutation']) ? 2 : 1;
    $end = ($results['suffix']) ? $size - 2 : $size - 1;

    $last = '';
    for ($i = $start; $i <= $end; $i++) {
        $last .= ' '.$r[$i];
    }
    $results['last'] = trim($last);

    return $results;
}
?>

非常感谢任何和所有帮助。或者甚至只是推动正确的方向。几个星期以来我一直在研究这个问题,我对php并不是很了不起。非常感谢你。

0 个答案:

没有答案