WordPress and Plugin Development

I’m currently in the process of building an existing website I built into WordPress. I’m making good progress but I’ve hit a hitch. I am well aware that WordPress has thousands of plugins already there that can be used but I’ve decided I would like to give it a go and try build my own. I came across an article on the internet that was a good starting part – but I’m getting an error and I’m not sure how to fix it.

I am using the following tutorial posted on this website – http://starkod.net/build-wordpress-contact-forms-plugin-save-to-db-and-send-email/

Read More

So far following the steps in the tutorial I’ve been able to make the plugin display in the plugins menu – activate the plugin – upon activating the plugin the custom table is added to the database to store any records, the menu item for the contact form responses is visible in the wordpress dashboard but after that I get the following error –

Parse error: syntax error, unexpected ‘=’, expecting ‘)’

The error says its based on line 73 of the code

//if you want to have both logged in and not logged in users submitting, you have to add both actions!
add_action( 'admin_post_add_foobar', 'starkod_admin_add_foobar' );
add_action( 'admin_post_nopriv_add_foobar', 'starkod_admin_add_foobar' );
function starkod_admin_add_foobar()
{

global $wpdb;
$data=array(
'name' => sanitize_text_field($_POST['name']),
'email' => isset($_POST['email'])?sanitize_email($_POST['email']):null,
'msg' => sanitize_text_field($_POST['msg']),
'time' => current_time('mysql')
);
$table_name=$wpdb->prefix.'starkod';
$headers = array('Content-Type: text/html; charset=UTF-8');
// send Email for admin
wp_mail( get_option('admin_email'), 'starkod Contact Form',
'Name : '.$data['name']. ' phone : '.$data['phone'].' email : '.$data['email'].' The message: '.$data['msg'].' Time : '.$data['time'] ,$headers);
if($wpdb->insert( $table_name, $data))
{
$_SESSION['message'] = "Your Message received , thanks ";
}
else
{
$_SESSION['message'] = "there's problem try again please";
}
//redirect back to where user was comming
wp_redirect($_SERVER['HTTP_REFERER']);
//request handlers should die() when they complete their task
}

Line 73 of code starts here –

'name' => sanitize_text_field($_POST['name']),

If anyone could help me out to fix this it would be much appreciated.

Related posts

1 comment

  1. The issue with the provided code is that you should have > on every place that you currently have >. Like so (I “beautified” it a little):

    //if you want to have both logged in and not logged in users submitting, you have to add both actions!
    add_action( 'admin_post_add_foobar', 'starkod_admin_add_foobar' );
    add_action( 'admin_post_nopriv_add_foobar', 'starkod_admin_add_foobar' );
    function starkod_admin_add_foobar() {
        global $wpdb;
        $data = array(
            'name'  => sanitize_text_field( $_POST['name'] ),
            'email' => isset( $_POST['email'] ) ? sanitize_email( $_POST['email'] ) : null,
            'msg'   => sanitize_text_field( $_POST['msg'] ),
            'time'  => current_time( 'mysql ')
        );
    
        $table_name = $wpdb->prefix.'starkod';
        $headers = array( 'Content-Type: text/html; charset=UTF-8' );
        // send Email for admin
        wp_mail(
            get_option( 'admin_email' ),
            'starkod Contact Form',
            'Name : ' . $data['name'] .
            ' phone : ' . $data['phone'] .
            ' email : ' . $data['email'] .
            ' The message: ' . $data['msg'] .
            ' Time : ' . $data['time'],
            $headers
        );
    
        if ( $wpdb->insert( $table_name, $data ) ) {
            $_SESSION['message'] = "Your Message received , thanks ";
        } else {
            $_SESSION['message'] = "there's problem try again please";
        }
        //redirect back to where user was comming
        wp_redirect( $_SERVER['HTTP_REFERER'] );
        //request handlers should die() when they complete their task
    }
    

Comments are closed.