0

What i am trying to do is to have this function working within my plugin block of code without having it as plugin on it own.

My current and working solution outside wordpress is

Save contents

$KeepText="$name||$Phone||";
$dataHolder="keepdata.txt";
$file = fopen($dataHolder,"w");
if(fwrite($file, $KeepText)){echo "Data Saved Successfully !";}
else{echo "Error Occur !";}
fclose($file);

Retrieve data

$dataHolder="keepdata.txt";
$dataPool=file($dataHolder);
$TextPool=explode("||", (string)$dataPool[0]);

if ($dataPool) {
$ContactName=$TextPool[0];
$ContactPhone = $TextPool[1];
}

Output //ContactName //ContactPhone

The logic will perfectly print out all required data.

However doing the same thing within WordPress generate errors or not even echoing out datas.

I have came across recommended solutions which suggested using the WP_Filesystem . From This Website I have tried it in which it work fine as a standalone Plugin . But that is not the solution I want.

What I will like to do is have the wp_filesystem like this without admin_menu().

function filesystem_init($form_url, $method, $context, $fields = null) {
    global $wp_filesystem;

    if (false === ($creds = request_filesystem_credentials($form_url, $method, false, $context, $fields))) {

    return false;
        }

        if (!WP_Filesystem($creds)) {
        request_filesystem_credentials($form_url, $method, true, $context);
        return false;
        }
        return true; 
    }

And later have the function dataTosave(){} like the below

function dataTosave($form_url){
    global $wp_filesystem;

    check_admin_referer('contact_form');

    $contactname = sanitize_text_field($_POST['contactname']); 
    $contactphone = sanitize_text_field($_POST['contactphone']); 

    $form_fields = array('contactname','contactphone'); 

    $method = ''; 
    $context = WP_PLUGIN_DIR . '/keepdata'; 

    $form_url = wp_nonce_url($form_url, 'contact_form'); 

    if(!filesystem_init($form_url, $method, $context, $form_fields))
        return false; 

    $target_dir = $wp_filesystem->find_folder($context);
    $target_file = trailingslashit($target_dir).'/keepdata.txt';


    $dataTosave=$contactname."||".$contactphone."||";
    if(!$wp_filesystem->put_contents($target_file, $dataTosave, FS_CHMOD_FILE)) 
        return new WP_Error('writing_error', 'Error when writing file');           

    return "Data Saved";
}

Once the form is post

 if(isset($_POST['contactform_submit'])){
dataTosave();
//new submission run the function
}

<form method="post" action="" >
<?php wp_nonce_field('contact_form'); ?>
    <label for="contactname">Contact Name</label><br>
    <input id="contactname" name="contactname" value="<?php echo $_POST['contactname']?>" require >
<label for="contactphone">Contact Phone</label><br>
    <input id="contactphone" name="contactphone" value="<?php echo $_POST['contactphone']?>" required >



<?php submit_button('Submit', 'primary', 'contactform_submit', true);?>

</form>

I have do it like this but no luck .Thanks

0

Reading and writing to a file is going to be dicey. You are going to be fighting with file permissions if you save anywhere but the wp-content/uploads directory (and it looks like you want to save to the plugins directory). If you have sufficient control of the server, you may get this to work by editing permissions in the plugins directory, but I wouldn't. It introduces, potentially, security problems. At any rate, requiring that directory permissions be edited will make your code very non-portable.

  1. You can save your file to wp-content/uploads but it may well accessible to snooping.
  2. You can use WP_Filesystem but you will need to get request_filesystem_credentials() working most likely. I don't know what you mean by using it without admin_menu() but I am thinking you mean without the dialogue. If I remember correctly, if you hardcode credentials in to wp-config.php you won't get the popup dialogue. You may be requiring users to edit files, which makes your code less portable.
  3. The best option to me is to simply not try to juggle data into and out of a file. Save your data as user meta in the database.
5
  • With your explanation I have change my mind to use Database instead of writing to file. However wp_usermeta structure will not fit into what I need to do so i will have to create table for this purpose. Thanks for your response
    – ShapCyber
    Jun 23 '15 at 0:58
  • Why won't the usermeta table work? Your data does not look very complicated.
    – s_ha_dum
    Jun 23 '15 at 1:00
  • As you can see I need to add contact name and phone number . my idea is not for the user to register on wp registration page neither have any role to play on the site all they needs will be provides within the plugin scope and ends there.
    – ShapCyber
    Jun 23 '15 at 1:03
  • If I am not asking you too much, can you produce block of code that can accomplish such task using wp_usermeta
    – ShapCyber
    Jun 23 '15 at 1:06
  • Not if you are going to completely bypass user registration, which, by the way, I also consider a bad idea. However, you could save to the $wpdb->options table instead of the $wpdb->usermeta table.
    – s_ha_dum
    Jun 23 '15 at 1:08

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.